OpenJS Foundation - openjsf.org · api.jquery.com

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

.on( "scroll" [, eventData ], handler )Returns: jQuery

Description: Bind an event handler to the "scroll" event.

  • version added: 1.7.on( "scroll" [, eventData ], handler )

    • "scroll"

      The string "scroll".

    • eventData

      An object containing data that will be passed to the event handler.

    • handler

      A function to execute each time the event is triggered.

This page describes the scroll event. For the deprecated .scroll() method, see .scroll().

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<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:

Figure 1 - Illustration of the rendered HTML

The scroll event handler can be bound to this element:

1

2

3

$( "#target" ).on( "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, use .trigger( "scroll" ):

1

2

3

$( "#other" ).on( "click", function() {

$( "#target" ).trigger( "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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>on demo</title>

<style>

div {

color: blue;

}

p {

color: green;

}

span {

color: red;

display: none;

}

</style>

<script src="https://code.jquery.com/jquery-4.0.0.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 ).on( "scroll", function() {

$( "span" ).css( "display", "inline" ).fadeOut( "slow" );

} );

</script>

</body>

</html>

Demo:

Read the original on api.jquery.com ↗