.scroll()

Syntax and Description


.scroll(handler)            
.scroll()

handler is a function to execute each time the event is triggered. Return value is the jQuery object, for chaining purposes.

scroll method binds an event handler to the scroll JavaScript event, or trigger that event on an element.

.scroll(handler) is a shortcut for .bind('scroll', handler). .scroll() is a shortcut for and .trigger('scroll').

scroll(fn) event fires when a document view is scrolled.

We can trigger the event manually when another element is clicked.


$('#other').click(function() {
// w  w  w .  j av  a 2s . co m
 $('#target').scroll();

});

Listen to scroll event


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--   w ww  .jav  a  2 s .  co  m-->
            $(window).scroll(function () { 
              $("span").css("display", "inline").fadeOut("slow"); 
            });
        });
    </script>
  </head>
  <body>
    <body>
         <span>Scroll happened! java2s.com</span>
    </body>
</html>

Click to view the demo

The code above generates the following result.

.scroll()
Home »
  Javascript Tutorial »
    jQuery Reference »
      Event
Javascript Tutorial Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()