简体   繁体   English

jQuery .scroll无法启动

[英]jQuery .scroll not picking up

I'm trying create an alert saying "True" when the user scrolls past the "#topp" element, yet it isn't doing anything, the element is just supposed to be a tiny div at the top of the page. 我正在尝试创建一个警报,当用户滚动到“ #topp”元素上时显示“ True”,但它没有执行任何操作,该元素只是页面顶部的一个小div。

HTML 的HTML

<div id="topp"></div>

jQuery jQuery的

    $(window).scroll(function() {
    var vpH = $(window).height(),
        st = $(window).scrollTop(),
        y = $('#topp').offset().top;
    if(y > (st + vpH)) alert('true');
});

Why do you need the window height? 为什么需要窗高? If you have the top and the scroll to top variables then theirs no need for the height of the window. 如果您具有top变量和滚动到top变量,则它们不需要窗口的高度。

$(document).scroll(function() 
{
    var scrollTop = $(window).scrollTop();
    var toppOffset = $('#topp').offset().top;

    if(toppOffset > scrollTop)
        alert('true');
});​

A clearer representation http://jsfiddle.net/zDpw3/1/ 更清晰的表示形式http://jsfiddle.net/zDpw3/1/

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

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