jQuery API

.scroll()

.scroll( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "scroll" JavaScript event, or trigger that event on an element.

  • version added: 1.0.scroll( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.4.3.scroll( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.scroll()

This method is a shortcut for .bind('scroll', handler) in the first and second variations, and .trigger('scroll') in the third.

The scroll event is sent to an element when the user scrolls to a different place in the element. It applies to window objects, but also to scrollable frames and elements with the overflow CSS property set to scroll (or auto when the element's explicit height or width is less than the height or width of its contents).

For example, consider the HTML:

<div id="target" style="overflow: scroll; width: 200px; height: 100px;">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  sed do eiusmod tempor incididunt ut labore et dolore magna
  aliqua. Ut enim ad minim veniam, quis nostrud exercitation
  ullamco laboris nisi ut aliquip ex ea commodo consequat.
  Duis aute irure dolor in reprehenderit in voluptate velit
  esse cillum dolore eu fugiat nulla pariatur. Excepteur
  sint occaecat cupidatat non proident, sunt in culpa qui
  officia deserunt mollit anim id est laborum.
</div>
<div id="other">
  Trigger the handler
</div>
<div id="log"></div>

The style definition is present to make the target element small enough to be scrollable:

The scroll event handler can be bound to this element:

$('#target').scroll(function() {
  $('#log').append('<div>Handler for .scroll() called.</div>');
});

Now when the user scrolls the text up or down, one or more messages are appended to <div id="log"></div>:

Handler for .scroll() called.

To trigger the event manually, apply .scroll() without an argument:

$('#other').click(function() {
  $('#target').scroll();
});

After this code executes, clicks on Trigger the handler will also append the message.

A scroll event is sent whenever the element's scroll position changes, regardless of the cause. A mouse click or drag on the scroll bar, dragging inside the element, pressing the arrow keys, or using the mouse's scroll wheel could cause this event.

Example:

To do something when your page is scrolled:

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  p { color:green; }
  span { color:red; display:none; }
  </style>
  <script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
  <div>Try scrolling the iframe.</div>
  <p>Paragraph - <span>Scroll happened!</span></p>
<script>
    $("p").clone().appendTo(document.body);
    $("p").clone().appendTo(document.body);
    $("p").clone().appendTo(document.body);
    $(window).scroll(function () { 
      $("span").css("display", "inline").fadeOut("slow"); 
    });

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .scroll() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .scroll()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Navneet

    How can UP/DOWN scroll be distinguished??

  • Iñaki

    Not works for IFRAMES.

  • jellan

    .scroll() does not work on $(‘html, body’).scroll()

    is there any other way to do this? or this just won’t work? pls help..

  • baha
  • newb

    $(window).scroll or $(document).scroll but it doesnt work on ie… grrr

  • newb

    $(window).scroll or $(document).scroll but it doesnt work on ie… grrr

  • Deno

    $(‘html, body’).animate({scrollTop:0}, ‘slow’);

  • Deno

    $(‘html, body’).animate({scrollTop:0}, ‘slow’);

  • Mahesh

    $(window).scroll does work in ie

  • http://upandcrawling.wordpress.com/ Upandcrawling

    You can use the mouse wheel extension to distinguish UP/DOWN scroll easily.

    Here is an easy exemple :
    http://upandcrawling.wordpress.com/2010/06/08/c…

  • huy

    i want scroll to option in select tag. help me

    mình có 1 danh sách như sau

    <input type=”text” name=”tk” id=”tk” value=”">
    <select style=”width: 440px; height: 200px;” id=”tkad” multiple=”multiple” name=”tkad”>
    <option value=”a” class=”a”>a</option>
    <option value=”ai biết” class=”ai biết”>ai biết</option>
    <option value=”anh” class=”anh”>anh</option>
    <option value=”anh em” class=”anh em”>anh em</option>
    <option value=”ao sâu” class=”ao sâu”>ao sâu</option>
    <option value=”áo vải” class=”áo vải”>áo vải</option>
    <option value=”b” class=”b”>b</option>
    <option value=”bảo ban” class=”bảo ban”>bảo ban</option>
    <option value=”bao cát” class=”bao cát”>bao cát</option>
    <option value=”bầu bí” class=”bầu bí”>bầu bí</option>
    <option value=”bò” class=”bò”>bò</option>
    <option value=”bông” class=”bông”>bông</option>
    <option value=”c” class=”c”>c</option>
    <option value=”cần câu” class=”cần câu”>cần câu</option>
    <option value=”cáp điện” class=”cáp điện”>cáp điện</option>
    <option value=”cho thuê kho bãi” class=”cho thuê kho bãi”>cho thuê kho bãi</option>
    <option value=”chúa” class=”chúa”>chúa</option>
    <option value=”chuyển phát” class=”chuyển phát”>chuyển phát</option>
    <option value=”cơm” class=”cơm”>cơm</option>
    <option value=”con cò” class=”con cò”>con cò</option>
    <option value=”d” class=”d”>d</option>
    <option value=”dài dài” class=”dài dài”>dài dài</option>
    <option value=”dạo này” class=”dạo này”>dạo này</option>
    <option value=”dây điện” class=”dây điện”>dây điện</option>
    <option value=”diều” class=”diều”>diều</option>
    <option value=”dung dịch” class=”dung dịch”>dung dịch</option>
    <option value=”e” class=”e”>e</option>
    <option value=”eke” class=”eke”>eke</option>
    <option value=”em út” class=”em út”>em út</option>
    <option value=”giao nhận hàng hóa” class=”giao nhận hàng hóa” >giao nhận hàng hóa</option>
    </select>

  • Comb

    as i test, $(document) & $(window) both can't work on IE

  • Evan

    An iFrame is a totally new window element. You might be able to listen to the iFrame's contentWindow property.

  • Superaktieboy

    Is there any way to complete disable scrolling??

  • Mr.

    The easiest way for me to tell was…

    var tempScrollTop, currentScrollTop = 0;

    $(“#div”).scroll(function(){

    currentScrollTop = $(“#div”).scrollTop();

    if (tempScrollTop currentScrollTop )
    //scrolling up

    tempScrollTop = currentScrollTop;
    }

    This works with mousewheel, up/down arrows, and slidebar

  • Geler

    css : overflow:hidden;

  • Aman

    How do i get the bottom of a scrollpoistion of the iframe which includes the src as a pdf?

  • marangelo

    hi can I ask some questions regarding scrolling down my page.. I would like to point my page in a specific element for example ..
    $(document).ready(funtion() {
    $('#divID').slideDown();
    });

  • http://vynce.myopenid.com/ Vynce

    as far as I can tell, this event is not firing for horizontal scrolling.

  • http://vynce.myopenid.com/ Vynce

    further investigation suggests that .scroll(…) works but that for some reason .live('scroll', …) does not.

  • http://twitter.com/legacye Addy Osmani

    Ariel Flesler created a plugin for doing this very easily a few years ago. It's still quite useful (even for just looking at how to achieve this through jQuery).

    You can find it here: http://flesler.blogspot.com/2007/10/jqueryscrollto.html

  • http://twitter.com/legacye Addy Osmani

    Horizontal scrolling works fine. See here for an example: http://jsbin.com/ocife4/2/edit

  • marangelo

    hi Addy I found the blog and I have a question, Can I specify the Id of the div instead of a class? thanks…

  • Norman

    Thanks Addy. It works brilliantly on JS Bin, but when I copy and paste the EXACT SAME code (inserting the JavaScript obviously) into a local file, it no longer works. (But “$(window).scroll” works fine.) I must be doing something stupid.

  • Norman

    … yep, it was something stupid. Moving the JavaScript from the head into the body fixed it, as did leaving it in the head, but wrapping it in $(document).ready(). Clearly the div must exist before an event handler can be bound to it. Doh.

  • Hector0193

    Well… It can be possible…

  • Mathankumar

    scroll event is firing when scroll is moved however its not fired, when the scroll is clicked.

  • Renato

    How can I intercept the button that triggered the event scroll ?

  • Chicna

    The plugin doesn't work in IE7 (at least) when the html is created after an ajax request, and if the $(…).scrollTo(…) is executed after this request.

    i'm trying to fix it. But if you have solutions, i'm interested :P

  • Chicna

    i just succeed to fix the bug of the plugin scrollTo.
    Just add this after the line 124 (in the beginning of the swith-case, case 'number' or 'string') :

    sharpPos = targ.indexOf('#', 0);
    if(sharpPos != 0 && sharpPos != -1) {
    targExcess = targ.substring(0, sharpPos);
    targ = targ.substring(targExcess.length, targ.length);
    }

    Don't forget the declar the vars 'sharpPos' and 'targExcess' before the switch-case.

    Source of the bug :
    if you do => $.scrollTo($([...]).attr('href'), { duration: 500, easing: 'easeOutExpo' });
    the href can be transformed to a full url when the html is inserted with a request.

  • Zach

    Does attaching .live() to the event work?

  • Zach

    Does attaching .live() to the event work?

  • Jordang

    I can't even get the following code to fire an alert on scroll…I have the latest v installed.

    $(window).scroll(function() {
    alert('scrolled');
    …;
    });

    The best part is this code does work:

    $(window).resize(function() {
    alert('scrolled');
    …;
    });