簡單輪播圖

輪播圖,因該是小白前端比較炫酷的動態效果;下面是最簡單的js輪播了,研究了下不是無縫滾動,經過三目運算符來判斷下標值完成首尾循環。
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>圖片展現</title>
</head>
<style>
body,ul,li{
margin: 0;
padding: 0;
}
li{
list-style:none;
height:300px;
border-bottom:1px solid #888;
width:600px;
float:left;
}
li img{
width:600px;
height:300px;
 
}
ul{
width: 3000px;
height:300px;
overflow: hidden;
position: relative;
}
</style>
<body>
<ul id="ul_img">
<li><img src="imgs/0.png" alt=""></li>
<li><img src="imgs/1.png" alt=""></li>
<li><img src="imgs/2.png" alt=""></li>
<li><img src="imgs/3.png" alt=""></li>
<li><img src="imgs/4.png" alt=""></li>
</ul>
</body>
<script>
var li=document.getElementById("ul_img").getElementsByTagName("li");
var num=0;
var leng=li.length;
setInterval(function(){
li[num].style.display="none";
num=++num==leng? 0:num;
li[num].style.display="inline-block";
},2000);
</script>
</html>

 

 
這個三目運算符用的好!!!記一下
 
css3實現的輪播圖效果很好,若是輪播插件與其餘插件起衝突了能夠用來;要考慮兼容性
<!DOCTYPE html>
<html>
<head>
<title>純CSS代碼實現圖片輪播 </title>
<meta charset="utf-8" />
<meta name="description" />
<meta name="keywords" />
<style type="text/css">
#frame {/*----------圖片輪播相框容器----------*/
position: absolute; /*--絕對定位,方便子元素的定位*/
width: 300px;
height: 200px;
overflow: hidden;/*--相框做用,只顯示一個圖片---*/
border-radius:5px;
}
#dis {/*--絕對定位方便li圖片簡介的自動分佈定位---*/
position: absolute;
left: -50px;
top: -10px;
opacity: 0.5;
}
#dis li {
display: inline-block;
width: 200px;
height: 20px;
margin: 0 50px;
float: left;
text-align: center;
color: #fff;
border-radius: 10px;
background: #000;
}
#photos img {
float: left;
width:300px;
height:200px;
}
#photos {/*---設置總的圖片寬度--經過位移來達到輪播效果----*/
position: absolute;z-index:9px;
width: calc(300px * 5);/*---修改圖片數量的話須要修改下面的動畫參數*/
}
.play{
animation: ma 20s ease-out infinite alternate;/**/
}
@keyframes ma {/*---每圖片切換有兩個階段:位移切換和靜置。中間的效果能夠任意定製----*/
0%,20% { margin-left: 0px; }
25%,40% { margin-left: -300px; }
45%,60% { margin-left: -600px; }
65%,80% { margin-left: -900px; }
85%,100% { margin-left: -1200px; }
}
.num{
position:absolute;z-index:10;
display:inline-block;
right:10px;top:165px;
border-radius:100%;
background:#f00;
width:25px;height:25px;
line-height:25px;
cursor:pointer;
color:#fff;
text-align:center;
opacity:0.8;
}
.num:hover{background:#00f;}
.num:hover,#photos:hover{animation-play-state:paused;}
.num:nth-child(2){margin-right:30px}
.num:nth-child(3){margin-right:60px}
.num:nth-child(4){margin-right:90px}
.num:nth-child(5){margin-right:120px}
#a1:hover ~ #photos{animation: ma1 .5s ease-out forwards;}
#a2:hover ~ #photos{animation: ma2 .5s ease-out forwards;}
#a3:hover ~ #photos{animation: ma3 .5s ease-out forwards;}
#a4:hover ~ #photos{animation: ma4 .5s ease-out forwards;}
#a5:hover ~ #photos {animation: ma5 .5s ease-out forwards;}
@keyframes ma1 {0%{margin-left:-1200px;}100%{margin-left:-0px;} }
@keyframes ma2 {0%{margin-left:-1200px;}100%{margin-left:-300px;} }
@keyframes ma3 {100%{margin-left:-600px;} }
@keyframes ma4 {100%{margin-left:-900px;} }
@keyframes ma5 {100%{margin-left:-1200px;} }
</style>
</head>
<body>
<div id="frame" >
<a id="a1" class="num">1</a>
<a id="a2" class="num">2</a>
<a id="a3" class="num">3</a>
<a id="a4" class="num">4</a>
<a id="a5" class="num">5</a>
<div id="photos" class="play">
<img src="http://eduppp.cn/images/0/1.jpg" >
<img src="http://eduppp.cn/images/0/3.jpg" >
<img src="http://eduppp.cn/images/0/4.jpg" >
<img src="http://eduppp.cn/images/0/5.jpg" >
<img src="http://eduppp.cn/images/0/2.jpg" >
<ul id="dis">
<li>中國標誌性建築:天安門</li>
<li>中國標誌性建築:東方明珠</li>
<li>中國標誌性建築:布達拉宮</li>
<li>中國標誌性建築:長城</li>
<li>中國標誌性建築:天壇</li>
</ul>
</div>
</div>
</body>
</html>
相關文章
相關標籤/搜索