Scarface
Scarface

Reputation: 3923

Event occur on scrolldown

I am just trying to execute some code when the scroll bar of the page is about halfway down my page (or 500px). The issue is that this code works for going all the way to the bottom of the page to execute but if I supply numbers nothing happens when scrolling. Does anyone know how to have it so the event will execute when scrolling 500px down?

Works

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

Does Not

$(window).scroll(function(){
        if  ($(window).scrollTop() == parseInt(500)){

Upvotes: 2

Views: 490

Answers (2)

VAShhh
VAShhh

Reputation: 3514

Do you really need to trigger that exactly at 500px? can't you do something like

$(window).scroll(function(){
        if  ($(window).scrollTop() >= 500){

use a flag variable if you want to trigger this event just once, but keep in mind that .scroll() is not triggered at EACH pixel the window is scrolling

a flag approach can be like this

var flag = false;
$(window).scroll(function(){
        if  ($(window).scrollTop() >= 500 && flag == false){
            flag = true;
            //do what you want

Upvotes: 3

red
red

Reputation: 2050

If you console.log the scrollTop parseInt you will see that the figure does not grow incrementally, but rather in chunks. You'd have to be quite lucky to hit exactly 500. Could the scroll event trigger when the user is PAST 500 mark? If that works for you, just change the if to > 500.

Upvotes: 1

Related Questions