如何检测元素是否已滚出,但只能一次? [英] How to detect if an element has scrolled out, but only once?

查看:92
本文介绍了如何检测元素是否已滚出,但只能一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测一个元素是否被滚动并且已经完成以下代码

I am trying to detect if an element has been scrolled out and have done the following code

$(window).bind('scroll', function(){
    var $btn = $('#intro div.summary a[href=#top]');
    if($(window).scrollTop() > ($btn.offset().top+$btn.height())){
        console.log('out');
    }
});

我在一些正文文本中有一个锚点,我希望克隆并制作一个固定的导航一旦div.intro被滚动出来。

I have an anchor in some body text, that I am hoping to clone and make a fixed nav out of once the div.intro is scrolled out.

我的问题是,一旦元素出来,代码就会触发,但是继续触发。所以我不能再做任何更多的事情继续发射。

My problem is that the code fires as soon as the element is out of view, but keeps firing. So I cannot do any more as anything more will keep firing.

一旦它出来,有没有办法熄灭?除了设置一个变量。

Is there a way to fire 'out' once it's out and 'in' once it's in? Aside from just setting a variable.

推荐答案

只有一次执行该事件才能执行

To execute the event only once you can do

$(window).one('scroll', function(){
    var $btn = $('#intro div.summary a[href=#top]');
    if($(window).scrollTop() > ($btn.offset().top+$btn.height())) {
        console.log('out');
    }
});

这篇关于如何检测元素是否已滚出,但只能一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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