如何在jQuery中使用这个函数 [英] How to use this function in jQuery

查看:97
本文介绍了如何在jQuery中使用这个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jQuery函数,它应该检测元素第一次滚动到页面中的视图时:

I have the following jQuery function which is supposed to detect when an element first scrolls into view on a page:

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

今天我的心态很糟糕......我该如何使用这个函数?

My mind is mush today... how can I use this function?

假设我想要检测何时 $('。guardrail360')进入视图。

Assume I want to detect when $('.guardrail360') comes into view.

推荐答案

似乎您需要绑定滚动事件并在事件触发时执行检测。

Seems like you need to bind the scroll event and do the detection when that event fires.

$(window).scroll(function () { 
  isScrolledIntoView('.guardrail360');
});

这篇关于如何在jQuery中使用这个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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