slide-所有功能
by Doris18
HTML
<div class="slider">
<div id="m-slider">
<div class="slide"> </div>
<div class="slide"> </div>
<div class="slide"> </div>
</div>
<ul class="m-cursors">
<li class="cursor"></li>
<li class="cursor active"></li>
<li class="cursor"></li>
<li class="cursor"></li>
<li class="cursor"></li>
</ul>
<div class="control">
<div id="prev" class="prev move"></div>
<div id="next" class="next move"></div>
</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
ul,ol,dl {
list-style: none;
}
.slider{
/* border: 1px solid green; */
height: 200px;
width: 400px;
position: relative;
}
#m-slider {
position: relative;
width: 100%;
height: 100%;
transform: translateX(-100%);
transition: transform .5s ease-out;
}
.slide {
position: absolute;
}
#m-slider > .slide > img {
width: 300px;
/* opacity: .2; */
}
.slide:nth-child(1) {
left: 0%;
}
.slide:nth-child(2) {
left: 100%;
}
.slide:nth-child(3) {
left: 200%;
}
/* cursors */
.control {
/* */
}
.move {
position: absolute;
width: 30px;
height: 30px;
top: 50%;
background-color: RGBA(255,255,255,0.8);
text-align: center;
line-height: 30px;
font-size: 26px;
color:#999;
}
.move:hover {
opacity: .5;
}
.prev {
left: 0;
}
.prev::before {
content:'<'
}
.next {
left: 68%;
}
.next::before {
content:'>'
}
ul.m-cursors {
position: absolute;
bottom: 0;
left: 20%;
}
ul > li.cursor {
float: left;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #fff;
margin-right: 10px;
}
ul > li.active {
background-color: #f00;
}
Babel + JSX
// Component
class Slider {
constructor(el) {
this.container = document.getElementById(el);// slide容器
this.slides = this.container.querySelectorAll('.slide');// 所有slide item
this.prevBtn = document.getElementById('prev');
this.nextBtn = document.getElementById('next');
this.cursors = document.querySelectorAll('.cursor')
this.offset = 1;// 偏移量,即个数
this.slideIndex = 1;// slide index one of 0, 1, 2
this.pageIndex = 1;// page index of total pictures
this.pageNum = 5;// total pictures
}
bindEvents() {
this.nextBtn.addEventListener( 'click',() => {
this.go(1)
})
this.prevBtn.addEventListener( 'click', () => {
this.go(-1)
})
}
// nav to index
nav(index) {
this.pageIndex = index;
this.calcDistance();
}
// 标准化index,使index始终为0,1,2中的一个
normalIdx(index,len) {
return (index + len) % len;
}
// slide move according to the flag
go(flag) {
// 1. offset and slideIndex change
this.offset += flag;
this.pageIndex += flag;
const slideIndex = this.slideIndex = this.normalIdx( this.slideIndex += flag,3);
// 计算移动距离
this.calcDistance();
}
// style change
calcDistance() {
const offset = this.offset;
const slideIndex = this.slideIndex;
// 1. relevant index
const prevIndex = this.normalIdx( slideIndex - 1, 3)
const nextIndex = this.normalIdx( slideIndex + 1, 3)
this.pageIndex = this.normalIdx(this.pageIndex,this.pageNum)
// 2. container and slide move
this.container.style.transform = `translateX(${-100 * (offset)}%) `;
this.slides[prevIndex].style.left = `${100 * (offset - 1)}%`;
this.slides[slideIndex].style.left = `${100 * offset}%`;
this.slides[nextIndex].style.left = `${100 * (offset + 1)}%`;
// 3. cursor style
const cursors = Array.from(this.cursors);
cursors.forEach(function(cursor,index) {
let cursorClass = cursor.classList;
if(cursorClass.contains('active')) {
...