Skip to content

Commit 2c4ff05

Browse files
committed
Hello World
1 parent 1883df3 commit 2c4ff05

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

store.js

-1
This file was deleted.

swiper/index.html

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Swiper</title>
9+
</head>
10+
<style>
11+
body {
12+
margin: 0;
13+
padding: 0;
14+
overflow: hidden;
15+
}
16+
17+
#container {
18+
width: 100vw;
19+
height: 100vh;
20+
}
21+
22+
section {
23+
width: 100%;
24+
height: 100%;
25+
text-align: center;
26+
font-size: 86px;
27+
line-height: 100vh;
28+
color: white;
29+
}
30+
</style>
31+
32+
<body>
33+
<div id="container">
34+
<section style="background:brown">1</section>
35+
<section style="background:green">2</section>
36+
<section style="background:gray">3</section>
37+
<section style="background:brown">4</section>
38+
<section style="background:lightcoral">5</section>
39+
<section style="background:brown">6</section>
40+
</div>
41+
</body>
42+
<script src="./swiper.js"></script>
43+
<script>
44+
const container = document.getElementById('container')
45+
const swiper = new Swiper(container, {
46+
direction: 'x',
47+
delay: '500'
48+
})
49+
swiper.toLast()
50+
setTimeout(() => {
51+
swiper.to(1)
52+
}, 1500)
53+
</script>
54+
55+
</html>

swiper/swiper.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @method
3+
* @param {function} cb
4+
* @return {array}
5+
*/
6+
NodeList.prototype.filter = function (cb) {
7+
8+
if (typeof cb === 'function') {
9+
let res = []
10+
for (let i = 0; i < this.length; i++) {
11+
if (cb(this[i])) {
12+
res.push(this[i])
13+
}
14+
}
15+
return res
16+
}
17+
return this
18+
}
19+
20+
const defaultConfig = {
21+
direction: 'x', // x | y
22+
delay: 500
23+
}
24+
25+
class Swiper {
26+
constructor($container, config) {
27+
// init reference
28+
this.contianer = $container
29+
this.config = Object.assign({}, defaultConfig, config)
30+
this.pages = this.contianer.childNodes.filter(node => {
31+
return node.nodeType === 1
32+
})
33+
// init page index
34+
this.index = 1
35+
this.length = this.pages.length
36+
37+
// intervalID hold
38+
this.interval = null
39+
40+
// init styles
41+
this.contianer.style.cssText += `position: relative; transform: translate3d(0, 0, 0); transition: all ${this.config.delay}ms ease 0s; `
42+
this.pages.forEach((page, idx) => {
43+
page.style.cssText +=
44+
this.config.direction === 'y' ?
45+
`position: absolute; top: ${idx*100}%; left: 0px;` :
46+
`position: absolute; top: 0px; left: ${idx*100}%;`
47+
})
48+
}
49+
50+
to(index) {
51+
// adjust
52+
if (index > this.length) index = this.length
53+
if (index < 1) index = 1
54+
// cancel last slide
55+
if (this.interval) clearInterval(this.interval)
56+
57+
// toNext
58+
if (index > this.index) {
59+
this.interval = setInterval(() => {
60+
if (index > this.index) {
61+
this.contianer.style.cssText +=
62+
this.config.direction === 'y' ?
63+
`transform: translate3d(0px, -${this.index*100}%, 0px);` :
64+
`transform: translate3d(-${this.index*100}%, 0px, 0px);`
65+
this.index++
66+
} else {
67+
clearInterval(this.interval)
68+
}
69+
}, this.config.delay)
70+
}
71+
// toPrevious
72+
else if (index < this.index) {
73+
this.interval = setInterval(() => {
74+
if (index < this.index) {
75+
this.index--
76+
this.contianer.style.cssText +=
77+
this.config.direction === 'y' ?
78+
`transform: translate3d(0px, -${(this.index-1)*100}%, 0px);` :
79+
`transform: translate3d(-${(this.index-1)*100}%, 0px, 0px);`
80+
} else {
81+
clearInterval(this.interval)
82+
}
83+
}, this.config.delay)
84+
}
85+
}
86+
87+
toNext() {
88+
this.to(++this.index)
89+
}
90+
91+
toPrevious() {
92+
this.to(--this.index)
93+
}
94+
95+
toFirst() {
96+
this.to(1)
97+
}
98+
99+
toLast() {
100+
this.to(this.length)
101+
}
102+
}
103+
104+
exports = Swiper

0 commit comments

Comments
 (0)