Code has been added to clipboard!

jQuery .scroll(): Assigning a Handler for a scroll Event

Reading time 2 min
Published Jan 10, 2018
Updated Oct 17, 2019

jQuery scroll: Main Tips

  • The .scroll() jQuery method is closely related to the scroll event.
  • In its simplest form, it can trigger said event.
  • By adding arguments, you can also make jQuery .scroll() attach an event handler to the scroll event.
  • To detach the handler, use .off() with the event parameter defined as scroll.

Usage of jQuery .scroll()

The jQuery .scroll() method either triggers a scroll event manually, or attaches an event handler that executes a function when one occurs. In the example below, you can see the total number of times you scrolled constantly updated and displayed below the <div> element:

A jQuery scroll event occurs each time a change in the elements scroll position is detected. It can be caused not only by literal scrolling with the scroll wheel on your mouse but also by pressing the arrow keys on your keyboard, as well as clicking or dragging the scroll bar with the cursor of the mouse.

Syntax for jQuery .scroll()

The syntax required to trigger the jQuery scroll event manually is very simple, as there are no arguments you should define:

$("selector").scroll();

You will, however, need to specify a few arguments for .scroll() in jQuery to attach an event handler:

$("selector").scroll(data, callback);

The only required argument is callback: it represents the callback function you wish to set as an event handler. If your function needs some additional data to run, include it in the brackets before the function.

Note: you can use jQuery .scroll() with window objects, scrollable frames, and elements that have their CSS overflow property set to scroll.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX