11# jquery-scroll-direction-plugin
22A lightweight jQuery plugin to detect scroll direction.
33
4+ ** [ View the Demo on CodePen &rarr ; ] ( https://codepen.io/phucbui/pen/yLaeqBw ) **
5+
46## Getting started
57
68### 1. Include Scroll Direction Plugin to your site.
@@ -15,4 +17,93 @@ You can [download the plugin directly from Github](https://raw.githubusercontent
1517
1618** CDN** [ ![ ] ( https://data.jsdelivr.com/v1/package/gh/phucbm/jquery-scroll-direction-plugin/badge )] ( https://www.jsdelivr.com/package/gh/phucbm/jquery-scroll-direction-plugin )
1719
18- You can also browse for the latest version by visiting [ Scroll Direction Plugin on jsDelivr] ( https://cdn.jsdelivr.net/gh/phucbm/jquery-scroll-direction-plugin/ )
20+ You can also browse for the latest version by visiting [ Scroll Direction Plugin on jsDelivr] ( https://cdn.jsdelivr.net/gh/phucbm/jquery-scroll-direction-plugin/ )
21+
22+ ``` html
23+ <!-- Scroll Direction Plugin - v1.0.0 -->
24+ <script src =" https://cdn.jsdelivr.net/gh/phucbm/jquery-scroll-direction-plugin@1.0.0/jquery.scroll-direction.js" ></script >
25+
26+ <!-- Scroll Direction Plugin - v1.0.0 - minified -->
27+ <script src =" https://cdn.jsdelivr.net/gh/phucbm/jquery-scroll-direction-plugin@1.0.0/jquery.scroll-direction.min.js" ></script >
28+ ```
29+
30+ ### 2. Initialize Scroll Direction
31+
32+ You have to init the plugin before do anything else.
33+
34+ ``` js
35+ // init Scroll Direction
36+ $ .scrollDirection .init ();
37+ ```
38+
39+ ## Usage
40+
41+ ### 1. Methods
42+
43+ #### $.scrollDirection.init()
44+
45+ ``` js
46+ // init Scroll Direction with full settings
47+ $ .scrollDirection .init ({
48+ // Offset
49+ topOffset: 0 , // Integer. Height of top zone in pixel.
50+ bottomOffset: 0 , // Integer. Height of bottom zone in pixel.
51+ atBottomIsAtMiddle: true , // Boolean. By default, consider bottom zone is also middle zone.
52+
53+ // Add class to indicate the scroll direction
54+ indicator: true , // Boolean. Turn indicator on/off.
55+ indicatorElement: $ (" body" ), // jQuery element. Add all indicator classes to this element.
56+ scrollUpClass: " scroll-up" , // scrolling up
57+ scrollDownClass: " scroll-down" , // scrolling down
58+ scrollAtTopClass: " scroll-top" , // at top zone
59+ scrollAtMiddleClass: " scroll-middle" , // at middle zone
60+ scrollAtBottomClass: " scroll-bottom" , // at bottom zone
61+
62+ // Add a class to indicatorElement when scroll pass the element
63+ extraIndicators: {
64+ " element" : $ (" #your-element" ), // jQuery element
65+ " class" : " scroll-pass-element" , // String.
66+ }
67+ });
68+ ```
69+
70+ ### 2. Events
71+
72+ ``` js
73+ // this event runs whenever you load, resize and scroll
74+ $ (window ).on (" scrollDirection" , function () {
75+ // do your job here
76+ });
77+
78+ // when you scroll up
79+ $ (window ).on (" scrollUp" , function () {});
80+
81+ // when you scroll down
82+ $ (window ).on (" scrollDown" , function () {});
83+
84+ // when you at the beginning of the page, you can increase the top zone using topOffset
85+ $ (window ).on (" scrollAtTop" , function () {});
86+
87+ // when you in the middle of the page
88+ // this means the top and bottom zone are not visible in view port
89+ $ (window ).on (" scrollAtMiddle" , function () {});
90+
91+ // when you touch the end of the page
92+ $ (window ).on (" scrollAtBottom" , function () {});
93+ ```
94+
95+ ### 3. APIs
96+
97+ You can also check the current scroll direction/position using these provided APIs.
98+
99+ - ` $.scrollDirection.isScrollUp `
100+ - ` $.scrollDirection.isScrollDown `
101+ - ` $.scrollDirection.isScrollAtTop `
102+ - ` $.scrollDirection.isScrollAtMiddle `
103+ - ` $.scrollDirection.isScrollAtBottom `
104+
105+ ``` js
106+ if ($ .scrollDirection .isScrollUp ){
107+ // do something
108+ }
109+ ```
0 commit comments