简体   繁体   English

jQuery-当用户从当前位置向上滚动100px时运行一个函数

[英]jQuery - Run a function when user has scrolled up 100px from current location

I am trying to run a function once the user has scrolled up 100px and another function when they scroll down (any number of pixels) from there current location. 当用户从当前位置向下滚动(任意数量的像素)时,一旦用户向上滚动了100px,我便尝试运行一个函数。 So if the user scrolls half way down a page and then starts to scroll up i need a function to run once they have scrolled up 100px from the current stored scrollTop value. 因此,如果用户向下滚动到页面的一半,然后开始向上滚动,则一旦他们从当前存储的scrollTop值向上滚动了100px,我就需要一个函数来运行。

If the user was 700px down the page and they started to scroll up I need a function to run when they hit 600px. 如果用户在页面上向下移动700px,并且他们开始向上滚动,则当他们达到600px时我需要一个函数来运行。 This should be dynamic from where ever the user is down the page. 无论用户在页面上哪个位置,它都应该是动态的。

As a starting point i have: 作为起点,我有:

var lastScrollTop = 0;

$(window).scroll(function(event){
    var st = $(this).scrollTop();

    if (st > lastScrollTop){
        // Scrolling down
    } else {
        // Scrolling up
    }
    lastScrollTop = st;
});

Thanks 谢谢

jsFiddle(http:/jsfiddle.net/nick_craver/gWD66/1/) jsFiddle(http:/jsfiddle.net/nick_craver/gWD66/1/)

Try this link. 试试这个链接。 May be its very helpful for you 可能对您很有帮助

use this Code 使用此代码

 <script type="text/javascript">
var position = $(window).scrollTop();
$(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if(scroll > position) {
      moveCarDownward(scroll);  // $('#status').html('going down!');
    } else {
       moveCarUpward(scroll);  //$('#status').html('going up!');
    }
    position = scroll;
});

one another link which one in javascript link 另一个链接,其中一个是javascript 链接

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

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