Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 突出显示滚动窗口中当前显示的所有div_Javascript_Jquery_Html - Fatal编程技术网

Javascript 突出显示滚动窗口中当前显示的所有div

Javascript 突出显示滚动窗口中当前显示的所有div,javascript,jquery,html,Javascript,Jquery,Html,我有一些级别相同但高度不同的div。我想在当前窗口框中显示的所有div上添加一个highlight类(窗口框可滚动) 我试过这个密码 $('.l3_box').each(function () { var bottom_of_object = $(this).offset().top + $(this).outerHeight(); var bottom_of_window = $(window).scrollTop() + $(window).height(); /*

我有一些级别相同但高度不同的div。我想在当前窗口框中显示的所有div上添加一个highlight类(窗口框可滚动)

我试过这个密码

$('.l3_box').each(function () {
    var bottom_of_object = $(this).offset().top + $(this).outerHeight();
    var bottom_of_window = $(window).scrollTop() + $(window).height();

    /* If the object is completely visible in the window, fade it it */
    if (bottom_of_window > bottom_of_object) {
        $('#tab_out01 .l3_box').removeClass("testt");
        $(this).addClass("testt");

    }
});
当我的div大小等于或小于窗口的大小时,这种方法可以很好地工作。
但是当我的div大小远大于窗口大小时,它就不起作用了,直到我走到div的末尾。为什么不同时检查顶部和底部

$('#tab_out01 .l3_box').removeClass("testt");
$('.l3_box').each(function () {
    var bottom_of_object = $(this).offset().top + $(this).outerHeight();
    var top_of_object = $(this).offset().top;
    var center_of_window = $(window).scrollTop() + $(window).height() / 2;

    /* If the object is completely visible in the window, fade it it */
    if (bottom_of_object > center_of_window && center_of_window > top_of_object) {
        $(this).addClass("testt");
        return false; // break
    }
});

创建一个fiddle@Ish我无法创建我所拥有的代码,因为它是动态的,但在这里它是一个更苗条的问题。请试穿一下。