Skip to content

Commit b85f8a6

Browse files
committed
♻️ add native JS events
1 parent 0ae9c24 commit b85f8a6

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

index.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ <h1>jQuery Scroll Direction Plugin</h1>
139139
<script src="jquery.scroll-direction.js"></script>
140140
<script>
141141
// init
142-
$.scrollDirection.init({
142+
window.scrollDirection.init({
143143
topOffset: () => $("header").height(),
144144
bottomOffset: () => $("footer").height()
145145
});
@@ -165,18 +165,17 @@ <h1>jQuery Scroll Direction Plugin</h1>
165165
}
166166

167167
// testing
168-
$(window).on("scrollDirection", function(){
168+
document.addEventListener("scrollDirection", () => {
169169
// see values update at the bottom left of the page
170-
log("isScrollingUp", $.scrollDirection.isScrollUp);
171-
log("isScrollingDown", $.scrollDirection.isScrollDown, true);
172-
log("isScrollAtTop", $.scrollDirection.isScrollAtTop);
173-
log("isScrollAtMiddle", $.scrollDirection.isScrollAtMiddle);
174-
log("isScrollAtBottom", $.scrollDirection.isScrollAtBottom);
170+
log("isScrollingUp", window.scrollDirection.isScrollUp);
171+
log("isScrollingDown", window.scrollDirection.isScrollDown, true);
172+
log("isScrollAtTop", window.scrollDirection.isScrollAtTop);
173+
log("isScrollAtMiddle", window.scrollDirection.isScrollAtMiddle);
174+
log("isScrollAtBottom", window.scrollDirection.isScrollAtBottom);
175175

176176
// open console to see body class
177177
console.log($("body").attr("class"));
178178
});
179-
180179
</script>
181180

182181
</body>

jquery.scroll-direction.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
let obj = {},
1313
pluginActive = false,
14-
$w = $(window),
14+
hasJquery = typeof jQuery !== 'undefined',
15+
$w = hasJquery ? $(window) : '',
1516
settings = {
1617
topOffset: () => 0,
1718
bottomOffset: () => 0,
@@ -54,12 +55,24 @@
5455
obj.isScrollAtBottom = false;
5556

5657
// Events
57-
let scrollDirection = $.Event("scrollDirection"),
58-
scrollDown = $.Event("scrollDown"),
59-
scrollUp = $.Event("scrollUp"),
60-
scrollAtTop = $.Event("scrollAtTop"),
61-
scrollAtMiddle = $.Event("scrollAtMiddle"),
62-
scrollAtBottom = $.Event("scrollAtBottom");
58+
const scrollDirection = new Event("scrollDirection"),
59+
scrollDown = new Event("scrollDown"),
60+
scrollUp = new Event("scrollUp"),
61+
scrollAtTop = new Event("scrollAtTop"),
62+
scrollAtMiddle = new Event("scrollAtMiddle"),
63+
scrollAtBottom = new Event("scrollAtBottom");
64+
65+
// jQuery support
66+
let scrollDirectionJquery, scrollDownJquery, scrollUpJquery, scrollAtTopJquery, scrollAtMiddleJquery,
67+
scrollAtBottomJquery;
68+
if(hasJquery){
69+
scrollDirectionJquery = $.Event("scrollDirection");
70+
scrollDownJquery = $.Event("scrollDown");
71+
scrollUpJquery = $.Event("scrollUp");
72+
scrollAtTopJquery = $.Event("scrollAtTop");
73+
scrollAtMiddleJquery = $.Event("scrollAtMiddle");
74+
scrollAtBottomJquery = $.Event("scrollAtBottom");
75+
}
6376

6477
// Indicator: add class to indicate the scrolling status
6578
function indicator(options){
@@ -104,25 +117,29 @@
104117
obj.isScrollUp = false;
105118
obj.isScrollDown = true;
106119

107-
$w.trigger(scrollDown);
120+
if(hasJquery) $w.trigger(scrollDownJquery);
121+
document.dispatchEvent(scrollDown);
108122
}else if(settings.scrollAmount() < lastScrollAmount && lastScrollAmount >= 0){
109123
// scroll up
110124
obj.isScrollUp = true;
111125
obj.isScrollDown = false;
112126

113-
$w.trigger(scrollUp);
127+
if(hasJquery) $w.trigger(scrollUpJquery);
128+
document.dispatchEvent(scrollUp);
114129
}else if(settings.scrollAmount() < 0){
115130
// scroll up (elastic scroll with negative value)
116131
obj.isScrollUp = true;
117132
obj.isScrollDown = false;
118133

119-
$w.trigger(scrollUp);
134+
if(hasJquery) $w.trigger(scrollUpJquery);
135+
document.dispatchEvent(scrollUp);
120136
}else if(settings.scrollAmount() > settings.maxScrollAmount()){
121137
// scroll down (elastic scroll with positive value)
122138
obj.isScrollUp = false;
123139
obj.isScrollDown = true;
124140

125-
$w.trigger(scrollDown);
141+
if(hasJquery) $w.trigger(scrollDownJquery);
142+
document.dispatchEvent(scrollDown);
126143
}
127144

128145
// update the last position
@@ -135,7 +152,8 @@
135152
obj.isScrollAtMiddle = false;
136153
obj.isScrollAtBottom = false;
137154

138-
$w.trigger(scrollAtTop);
155+
if(hasJquery) $w.trigger(scrollAtTopJquery);
156+
document.dispatchEvent(scrollAtTop);
139157
}else if(
140158
settings.scrollAmount() >= settings.maxScrollAmount() - settings.bottomOffset() &&
141159
settings.scrollAmount() <= settings.maxScrollAmount()
@@ -145,19 +163,23 @@
145163
obj.isScrollAtMiddle = false;
146164
obj.isScrollAtBottom = true;
147165

148-
$w.trigger(scrollAtBottom);
166+
if(hasJquery) $w.trigger(scrollAtBottomJquery);
167+
document.dispatchEvent(scrollAtBottom);
149168

150169
if(settings.atBottomIsAtMiddle){
151170
obj.isScrollAtMiddle = true;
152-
$w.trigger(scrollAtMiddle);
171+
172+
if(hasJquery) $w.trigger(scrollAtMiddleJquery);
173+
document.dispatchEvent(scrollAtMiddle);
153174
}
154175
}else{
155176
// at middle
156177
obj.isScrollAtTop = false;
157178
obj.isScrollAtMiddle = true;
158179
obj.isScrollAtBottom = false;
159180

160-
$w.trigger(scrollAtMiddle);
181+
if(hasJquery) $w.trigger(scrollAtMiddleJquery);
182+
document.dispatchEvent(scrollAtMiddle);
161183
}
162184

163185
// Indicator
@@ -173,7 +195,8 @@
173195
});
174196
}
175197

176-
$w.trigger(scrollDirection);
198+
if(hasJquery) $w.trigger(scrollDirectionJquery);
199+
document.dispatchEvent(scrollDirection);
177200
}
178201
}
179202

@@ -188,7 +211,7 @@
188211
window.scrollDirection = obj;
189212

190213
// Only assign to jQuery.scrollDirection if jQuery is loaded
191-
if(jQuery){
214+
if(hasJquery){
192215
jQuery.scrollDirection = window.scrollDirection;
193216
}
194217

0 commit comments

Comments
 (0)