开发者

How to make jquery scroll function take effect earlier?

开发者 https://www.devze.com 2023-04-03 14:03 出处:网络
I have a jquery function that looks like this $(window).scroll(function(){ if($(window).scrollTop() == $(document).height() - $(window).height()){

I have a jquery function that looks like this

$(window).scroll(function(){
            if  ($(window).scrollTop() == $(document).height() - $(window).height()){
               my_funtion();
            }
        });

When a user scrolls down the page and hits bottom my_function executes. However I would like the function to be executed a bit before hitting the bottom. Like 500px or something? How can this be do开发者_运维问答ne? Im sorry but im not a jquery genius and this seems like something that has to be calculated dynamicly. So would really appreciate if someone could help/guide me.

Best regards.


Am i missing something or??? :-)

$(window).scroll(function(){
            if  ($(window).scrollTop() == $(document).height() - $(window).height() - 500){
               my_funtion();
            }
        });

EDIT (this works here - if its lower than 500px to the bottom, it fires!)

(If you have EQUAL then they need to hit the mark exactly, which isnt sure if they scroll further)

EXAMPLE: http://jsfiddle.net/4NBnj/

$(window).scroll(function(){

            var CurrentPost = $(window).scrollTop();
            var DocHeight = $(document).height() - $(window).height();

            if  (CurrentPost > DocHeight-500){
                alert('there!');
            }
        });


I was also thinking about Marco's solution but couldn't make it work on jsfiddle. Here is another way:

$(window).scroll(function(){
    if  ($(document).scrollTop() / $(document).height() > 0.9){){
      alert("you reached 90% of the document");
    }
});


try this one:

jQuery(window).scroll(function () {

    var height = jQuery(document).height() - jQuery(window).height();

    if(height <= (jQuery(window).scrollTop() + 40)){

        your_function();
    }
});


  • $(window).scrollTop() will give you the pixels scrolled out on top,
  • $(document).height() the total height of your webpage and
  • $(window).height() the height of the visible part (height of the browser window)

Now knowing the parameters makes this more a maths problem

Maybe

$(window).scrollTop() >= $(document).height() - $(window).height() - 500

But be careful and use a variable to check if you already called your function otherwise it got called again if you scroll down some more pixels.

0

精彩评论

暂无评论...
验证码 换一张
取 消