简体   繁体   English

滚动时捕获第一个可见的div id(视口)

[英]Capture first visible div id while scrolling (viewport)

I have this page: 我有这个页面:

在此处输入图片说明

I want to capture on which div I am while I'm scrolling. 我想捕捉滚动时所在的div。

I know If I use: 我知道如果我使用:

if( $(document).scrollTop() > $('#div1').position().top) {  
console.log('Div1')
  }

...it will capture the div1 but instead of using this code for every div I want to set 1 snippet for all divs ...它将捕获div1,但我不想为每个div使用此代码,而是要为所有div设置1个代码段

Something like: 就像是:

var a =   // The div i am at
if( $(document).scrollTop() > $(a).position().top) {    
    console.log($(a).attr('id'))
}

I am looking something like the viewport: http://www.appelsiini.net/projects/viewport/3x2.html 我正在寻找类似视口的东西: http : //www.appelsiini.net/projects/viewport/3x2.html

Can I achieve that without a plugin, simply 2-3 lines? 如果没有插件,只需2-3行,我能否实现?

Here's a nice way to do it. 这是一个不错的方法。 You may want to optimize the '<=' with a pixel offset to improve user experience and move the div selector ($divs) outside the callback to increase performance. 您可能需要使用像素偏移优化'<='以改善用户体验,并将div选择器($ divs)移动到回调之外以提高性能。 Have a look at my fiddle: http://jsfiddle.net/brentmn/CmpEt/ 看看我的小提琴: http : //jsfiddle.net/brentmn/CmpEt/

$(window).scroll(function() {

    var winTop = $(this).scrollTop();
    var $divs = $('div');

    var top = $.grep($divs, function(item) {
        return $(item).position().top <= winTop;
    });
});
   $(window).scroll(function () {

        $("#privacyContent div").each(function () {
            var bottomOffset = ($(this).offset().top + $(this).height());
            console.log("Botom=",bottomOffset ,"Win= ", $(window).scrollTop());
            if (bottomOffset > $(window).scrollTop()) {

                $("#menu a").removeClass("active");
              //  console.log("Div is= ",$(this).attr('id'));
                $("#menu a[href='#" + $(this).attr('id') + "']").addClass("active");
                $(".b").removeClass("fsActive");
                var div = $(this);
                div.find(".b").addClass("fsActive");

                return false;
            }
        });

});

I do it like this it works fine it detect all div id 我这样做是这样,它可以很好地检测所有div ID

Just throw it into a loop. 只需将其放入循环中即可。

var list = [];
$("div").each(function(index) {
    if( $(document).scrollTop() > $(this).position().top) 
        list.push($(this));
});

alert(list);

The list will than have every div that is within your viewport. 列表将包含视口中的每个div。

I'd suggest using the jQuery Inview plugin: 我建议使用jQuery Inview插件:

https://github.com/protonet/jquery.inview https://github.com/protonet/jquery.inview

Well maintained Plugin that detects whatever content is in the viewer currently, enabling you to bind functions to an inview event. 维护良好的插件可检测当前查看器中的内容,使您可以将功能绑定到inview事件。 So as soon as your div is in view you could fire off all the relevant functions you wanted and then again when it has left the users view. 因此,一旦看到div,您便可以关闭所需的所有相关功能,然后在离开用户视图时再次关闭。 Would be great for your needs. 将非常适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM