Trigger JQuery function when passed half way down the page

前端 未结 4 1435
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 04:34

Is there a way to tell if you have scrolled passed the center of the web page or in other words, when you have scrolled passed exactly half of the web page and your scrollba

相关标签:
4条回答
  • 2020-12-20 04:57

    You can get the pixel amount of an element has been scrolled by using .scrollTop(). To listen to scroll events use .scroll().

    When you want to identify the halfway, use height of the scroll:

    $(window).scroll(function () { 
      if ($(window).scrollTop() > $('body').height() / 2) {
        $('.pineapple-man').show();
      } 
    });
    

    If you are scrolling some other element than the whole window/body, please feel free to change the selectors.

    To make the showing one-timer, add the removal of scroll event listener, by adding the following after the .show() call:

    $(window).unbind('scroll');
    
    0 讨论(0)
  • 2020-12-20 05:07

    I guess you want to do something like this:

    if($(document).scrollTop() > $(document).height()/2){
       $('.pineapple-man').show();
    }
    

    where scrollTop() gets the current horizontal position and height() defines the document height.

    0 讨论(0)
  • 2020-12-20 05:09

    you can use the focus event if you scroll down to it (just like jQuery uses for their comments)

    jQuery('selector').focus(function() {
      jQuery('.page').show();
    });
    
    0 讨论(0)
  • 2020-12-20 05:12

    See the scroll event and the scrollTop method.

    0 讨论(0)
提交回复
热议问题