无缝轮播
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图</title>
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
.flex{
display: flex;
}
.com{
width: 400px;
height: 200px;
margin: 100px auto;
position: relative;
border: 1px solid black;
overflow: hidden;
}
.sliderPage{
height: 100%;
position: absolute;
top: 0;
left: -400px;
transition: all .3s;
}
.sliderPage li,
.sliderPage img{
width: 400px;
height: 100%;
}
.cut{
position: absolute;
top: 50%;
left: 0;
height: 0;
width: 100%;
justify-content: space-between;
}
.cut div{
height: 40px;
width: 30px;
line-height: 40px;
text-align: center;
margin-top: -20px;
color: white;
background-color: rgba(0, 0, 0, .4);
cursor: pointer;
}
.dots{
position: absolute;
bottom: 10px;
left: 0;
width: 100%;
justify-content: center;
}
.dots li{
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background-color: gray;
cursor: pointer;
}
.dots li.dotsAct{
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="com">
<ul class="sliderPage flex">
<!-- 最后一张图片 -->
<li><img src="./img/cat4.jpg" alt=""></li>
<!-- 真正的图片列表 -->
<li><img src="./img/cat1.jpg" alt=""></li>
<li><img src="./img/cat2.jpg" alt=""></li>
<li><img src="./img/cat3.jpg" alt=""></li>
<li><img src="./img/cat4.jpg" alt=""></li>
<!-- 第一张图片 -->
<li><img src="./img/cat1.jpg" alt=""></li>
</ul>
<div class="cut flex">
<div class="prev"><</div>
<div class="next">></div>
</div>
<ul class="dots flex">
<li data-index='1' class="dotsAct"></li>
<li data-index='2' class=""></li>
<li data-index='3' class=""></li>
<li data-index='4' class=""></li>
</ul>
</div>
<script>
let sliderPage = document.querySelector('.sliderPage')
prev = document.querySelector('.prev')
next = document.querySelector('.next')
dots = document.querySelector('.dots')
timer = null
index = 1
initdirection = 'right'
direction = 'right'
isMove = true
len = sliderPage.children.length - 1
moveWidth = sliderPage.children[0].clientWidth
autoPlay()
function autoPlay(){
timer = setInterval(()=>{
direction = initdirection
direction === 'right' ? index++ : index--
autoMove()
}, 1000);
}
function autoMove(){
isMove = false
let move = index * moveWidth
sliderPage.style.transition = 'all .3s'
sliderPage.style.left = -move + 'px'
dotsHandle(index)
}
function dotsHandle(i){
let ls = i-1
let dlen = dots.children.length - 1
ls = ls < 0 ? dlen : ls
ls = ls > dlen ? 0 : ls
Object.values(dots.children).forEach((el, index) => {
el.classList.remove('dotsAct')
if(ls === index){
el.classList.add('dotsAct')
}
});
}
sliderPage.addEventListener('transitionend',function(){
console.log(index)
if(index >= len && direction == 'right'){
sliderPage.style.transition = 'none'
sliderPage.style.left = -moveWidth+'px'
index = 1
}
if(index <= 0 && direction == 'left'){
sliderPage.style.transition = 'none'
sliderPage.style.left = -moveWidth*(len-1) +'px'
index = len-1
}
isMove = true
})
dots.onclick = function(e){
let target = e.target
if(target.nodeName == 'LI'){
clearInterval(timer)
index = target.dataset.index
autoMove()
autoPlay()
}
}
prev.onclick = function(){
if(!isMove) return
clearInterval(timer)
direction = 'left'
index--
autoMove()
autoPlay()
}
next.onclick = function(){
if(!isMove) return
clearInterval(timer)
direction = 'right'
index++
autoMove()
autoPlay()
}
</script>
</body>
</html>
原型方法封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
list-style: none;
}
.flex {
display: flex;
}
.div {
width: 400px;
height: 200px;
margin: 100px auto;
position: relative;
border: 1px solid black;
overflow: hidden;
}
.sliderPage {
height: 100%;
position: absolute;
top: 0;
left: -400px;
transition: all .3s;
}
.sliderPage li,
.sliderPage img {
width: 400px;
height: 100%;
}
.cut {
position: absolute;
top: 50%;
left: 0;
height: 0;
width: 100%;
justify-content: space-between;
}
.cut div {
height: 40px;
width: 30px;
line-height: 40px;
text-align: center;
margin-top: -20px;
color: white;
background-color: rgba(0, 0, 0, .4);
cursor: pointer;
}
.dots {
position: absolute;
bottom: 10px;
left: 0;
width: 100%;
justify-content: center;
}
.dots li {
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background-color: gray;
cursor: pointer;
}
.dots li.dotsAct {
background-color: aquamarine;
}
</style>
</head>
<body>
<div class="div"></div>
<script>
HTMLDivElement.prototype.createTurnPage = function (option) {
const {
imgArr,
isDots,
isCut
} = Object.assign({
imgArr: [],
isDots: true,
isCut: true,
}, option)
const leng = imgArr.length
const imgCon = creataEl('ul',['sliderPage','flex'])
let lisArr = [imgArr[imgArr.length-1],...imgArr,imgArr[0]]
lisArr.forEach(item=>{
let li = creataEl('li')
li.appendChild(creataEl('img',null,null,{src:item}))
imgCon.appendChild(li)
})
this.appendChild(imgCon)
const cut = creataEl('div',['cut','flex'])
const prev = creataEl('div',['prev'])
const next = creataEl('div',['next'])
prev.innerHTML = '<'
next.innerHTML = '>'
cut.appendChild(prev)
cut.appendChild(next)
if(isCut) this.appendChild(cut)
const dots = creataEl('ul',['dots','flex'])
if(isDots){
imgArr.forEach((s,i)=>{
let classList = []
if(i == 0) classList.push('dotsAct')
dots.appendChild(creataEl('li',classList,null,{'data-index':i+1}))
})
this.appendChild(dots)
}
turnPage(imgCon, prev, next, dots)
function turnPage(sliderPage, prev, next, dots){
let timer = null
index = 1
initdirection = 'right'
direction = 'right'
isMove = true
len = sliderPage.children.length - 1
moveWidth = sliderPage.children[0].clientWidth
autoPlay()
function autoPlay(){
timer = setInterval(()=>{
direction = initdirection
direction === 'right' ? index++ : index--
autoMove()
}, 1000);
}
function autoMove(){
isMove = false
let move = index * moveWidth
sliderPage.style.transition = 'all .3s'
sliderPage.style.left = -move + 'px'
dotsHandle(index)
}
function dotsHandle(i){
let ls = i-1
let dlen = dots.children.length - 1
ls = ls < 0 ? dlen : ls
ls = ls > dlen ? 0 : ls
Object.values(dots.children).forEach((el, index) => {
el.classList.remove('dotsAct')
if(ls === index){
el.classList.add('dotsAct')
}
});
}
sliderPage.addEventListener('transitionend',function(){
if(index >= len && direction == 'right'){
sliderPage.style.transition = 'none'
sliderPage.style.left = -moveWidth+'px'
index = 1
}
if(index <= 0 && direction == 'left'){
sliderPage.style.transition = 'none'
sliderPage.style.left = -moveWidth*(len-1) +'px'
index = len-1
}
isMove = true
})
dots.onclick = function(e){
let target = e.target
if(target.nodeName == 'LI'){
clearInterval(timer)
index = target.dataset.index
autoMove()
autoPlay()
}
}
prev.onclick = function(){
if(!isMove) return
clearInterval(timer)
direction = 'left'
index--
autoMove()
autoPlay()
}
next.onclick = function(){
if(!isMove) return
clearInterval(timer)
direction = 'right'
index++
autoMove()
autoPlay()
}
}
function creataEl(nodeName, classList=[], styleObj={}, propObj={}){
const node = document.createElement(nodeName)
node.classList = classList ? classList.join(" ") : ''
styleObj && Object.keys(styleObj).forEach(key => {
let rkey = kaseSmallHump(key)
node.style[rkey] = styleObj[key]
});
propObj && Object.keys(propObj).forEach(key => {
if(key.indexOf('data-') != -1){
let str = key.split('data-')[1]
let rstr = kaseSmallHump(str)
node.dataset[rstr] = propObj[key]
}else{
node[key] = propObj[key]
}
})
return node
}
function kaseSmallHump(str){
return str.replace(/-\w/g,function($){
return $.split('')[1].toLocaleUpperCase()
})
}
}
const div = document.getElementsByClassName('div')[0]
div.createTurnPage({
imgArr: [
"./img/cat1.jpg",
"./img/cat2.jpg",
"./img/cat3.jpg",
"./img/cat4.jpg"
]
})
</script>
</body>
</html>