@jocampo3/slider 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # slider-js
package/index.js ADDED
@@ -0,0 +1,51 @@
1
+ export default class Slider {
2
+ constructor(slider, interval) {
3
+ this.slider = slider;
4
+ this.slides = this.slider.children;
5
+ this.index = 0;
6
+ this.interval = interval;
7
+ this.timer = null;
8
+ this.restartTimer();
9
+ this.addListeners();
10
+ }
11
+
12
+ slideWidth() {
13
+ return this.slider.clientWidth;
14
+ }
15
+
16
+ restartTimer() {
17
+ clearInterval(this.timer);
18
+ this.timer = setInterval(() => this.next(), this.interval);
19
+ }
20
+
21
+ goTo(i) {
22
+ this.index = (i + this.slides.length) % this.slides.length;
23
+ this.slider.scrollTo({ left: this.index * this.slideWidth(), behavior: 'smooth' });
24
+ this.restartTimer();
25
+ }
26
+
27
+ next() {
28
+ this.goTo(this.index + 1);
29
+ }
30
+
31
+ getCurrentIndex() {
32
+ return Math.round(this.slider.scrollLeft / this.slideWidth());
33
+ }
34
+
35
+ addListeners() {
36
+ this.slider.addEventListener('scroll', () => {
37
+ this.index = this.getCurrentIndex();
38
+ })
39
+ this.slider.addEventListener('mouseenter', () => {
40
+ clearInterval(this.timer);
41
+ });
42
+
43
+ this.slider.addEventListener('mouseleave', () => {
44
+ this.restartTimer();
45
+ });
46
+
47
+ window.addEventListener('resize', () => {
48
+ this.goTo(this.index);
49
+ });
50
+ }
51
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@jocampo3/slider",
3
+ "version": "1.0.1",
4
+ "description": "Simple, reusable content carousel. Built with CSS and minimal JavaScript.",
5
+ "keywords": [
6
+ "slider"
7
+ ],
8
+ "homepage": "https://github.com/jocampo3/slider-js#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/jocampo3/slider-js/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/jocampo3/slider-js.git"
15
+ },
16
+ "license": "ISC",
17
+ "author": "Jose Ocampo",
18
+ "type": "commonjs",
19
+ "main": "index.js",
20
+ "exports": {
21
+ ".": "./index.js",
22
+ "./styles.css": "./styles.css"
23
+ },
24
+ "files": [
25
+ "index.js",
26
+ "styles.css"
27
+ ],
28
+ "scripts": {
29
+ "test": "echo \"Error: no test specified\" && exit 1"
30
+ }
31
+ }
package/styles.css ADDED
@@ -0,0 +1 @@
1
+ .slideshow,.slideshow>* a{width:100%;height:100%;display:flex}a{text-decoration:none}.slideshow{overflow-x:scroll;overflow-y:hidden;scroll-snap-type:x mandatory;overscroll-behavior-x:contain;scrollbar-width:none}.slideshow::-webkit-scrollbar{display:none}.slideshow:hover{scroll-behavior:smooth}.slideshow>*{position:relative;scroll-snap-align:start;flex-shrink:0;width:100%;height:100%}.slideshow>* a{align-items:center;justify-content:center;-webkit-tap-highlight-color:transparent}.slideshow a>*{max-width:100%;max-height:100%;object-fit:contain;z-index:1}.slideshow a:focus-visible{outline:0}.slideshow a:after,.slideshow a:before{z-index:2;content:"";display:block;position:absolute;top:0;bottom:0;width:calc(50% + 4vw)}.slideshow a:before{left:calc(-1 * calc(50% + 4vw))}.slideshow a:after{right:calc(-1 * calc(50% + 4vw))}.slideshow :first-child a:before,.slideshow :last-child a:after{display:none}