JS輪播圖實(shí)現(xiàn)簡(jiǎn)單代碼
本文實(shí)例為大家分享了js輪播圖實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
思路:
1、首先要有個(gè)盛放圖片的容器,設(shè)置為單幅圖片的寬高,且overflow:hidden,這樣保證每次可以只顯示一個(gè)圖片
2、Container內(nèi)有個(gè)放圖片的list進(jìn)行position的定位 ,其中的圖片采用float的方式,同時(shí)當(dāng)圖片進(jìn)行輪播時(shí),改變list的Left值使得其顯示的圖片發(fā)生變化。
3、圖片的輪播使用定時(shí)器,通過(guò)定時(shí)器改變list的Left值是的圖片循環(huán)展示
4、當(dāng)鼠標(biāo)滑動(dòng)到圖片上時(shí),清除定時(shí)器,圖片停止輪播,當(dāng)鼠標(biāo)移出時(shí)繼續(xù)進(jìn)行輪播
5、圖片上有一組小圓點(diǎn)用于與當(dāng)前顯示的圖片相對(duì)應(yīng),同時(shí)可以通過(guò)點(diǎn)擊的方式查看對(duì)應(yīng)的圖片
6、圖片可以通過(guò)點(diǎn)擊進(jìn)行左右滑動(dòng)顯示
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>輪播圖</title>
<style type="text/css">
.container{
margin:0 auto;
width:600px;
height:400px;
position: relative;
overflow: hidden;
border:4px solid gray;
box-shadow: 3px 3px 5px gray;
border-radius:10px;
}
.list{
width:4200px;
height:400px;
position: absolute;
top:0px;
}
img{
float:left;
width:600px;
height:400px;
}
.dots{
position: absolute;
left:40%;
bottom:30px;
list-style: none;
}
.dots li{
float:left;
width:8px;
height:8px;
border-radius: 50%;
background: gray;
margin-left:5px
}
.dots .active{
background: white;
}
.pre,.next{
position: absolute;
top:40%;
font-size:40px;
color:white;
text-align:center;
background: rgba(128,128,128,0.5);
/* display:none;*/
}
.pre{
left:30px;
}
.next{
right:30px;
}
</style>
</head>
<body>
<div class="container">
<div class="list" style=" left:-600px;">
<img src="img/5.jpg">
<img src="img/1.jpg">
<img src="img/2.jpg">
<img src="img/3.jpg">
<img src="img/4.jpg">
<img src="img/5.jpg">
<img src="img/1.jpg">
</div>
<ul class="dots">
<li index=1 class="active dot"></li>
<li index=2 class="dot"></li>
<li index=3 class="dot"></li>
<li index=4 class="dot"></li>
<li index=5 class="dot"></li>
</ul>
<div class="pre"><</div>
<div class="next">></div>
</div>
<script type="text/javascript">
var index=1,timer;
function init(){
eventBind();
autoPlay();
}
init();
function autoPlay(){
timer =setInterval(function () {
animation(-600);
dotIndex(true);
},1000)
}
function stopAutoPlay() {
clearInterval(timer);
}
function dotIndex(add){
if(add){
index++;
}
else{
index--;
}
if(index>5){
index = 1;
}
if(index<1){
index = 5;
}
dotActive();
}
function dotActive() {
var dots = document.getElementsByClassName("dot");
var len = dots.length;
for(var i=0 ;i<len ;i++){
dots[i].className = "dot";
}
for(var i=0;i<len;i++){
/*此處可以不用parseInt,當(dāng)不用全等時(shí)*/
if(index === parseInt(dots[i].getAttribute("index"))){
dots[i].className = "dot active";
}
}
}
function eventBind(){
/*點(diǎn)的點(diǎn)擊事件*/
var dots = document.getElementsByClassName("dot");
var len = dots.length;
for(var i=0;i<len;i++){
(function(j){
dots[j].onclick = function(e){
var ind = parseInt(dots[j].getAttribute("index"));
animation((index - ind)*(-600));/*顯示點(diǎn)擊的圖片*/
index = ind;
dotActive();
}
})(i)
}
/*容器的hover事件*/
var con = document.getElementsByClassName("container")[0];
/*鼠標(biāo)移動(dòng)到容器上時(shí),停止制動(dòng)滑動(dòng),離開時(shí)繼續(xù)滾動(dòng)*/
con.onmouseover = function (e) {
stopAutoPlay();
}
con.onmouseout =function(e){
autoPlay();
}
/*箭頭事件的綁定*/
var pre = document.getElementsByClassName("pre")[0];
var next = document.getElementsByClassName("next")[0];
pre.onclick = function (e) {
dotIndex(false);
animation(600);
}
next.onclick = function (e) {
dotIndex(true);
animation(-600);
}
}
function animation(offset){
var lists = document.getElementsByClassName("list")[0];
var left = parseInt(lists.style.left.slice(0,lists.style.left.indexOf("p"))) + offset;
if(left<-3000){
lists.style.left = "-600px";
}
else if(left>-600){
lists.style.left = "-3000px";
}
else{
lists.style.left = left+"px";
}
}
</script>
</body>
</html>
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
bootstrap-paginator服務(wù)器端分頁(yè)使用方法詳解
這篇文章主要為大家詳細(xì)介紹了bootstrap-paginator服務(wù)器端分頁(yè)的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Javascript中查找不以XX字符結(jié)尾的單詞示例代碼
我在寫這篇文章之前花了2個(gè)多小時(shí)在弄正則表達(dá)式,下為大家介紹下具體的實(shí)現(xiàn)思路,感興趣的朋友可以參考下2013-10-10
JavaScript返回網(wǎng)頁(yè)中超鏈接數(shù)量的方法
這篇文章主要介紹了JavaScript返回網(wǎng)頁(yè)中超鏈接數(shù)量的方法,使用javascript中的document.links實(shí)現(xiàn)這一功能,需要的朋友可以參考下2015-04-04
使用javascript做時(shí)間倒數(shù)讀秒功能的實(shí)例
今天小編就為大家分享一篇關(guān)于使用javascript做時(shí)間倒數(shù)讀秒功能的實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

