首先是正常的开头和样式设置:
```
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.wrapper{
margin: 0 auto;
width: 600px;
height: 250px;
overflow: hidden;
position: relative;
}
ul{
list-style: none;
width: 2400px;
position: absolute;
top: 0;
left: 0;
}
ul::after{
content: '';
display: block;
height: 0;
visibility: hidden;
clear: both;
}
li{
float: left;
width: 600px;
}
img{
width: 600px;
height: 250px;
display: block;
}
p {
width: 100%;
height: 10px;
position: absolute;
bottom: 5px;
left: 0;
text-align: center;
z-index: 100;
}
p span {
display: inline-block;
width: 10px;
height: 10px;
background-color: black;
border-radius: 50%;
vertical-align: top;
cursor: pointer;
margin: 0 5px;
}
p span.active {
background-color: red;
}
a {
text-decoration: none;
display: block;
width: 40px;
height: 80px;
color: white;
background: black;
font-size: 30px;
line-height: 80px;
text-align: center;
position: absolute;
z-index: 100;
opacity: .3;
}
#leftBtn,
#rightBtn {
top: 50%;
margin-top: -40px;
}
#leftBtn {
left: 20px;
}
#rightBtn {
right: 20px;
}
</style>
</head>
<body>
设置图片:
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
a href="javascript:;" id="leftBtn"<</a>
a href="javascript:;" id="rightBtn"></a>
</div>
取外置js
<script src="./34_move.js"></script>
<script>
/*
1.先把第一张图片复制出来,放到最后
2.如果实现无缝轮播 要先判断 再递增或者递减
*/
var box = document.getElementsByClassName('wrapper')[0]
var ul = document.getElementsByTagName('ul')[0]
var lis = document.getElementsByTagName('li')
var spans = document.getElementsByTagName('span')
var leftBtn = document.getElementById('leftBtn')
var rightBtn = document.getElementById('rightBtn')
var count = 0
var timer = null
var picWidth = lis[0].clientWidth;
/*
clone:复制
Node:节点(标签)
cloneNode():复制一个标签 如果没有参数,只复制标签,只复制里面的内容
如果有参数为true,复制标签的同时,把标签下面的内容也复制出来
*/
ul.appendChild(lis[0].cloneNode(true))
//
ul.style.width = lis.length * picWidth + 'px'
timer = setInterval(auto,3000)
//
box.onmouseover = function(){
clearInterval(timer)
}
box.onmouseout = function(){
timer = setInterval(auto,3000)
}
//
rightBtn.onclick = function(){
auto()
}
leftBtn.onclick = function(){
if(count < 0){
count = lis.length - 1
ul.style.left = -count * picWidth + 'px'
}
count--
change()
}
//
for(var i = 0;i < spans.length;i++){
spans[i].index = i
spans[i].onclick = function(){
this.className = 'active'
count = this.index
change()
}
}
//
function auto(){
if(count >= lis.length - 1){
count = 0
ul.style.left = 0 + 'px'
}
count++
change()
}
//
function change(){
move(ul,{
left: -count * picWidth
})
for(var i = 0;i < spans.length;i++){
spans[i].className = ''
}
if(count == spans.length){
spans[0].className = 'active'
}else{
spans[count].className = 'active'
}
}
</script>
</body>
</html>