原生js實現輪播

來源:互聯網
上載者:User

標籤:select   lang   jpg   ext   index   interval   indexof   idt   type   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container{
width: 500px;
height: 300px;
position: relative;
border: 1px solid red;
}
.img-content{
width: 500px;
height: 300px;
}
.point{
position: absolute;
left: 30%;
bottom: 5%;
list-style: none;
}
.point>li{
width: 20px;
height: 20px;
float: left;
margin-left: 10px;
border: 1px solid red;
border-radius: 50%;
line-height: 20px;
text-align: center;
}
.left{
width: 50px;
height: 30px;
position: absolute;
top: 40%;
left: 10%;
border: 1px solid red;
}
.right{
width: 50px;
height: 30px;
position: absolute;
top: 40%;
right: 10%;
border: 1px solid red;
}
.img-content>img{
display: none;
}
.img-content>img:nth-child(1){
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<div class="img-content">
<img src="img/f1.jpg" >
<img src="img/f2.jpg" >
<img src="img/f3.jpg" >
</div>
<ul class="point">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<div class="left">
<
</div>
<div class="right">
>
</div>
</div>
<script>
/*擷取dom元素*/
var img=document.querySelectorAll(".img-content>img");
var li=document.querySelectorAll(".point>li");
var left=document.getElementsByClassName("left")[0];
var right=document.getElementsByClassName("right")[0];
var container=document.getElementsByClassName("container")[0];
var index=0;
var timer=setInterval(slide,2000);//定義一個計時器
function slide(){
for(var i=0;i<img.length;i++){
img[i].style.display="none";//先將所有的圖片都隱藏
}
img[index].style.display="inline-block";//再顯示其中一張
index++;
if(index==img.length){
index=0;//當顯示到最後一張時,在回到第一張
}
}
container.onmouseover=function(){
clearInterval(timer);//清楚計時器
};
container.onmouseout=function(){
timer=setInterval(slide,2000);//再次啟用計時器,記住此處一定不要再用var
};
var arr=[];//定義一個數組
for(var i=0;i<li.length;i++){
arr.push(li[i]);//將每個添加到數組內
li[i].addEventListener("click",function(){//利用迴圈給每個li綁定點擊事件
var index1=arr.indexOf(this);//擷取當前點擊事件的對象也就是this在arr中的下標,然後再顯示當前下標的圖片;
console.log(index1);
for(var j=0;j<img.length;j++){
img[j].style.display="none";//先用迴圈將每張圖片隱藏
}
img[index1].style.display="inline-block";//顯示這個下標的圖片
})
}
/*right.addEventListener("click", function () {
var arr1=[];
for(var i=0;i<img.length;i++){
//arr1.push(getComputedStyle(img[i])[‘display‘]);
if(getComputedStyle(img[i])[‘display‘]=="inline-block"){
img[i+1].style.display="inline-block";
if(i>=img.length){
img[0].style.display="inline-block";
}
}
}
console.log(arr1);
})*/
</script>
</body>
</html>

原生js實現輪播

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.