這篇文章給大家分享的是有關(guān)js實(shí)現(xiàn)輪播圖效果的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),彭澤網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:彭澤等地區(qū)。彭澤做網(wǎng)站價(jià)格咨詢:13518219792
實(shí)現(xiàn)輪播圖
學(xué)習(xí)前端差不多兩三個(gè)月,在這里記錄分享一下。因本人菜鳥(niǎo)一枚,希望大佬們多多指點(diǎn),勿噴。
通過(guò)計(jì)算每一張圖片高度實(shí)現(xiàn)滑動(dòng)輪播圖
HTML代碼:
<div class="fate"> <div class="lancer"> <ul class="saber"> <li><img src="img/illust_13010631_20191118_150928.jpg"></li> <li><img src="img/illust_13010631_20191118_150928.jpg"></li> <li><img src="img/illust_13010631_20191118_150928.jpg"></li> <li><img src="img/illust_13010631_20191118_150928.jpg"></li> <li><img src="img/illust_13010631_20191118_150928.jpg"></li> </ul> </div> </div> <div class="archer"> <button class="goup" onclick="goup()"><</button> <button class="godown" onclick="godown()">></button> </div>
CSS代碼:
* {
transition: all 1s;
margin: 0px;
padding: 0px;
}
.fate {
position: absolute;
top: 0%;
width: 512px;
height: 512px;
overflow: hidden;
}
ul li {
list-style: none;
}
.lancer{
position: absolute;
top: 0%;
width: 100%;
height: 100%;
}
.saber {
top: 0px;
position: absolute;
}
.archer{
position: absolute;
}
button {
z-index: 99;
}JS代碼:
var index=0;
function godown(){
var li = document.getElementsByTagName("li");
//獲取單個(gè)li寬度(單張圖片高度)
var liHeight = li[0].scrollHeight;
var goup=document.getElementsByClassName("goup");
var lancer=document.getElementsByClassName("lancer")[0];
if(index<4){
index++;
}else{
index=0;
}
lancer.style.top = -index*liHeight+"px";
}
function goup(){
var li = document.getElementsByTagName("li");
//獲取單個(gè)li寬度(單張圖片高度)
var liHeight = li[0].scrollHeight;
var goup=document.getElementsByClassName("goup");
var lancer=document.getElementsByClassName("lancer")[0];
if(index>0){
index--;
}else{
index=4;
}
lancer.style.top = -index*liHeight+"px";
}如有錯(cuò)誤望指出。
利用z-index實(shí)現(xiàn)輪播圖
HTML代碼
<div class="fate"> <div class="saber"> <ul> <li class="archer active"><img src="img/illust_13010631_20191118_150928.jpg"></li> <li class="archer"><img src="img/illust_13010631_20191118_150928.jpg"></li> <li class="archer"><img src="img/illust_13010631_20191118_150928.jpg"></li> <li class="archer"><img src="img/illust_13010631_20191118_150928.jpg"></li> <li class="archer"><img src="img/illust_13010631_20191118_150928.jpg"></li> </ul> </div> </div> <div class="lancer"> <button id="goup">></button> <button id="godown"><</button> </div>
CSS代碼
li {
list-style: none;
}
.archer {
position: absolute;
top: 0%;
display: none;
}
.active {
display: block;
}
button {
position: absolute;
top: 70%;
width: 50px;
}
#goup {
left: 400px;
}
#godown {
left: 0px;
}JS代碼
var pic = document.getElementsByTagName("li");
var archer = document.getElementsByClassName("archer");
var goup = document.getElementById("goup");
var godown = document.getElementById("godown");
var index = 0;
goup.onclick = function() {
for (var i = 0; i < pic.length; i++) {
pic[i].className = "archer";
}
if (index < 4) {
index++;
} else {
index = 0;
}
pic[index].className = "active";
}
godown.onclick = function() {
for (var i = 0; i < pic.length; i++) {
pic[i].className = "archer";
}
if (index > 0) {
index--;
} else {
index = 4;
}
pic[index].className = "active";
}利用index來(lái)實(shí)現(xiàn)輪播,但是生硬。
感謝各位的閱讀!關(guān)于“js實(shí)現(xiàn)輪播圖效果的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享標(biāo)題:js實(shí)現(xiàn)輪播圖效果的方法
當(dāng)前路徑:http://www.jbt999.com/article4/psiooe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、營(yíng)銷型網(wǎng)站建設(shè)、建站公司、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)