靜態(tài)獲取圖片寫法,給定圖片的個數(shù),用js實現(xiàn)輪播圖自動轉換。
創(chuàng)新互聯(lián)建站是專業(yè)的拱墅網(wǎng)站建設公司,拱墅接單;提供成都網(wǎng)站建設、成都網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行拱墅網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
titleDocument/title
!-- *******設置樣式********** --
style type="text/css"
.show_div{
width: 400px;
height: 400px;
margin: ?0 auto;
border: 2px solid block;
overflow: hidden;
}
.scroll_div{
width: 2000px;
height: 400px;
}
.scroll_div img{
width: 400px;
height: 400px;
float: left;
}
/style
!-- end --
/head
body
div class="show_div"
div class="scroll_div"
img src="img/b.jpg" alt=""
img src="img/c.jpg" alt=""
img src="img/d.jpg" alt=""
img src="img/a.jpg" alt=""
img src="img/b.jpg" alt=""
/div
/div
/body
!-- *********js代碼******** --
script type="text/javascript"
var scrollDiv = document.getElementsByClassName("scroll_div")[0];
// 定義初始值
var left =0;
// 定義一個定時器 走一步
function move(){
var timer = setInterval(function(){
left --;
if (left = -1600) {
left = 0;
}
if (left % -400 == 0) {
clearInterval(timer);
timer = null;
}
scrollDiv.style.marginLeft = left + "px";
},10);
}
// 定義一個定時器 每隔固定時間 走一張
setInterval(function(){
move();
},5000);
/script
/html
1、首先我們創(chuàng)建一個簡單的項目,如圖所示包括html,css和img三個。
2、這里是html文件,引入css和html代碼文件,如圖所示。
3、這里是css文件代碼,上面是div和圖片顯示的效果代碼,后面是動畫效果。
4、這里是事件,這里定義了四個時間段的狀態(tài),兼容了ie的。
5、如圖所示這里是效果圖,會根據(jù)時間輪播顯示下一張圖片 了。
!DOCTYPE?htmlhtml
head
meta?charset="utf-8"?/
title輪播圖/title
style?type="text/css"
*?{????????????????margin:?0px;????????????????padding:?0px;
}????????????#lunbotu?{????????????????width:?1226px;????????????????height:?460px;????????????????overflow:?hidden;????????????????position:?relative;????????????????margin:?0px?auto;????????????????position:?relative;
}????????????#banner?{????????????????height:?460px;????????????????width:?6130px;????????????????position:?absolute;????????????????transition:?2s;????????????????left:?0px;
}????????????#banner?img?{????????????????float:?left;
}????????????#biao?{????????????????position:?absolute;????????????????top:?430px;????????????????left:?43%;
}????????????#biao_1?{????????????????height:?20px;????????????????width:?20px;????????????????border:?1px?solid?#000;????????????????z-index:?10;????????????????float:?left;????????????????list-style:?none;????????????????border-radius:?50%;????????????????margin-left:?20px;????????????????text-align:?center;????????????????cursor:pointer;
}????????/style
/head
body
div?id="lunbotu"
div?id="banner"
img?class="m"?src="img/T1hiDvBvVv1RXrhCrK.jpg"?/
img?class="m"?src="img/T1jrxjB_VT1RXrhCrK.jpg"?/
img?class="m"?src="img/T1oTJjBKKT1RXrhCrK.jpg"?/
img?class="m"?src="img/T1RICjB7DT1RXrhCrK.jpg"?/
img?class="m"?src="img/T1vWdTBKDv1RXrhCrK.jpg"?/
/div
div?id="biao"
ul
li?id="biao_1"1/li
li?id="biao_1"2/li
li?id="biao_1"3/li
li?id="biao_1"4/li
li?id="biao_1"5/li
/ul
/div
/div
/body
script?type="text/javascript"
var?slid?=?document.getElementById("banner");????????//var?id?=?document.getElementById("bt");
var?imgwidth?=?document.getElementsByClassName("m");????????var?oli=document.getElementsByTagName("li");????????//console.log(oli);
//console.log(imgwidth?);
var?i?=0;
auto();
oli[0].style.cssText="background:#ff6700;color:#fff;";????????function?auto(){
time?=?setInterval(function(){
i++;????????????if(i?=?4)?{
slid.style.left?=?slid.offsetLeft?-?1226?+?"px";
oli[i].style.cssText="background:#ff6700;color:#fff;";
oli[i-1].style.cssText="background:none;color:#000;";
}?else?{
slid.style.left?="0px";
oli[4].style.cssText="background:none;color:#000;";
oli[0].style.cssText="background:#ff6700;color:#fff;";
i=0;
}????????????console.log(i);
},?3000)
}????????????for(var?j=0;j=4;j++){????????????????//console.log(imgwidth[j].index);
imgwidth[j].index=j;
oli[j].index=j;
oli[j].onmouseover=function(){????????????????this.style.cssText="background:#ff6700;color:#fff;"????
this.onmouseout=function(){????????????????????this.style.cssText="background:none;color:#000;"
}
}
oli[j].onclick=function(){
clearInterval(time);
m=this.index;
slid.style.left=-m*1226+"px";????
i=m;
auto();????????????????console.log(i);
}
}????
/script/html
光是html的話很難實現(xiàn)輪播,輪播一般都是html+js才能完成。
給你一個最簡單的html+js輪播例子:
html代碼:
div?class="side"?id?=?"lunbo"????
ul"????
liimg?src="../images/1.jpg"/li????
liimg?src="../images/2.jpg"/li????
liimg?src="../images/3.jpg"/li????
/ul????
/div
js代碼:
var?li=document.getElementById('lunbo').getElementsByTagName("li");??
var?num=0;??
var?len=li.length;??
setInterval(function(){??
li[num].style.display="none";??
num=++num==len?0:num;??
li[num].style.display="inline-block";??
},3000);//切換時間