简体   繁体   English

当类在视口中时显示元素

[英]show element when class is in viewport

I'm trying to make a div only appear when elements of a certain class are visible in the viewport. 我试图使div仅当某个类的元素在视口中可见时才显示。

I got almost there via this http://jsfiddle.net/blowsie/M2RzU/ 我几乎是通过http://jsfiddle.net/blowsie/M2RzU/到达的

$(document).ready(function(){
    $('.myclass').bind('inview', function (event, visible) {
      if (visible == true) {
        // element is now visible in the viewport
        $(this).removeClass('myclass');
          alert('found h2!')
      } else {
        // element has gone out of viewport
         $(this).addClass('myclass');
      }
    });
});

but as you can see by this edit http://jsfiddle.net/deenbag/6D9x5/ the event is triggered every time the any element with the class enters or exits the viewport, thus an exiting element will turn off the desired effect even if another element with the relevant class is visible. 但是正如您通过此编辑http://jsfiddle.net/deenbag/6D9x5/所见,每次具有该类的任何元素进入或退出视口时都会触发该事件,因此即使具有相关类的另一个元素是可见的。

i also have been messing with this plugin but couldn't figure out how to get it to apply to what i want to do. 我也一直在用这个插件弄乱,但不知道如何使它适用于我想做的事情。 http://opensource.teamdf.com/visible/examples/demo-basic.html http://opensource.teamdf.com/visible/examples/demo-basic.html

Just keep track of the visible elements in an array: 只需跟踪数组中的可见元素:

var visibleEls = [];
$('.myclass').bind('inview', function (event, visible) {
    if (visible == true) {
        // element is now visible in the viewport
        if(!~visibleEls.indexOf(this)) visibleEls.push(this);
    } else {
        // element has gone out of viewport
        var i=visibleEls.indexOf(this);
        if(~i) visibleEls.splice(i, 1);
    }
    $('body').toggleClass('red', !!visibleEls.length);
});

Demo 演示版


Note that you could simplify it to 请注意,您可以将其简化为

var counter = 0;
$('.myclass').bind('inview', function (event, visible) {
    counter += visible*2-1;
    $('body').toggleClass('red', !!counter);
});

but this could be more error-prone. 但这可能更容易出错。

Demo 演示版

Since I don't have enough reputation to comment I'm going to attempt an answer. 由于我没有足够的声誉来发表评论,所以我将尝试一个答案。 What if you added a counter that kept track of how many elements with the myclass class are "in view" then every time the "inview" event triggers you could update the counter and if it's 1 you switch on if it's 0 you switch off if it's anything else you do nothing. 如果添加了一个计数器来跟踪myclass类中有多少个元素在“可见”状态,那么每次“ inview”事件触发时,您都可以更新该计数器,如果它是1,则打开它;如果是0,则关闭它,该怎么办?别的什么都不做。 Here's my jsfiddle . 这是我的jsfiddle

$(document).ready(function(){
    numInView = 0;
    $('.myclass').bind('inview', function (event, visible) {
      if (visible) {
        numInView++;
      } else {
        numInView--;
      }
      if (numInView === 1) {
        // element is now visible in the viewport
        $("h2").removeClass('myclass');
        $('body').css('color','red');
      } else if (numInView === 0) {
        // element has gone out of viewport
        $("h2").addClass('myclass');
        $('body').css('color','black');
      }
    });
});

Pro-tip: When testing a condition you don't need to compare it to true unless you're trying to discount "truthy" values (like the number 1), but not true. 专家提示:测试条件时,无需将其与true进行比较,除非您尝试不考虑“真实的”值(如数字1),但不是true。 If the thing you're testing is true then the condition will pass anyway without comparing it to the value true. 如果您要测试的内容为true,则该条件无论如何都会通过而不会将其与true值进行比较。 That is save your self some code: 那为您节省了一些代码:

if (visible == true) {
    bar();
} else {
    bazz();
}

does the same as 与...相同

if (visible) {
    bar();
} else {
    bazz()
}

in this scenario. 在这种情况下。

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

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