Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript if scroll position > 100

How can I test it if it's greater than 100. For example how can I get an alert.

Here is my code that doesn't work, I don't know why.

var ScrollTop = $("body").scrollTop();

if (ScrollTop > 100){
    alert("Scroll is greater than 100");
    //document.getElementById('back-to-top').fadeOut;
}
like image 303
Alpan Karaca Avatar asked Jul 24 '13 20:07

Alpan Karaca


People also ask

How do I set scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

What is scrollTop in JavaScript?

The Element. scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content.


2 Answers

Use a scroll listener http://jsfiddle.net/6A6qy/

$(window).scroll(function(){...});
like image 143
verbanicm Avatar answered Nov 03 '22 01:11

verbanicm


PERFECT FIDDLE EXAMPLE

var el = $('.test');

el.on('scroll', function(){

  alert(el.scrollTop());


});
like image 36
HIRA THAKUR Avatar answered Nov 03 '22 00:11

HIRA THAKUR