Skip to content

Commit 2e56c88

Browse files
committed
fix: fixed sluggish slide-pass event
1 parent 8d928ec commit 2e56c88

24 files changed

+316
-299
lines changed

README.md

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<br>
77
[![npm Version](https://badgen.net/npm/v/scroll-snap-slider)](https://www.npmjs.com/package/scroll-snap-slider)
88
[![Dependency Count: 0](https://badgen.net/bundlephobia/dependency-count/scroll-snap-slider)](https://bundlephobia.com/result?p=scroll-snap-slider)
9-
[![minzippped Size](https://badgen.net/bundlephobia/minzip/scroll-snap-slider)](https://bundlephobia.com/result?p=scroll-snap-slider)
9+
[![minzipped Size](https://badgen.net/bundlephobia/minzip/scroll-snap-slider)](https://bundlephobia.com/result?p=scroll-snap-slider)
1010

1111
Mostly CSS slider with great performance.
1212

@@ -54,9 +54,9 @@ the entrypoint changing from 'ScrollSnapSlider' to 'index'.
5454
| index | 348 B | 143 B |
5555
| ScrollSnapAutoplay | 1479 B | 559 B |
5656
| ScrollSnapDraggable | 2459 B | 772 B |
57-
| ScrollSnapLoop | 1840 B | 623 B |
57+
| ScrollSnapLoop | 1849 B | 615 B |
5858
| ScrollSnapPlugin | 70 B | 110 B |
59-
| ScrollSnapSlider | 2361 B | 811 B |
59+
| ScrollSnapSlider | 2361 B | 809 B |
6060

6161
## Restrictions
6262

@@ -77,9 +77,7 @@ yarn add scroll-snap-slider
7777

7878
## Usage
7979

80-
HTML + CSS are enough for a working slider. You can use IDs and anchor links to create navigation buttons.
81-
82-
The ES6 class provided in this package augments the slider with a few events and methods.
80+
The class provided in this package augments a slider with a few events and methods.
8381

8482
### Markup
8583

@@ -111,7 +109,7 @@ You can add whatever markup inside the slides.
111109

112110
### Additional Styles
113111

114-
Prevents page navigation on horizontal scrolling, i.E. on MacOS.
112+
Prevents page navigation on horizontal scrolling, i.E. on macOS.
115113
[\[Support tables\]](https://caniuse.com/?search=overscroll-behavior)
116114

117115
```css
@@ -138,44 +136,44 @@ events and exposes a few methods, with which you can enhance your slider's behav
138136
**Default behaviour:**
139137

140138
```javascript
141-
import {ScrollSnapSlider} from 'scroll-snap-slider'
139+
import { ScrollSnapSlider } from 'scroll-snap-slider'
142140

143141
const element = document.querySelector('.example-slider')
144-
const slider = new ScrollSnapSlider({element})
142+
const slider = new ScrollSnapSlider({ element })
145143

146144
slider.addEventListener('slide-start', function (event) {
147-
console.info(`Started sliding towards slide ${event.detail}.`)
145+
console.info(`Started sliding towards slide ${event.detail}.`)
148146
})
149147

150148
slider.addEventListener('slide-pass', function (event) {
151-
console.info(`Passing slide ${event.detail}.`)
149+
console.info(`Passing slide ${event.detail}.`)
152150
})
153151

154152
slider.addEventListener('slide-stop', function (event) {
155-
console.info(`Stopped sliding at slide ${event.detail}.`)
153+
console.info(`Stopped sliding at slide ${event.detail}.`)
156154
})
157155
```
158156

159157
**Advanced config:**
160158

161159
```javascript
162-
import {ScrollSnapSlider} from 'scroll-snap-slider'
160+
import { ScrollSnapSlider } from 'scroll-snap-slider'
163161

164162
// Do not automatically attach scroll listener
165163
const slider = new ScrollSnapSlider({
166-
element: document.querySelector('.example-slider'),
167-
scrollTimeout: 50, // Sets a shorter timeout to detect scroll end
168-
roundingMethod: Math.round, // Dispatch 'slide-pass' events around the center of each slide
169-
// roundingMethod: Math.ceil, // Dispatch 'slide-pass' events as soon as the next one is visible
170-
// roundingMethod: Math.floor, // Dispatch 'slide-pass' events only when the next one is fully visible
171-
sizingMethod(slider) {
172-
173-
// with padding
174-
return slider.element.firstElementChild.offsetWidth
175-
176-
// without padding
177-
// return slider.element.firstElementChild.clientWidth
178-
}
164+
element: document.querySelector('.example-slider'),
165+
scrollTimeout: 50, // Sets a shorter timeout to detect scroll end
166+
roundingMethod: Math.round, // Dispatch 'slide-pass' events around the center of each slide
167+
// roundingMethod: Math.ceil, // Dispatch 'slide-pass' events as soon as the next one is visible
168+
// roundingMethod: Math.floor, // Dispatch 'slide-pass' events only when the next one is fully visible
169+
sizingMethod (slider) {
170+
171+
// with padding
172+
return slider.element.firstElementChild.offsetWidth
173+
174+
// without padding
175+
// return slider.element.firstElementChild.clientWidth
176+
}
179177
})
180178
```
181179

@@ -191,14 +189,14 @@ You can add one or multiple of the available Plugins:
191189
Additional Note: The `ScrollSnapDraggable` and `ScrollSnapLoop` **do not** work well together.
192190

193191
```javascript
194-
import {ScrollSnapSlider} from 'scroll-snap-slider/src/ScrollSnapSlider.js'
195-
import {ScrollSnapAutoplay} from 'scroll-snap-slider/src/ScrollSnapAutoplay.js'
196-
import {ScrollSnapLoop} from 'scroll-snap-slider/src/ScrollSnapLoop.js'
192+
import { ScrollSnapSlider } from 'scroll-snap-slider/src/ScrollSnapSlider.js'
193+
import { ScrollSnapAutoplay } from 'scroll-snap-slider/src/ScrollSnapAutoplay.js'
194+
import { ScrollSnapLoop } from 'scroll-snap-slider/src/ScrollSnapLoop.js'
197195

198196
const element = document.querySelector('.example-slider')
199-
const slider = new ScrollSnapSlider({element}).with([
200-
new ScrollSnapAutoplay(1200),
201-
new ScrollSnapLoop
197+
const slider = new ScrollSnapSlider({ element }).with([
198+
new ScrollSnapAutoplay(1200),
199+
new ScrollSnapLoop
202200
])
203201
```
204202

@@ -207,48 +205,48 @@ Creating your own plugin:
207205
```javascript
208206
export class CustomPlugin extends ScrollSnapPlugin {
209207

210-
/**
211-
* Pass any config here
212-
* @param {*} config
213-
*/
214-
constructor(config) {
215-
super()
216-
217-
this.config = config
218-
}
219-
220-
/**
221-
* Chose a unique plugin name. If you need multiple instances of the same plugin on a slider, each must return a unique id.
222-
* @return {String}
223-
*/
224-
get id() {
225-
return 'lubba-wubba-dub-dub'
226-
}
227-
228-
/**
229-
* Attach listeners, fetch DOM things, save reference to the slider
230-
* @param {ScrollSnapSlider} slider
231-
* @override
232-
*/
233-
enable(slider) {
234-
// TODO method stub
235-
}
236-
237-
/**
238-
* Free resources, remove listeners, ...
239-
* @override
240-
*/
241-
disable() {
242-
// TODO method stub
243-
}
208+
/**
209+
* Pass any config here
210+
* @param {*} config
211+
*/
212+
constructor (config) {
213+
super()
214+
215+
this.config = config
216+
}
217+
218+
/**
219+
* Chose a unique plugin name. If you need multiple instances of the same plugin on a slider, each must return a unique id.
220+
* @return {String}
221+
*/
222+
get id () {
223+
return 'lubba-wubba-dub-dub'
224+
}
225+
226+
/**
227+
* Attach listeners, fetch DOM things, save reference to the slider
228+
* @param {ScrollSnapSlider} slider
229+
* @override
230+
*/
231+
enable (slider) {
232+
// TODO method stub
233+
}
234+
235+
/**
236+
* Free resources, remove listeners, ...
237+
* @override
238+
*/
239+
disable () {
240+
// TODO method stub
241+
}
244242
}
245243
```
246244

247245
## API
248246

249247
| Method | Description |
250248
|--------------------------------|-----------------------------------------------------------------------------|
251-
| `slideTo(index: Number): void` | Scrolls to slide with at `index`. |
249+
| `slideTo(index: Number): void` | Scrolls to slide at `index`. |
252250
| `addEventListener(...)` | This is a shortcut for `slider.element.addEventListener(...)`. |
253251
| `removeEventListener(...)` | This is a shortcut for `slider.element.removeEventListener(...)`. |
254252
| `attachEventListeners()` | Enables the JS behaviour of this plugin. This is called in the constructor. |

demo/slider-simple.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
ScrollSnapAutoplay,
3-
ScrollSnapDraggable,
4-
ScrollSnapLoop,
5-
ScrollSnapSlider
2+
ScrollSnapAutoplay,
3+
ScrollSnapDraggable,
4+
ScrollSnapLoop,
5+
ScrollSnapSlider
66
} from '../dist/scroll-snap-slider.mjs'
77

88
const sliderSimpleElement = document.querySelector('.scroll-snap-slider.-simple')
@@ -25,6 +25,7 @@ const prev = document.querySelector('.indicators.-simple .arrow.-prev')
2525
const next = document.querySelector('.indicators.-simple .arrow.-next')
2626

2727
const setSelected = function (event) {
28+
console.info(event)
2829
const slideElementIndex = event.detail
2930
const slideElement = slides[slideElementIndex]
3031

dist/scroll-snap-slider.iife.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ var ScrollSnapSlider = function(exports) {
268268
this.slider.element.style.scrollSnapStop = "";
269269
this.slider.element.style.scrollSnapType = "";
270270
this.slider.attachListeners();
271-
setTimeout(this.slider.update, 0);
271+
requestAnimationFrame(this.slider.update);
272272
}
273273
/**
274274
* Move last slide to the start of the slider.
@@ -452,18 +452,18 @@ var ScrollSnapSlider = function(exports) {
452452
* Updates the computed values
453453
*/
454454
update = () => {
455-
requestAnimationFrame(() => {
456-
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
457-
this.slideScrollLeft = this.slide * this.itemSize;
458-
});
455+
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
456+
this.slideScrollLeft = this.slide * this.itemSize;
459457
};
460458
/**
461459
* Calculate all necessary things and dispatch an event when sliding stops
462460
*/
463461
onScrollEnd = () => {
464-
this.scrollTimeoutId = null;
465-
this.update();
466-
this.dispatch("slide-stop", this.slide);
462+
requestAnimationFrame(() => {
463+
this.scrollTimeoutId = null;
464+
this.update();
465+
this.dispatch("slide-stop", this.slide);
466+
});
467467
};
468468
/**
469469
* This will recompute the <code>itemSize</code>

dist/scroll-snap-slider.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class ScrollSnapLoop extends ScrollSnapPlugin {
268268
this.slider.element.style.scrollSnapStop = "";
269269
this.slider.element.style.scrollSnapType = "";
270270
this.slider.attachListeners();
271-
setTimeout(this.slider.update, 0);
271+
requestAnimationFrame(this.slider.update);
272272
}
273273
/**
274274
* Move last slide to the start of the slider.
@@ -452,18 +452,18 @@ class ScrollSnapSlider {
452452
* Updates the computed values
453453
*/
454454
update = () => {
455-
requestAnimationFrame(() => {
456-
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
457-
this.slideScrollLeft = this.slide * this.itemSize;
458-
});
455+
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
456+
this.slideScrollLeft = this.slide * this.itemSize;
459457
};
460458
/**
461459
* Calculate all necessary things and dispatch an event when sliding stops
462460
*/
463461
onScrollEnd = () => {
464-
this.scrollTimeoutId = null;
465-
this.update();
466-
this.dispatch("slide-stop", this.slide);
462+
requestAnimationFrame(() => {
463+
this.scrollTimeoutId = null;
464+
this.update();
465+
this.dispatch("slide-stop", this.slide);
466+
});
467467
};
468468
/**
469469
* This will recompute the <code>itemSize</code>

dist/scroll-snap-slider.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class ScrollSnapLoop extends ScrollSnapPlugin {
266266
this.slider.element.style.scrollSnapStop = "";
267267
this.slider.element.style.scrollSnapType = "";
268268
this.slider.attachListeners();
269-
setTimeout(this.slider.update, 0);
269+
requestAnimationFrame(this.slider.update);
270270
}
271271
/**
272272
* Move last slide to the start of the slider.
@@ -450,18 +450,18 @@ class ScrollSnapSlider {
450450
* Updates the computed values
451451
*/
452452
update = () => {
453-
requestAnimationFrame(() => {
454-
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
455-
this.slideScrollLeft = this.slide * this.itemSize;
456-
});
453+
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
454+
this.slideScrollLeft = this.slide * this.itemSize;
457455
};
458456
/**
459457
* Calculate all necessary things and dispatch an event when sliding stops
460458
*/
461459
onScrollEnd = () => {
462-
this.scrollTimeoutId = null;
463-
this.update();
464-
this.dispatch("slide-stop", this.slide);
460+
requestAnimationFrame(() => {
461+
this.scrollTimeoutId = null;
462+
this.update();
463+
this.dispatch("slide-stop", this.slide);
464+
});
465465
};
466466
/**
467467
* This will recompute the <code>itemSize</code>

dist/scroll-snap-slider.umd.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
this.slider.element.style.scrollSnapStop = "";
271271
this.slider.element.style.scrollSnapType = "";
272272
this.slider.attachListeners();
273-
setTimeout(this.slider.update, 0);
273+
requestAnimationFrame(this.slider.update);
274274
}
275275
/**
276276
* Move last slide to the start of the slider.
@@ -454,18 +454,18 @@
454454
* Updates the computed values
455455
*/
456456
update = () => {
457-
requestAnimationFrame(() => {
458-
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
459-
this.slideScrollLeft = this.slide * this.itemSize;
460-
});
457+
this.slide = this.roundingMethod(this.element.scrollLeft / this.itemSize);
458+
this.slideScrollLeft = this.slide * this.itemSize;
461459
};
462460
/**
463461
* Calculate all necessary things and dispatch an event when sliding stops
464462
*/
465463
onScrollEnd = () => {
466-
this.scrollTimeoutId = null;
467-
this.update();
468-
this.dispatch("slide-stop", this.slide);
464+
requestAnimationFrame(() => {
465+
this.scrollTimeoutId = null;
466+
this.update();
467+
this.dispatch("slide-stop", this.slide);
468+
});
469469
};
470470
/**
471471
* This will recompute the <code>itemSize</code>

docs/assets/main.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)