当使用jQuery的访问者可见div时触发事件? [英] Fire event when div is visible to visitor with jQuery?

查看:58
本文介绍了当使用jQuery的访问者可见div时触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能:
当用户看到某个div(向下滚动)时,我想更改某个项目的类别。

The function: I want to change the class of a certain item when a user see a certain div (scrolls down to it).

我现在的操作方式:我使用这个精简脚本( http://code.google.com/p/jquery-appear/ ),当一个div对用户可见时,它只是触发一个jquery事件。但是它只会触发一次。所以如果你向下滚动页面jquery执行罚款。但如果我向上滚动,然后再下来它发生火灾。这是我的jquery:

How I do it now: I'm using this fine script (http://code.google.com/p/jquery-appear/) wich works really fine to just fire an jquery event when a div gets visible to the user. However it only fires once. So if you scroll down the page the jquery gets executed fine. But if I scroll up and then down again it doesent fire. This is my jquery:

$('#myDiv').appear(function() {
        $("#aDiv").addClass("active");
});

我想做什么:使脚本每次执行myDiv

What I want to do: Make the script execute everytime "myDiv" appear and not only the first.

有没有人知道我该怎么做?

Does anyone have an idea on how I could do this?

推荐答案

判断div是否可见,您可以:

to judge the div is visible or not, you can:

$(window).scroll(function(event) {
    console.log($(".target").offset().top < $(window).scrollTop() + $(window).outerHeight());
});

所以,我不认为你需要任何插件。

so, i don't think you need any plugin.

只是通过下面的表达式判断:

just judge it by the expression like:

if($(".target").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
    // something when the .target div visible
} else {
    // something when the .target div invisible
}

这篇关于当使用jQuery的访问者可见div时触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆