JavaScript輪播停留效果的實(shí)現(xiàn)思路
一、思路
1.輪播停留與無(wú)線(xiàn)滾動(dòng)十分類(lèi)似,都是利用屬性及變量控制移動(dòng)實(shí)現(xiàn)輪播;
2.不同的是輪播停留需要添加過(guò)渡屬性搭配定時(shí)器即可實(shí)現(xiàn)輪播停留效果;
二、步驟
1.寫(xiě)基本結(jié)構(gòu)樣式
需在末尾多添加一張與第一張相同的圖片,消除切換時(shí)的抖動(dòng);
2.添加輪播停留事件 有了之前的基礎(chǔ),直接添加索引圈默認(rèn)事件到輪播停留事件內(nèi);
注意:當(dāng)輪播到最后一張時(shí),需要消除掉過(guò)渡,這里使用setTimeout定時(shí)器,卡最后一張圖片輪播完不延時(shí),直接跳轉(zhuǎn)到第一張,由于第一張和最后一張一樣,所以會(huì)形成視覺(jué)盲區(qū),看起來(lái)是連續(xù)輪播效果;
//輪播停留方法
function move() {
box.className = "box anmint";
circle[count].style.backgroundColor = "";
count++;
box.style.marginLeft = (-800 * count) + "px";
//最后一張走完之后,執(zhí)行一次定時(shí)器不循環(huán),卡過(guò)渡時(shí)間,消除切換
setTimeout(function () {
if (count >= 6) {
count = 0;
box.className = "box";
//marginLeft=0之前去除過(guò)渡屬性
box.style.marginLeft = "0px";
}
circle[count].style.backgroundColor = "red";
}, 500);
}
3.添加進(jìn)入索引圈事件
這和淡入淡出進(jìn)入索引圈事件基本一致,不同的是這里不用調(diào)用輪播停留事件,直接利用當(dāng)前index來(lái)索引使圖片跟隨變換;注意最后要標(biāo)記count=this.index值,令再次執(zhí)行默認(rèn)行為時(shí)是緊跟著當(dāng)前顯示圖片向后執(zhí)行默認(rèn)行為;
//進(jìn)入索引圈事件
for(var j=0;j<circle.length;j++){
circle[j].index=j;
circle[j].onmouseenter=function(){
for(var k=0;k<circle.length;k++){
circle[k].style.backgroundColor="";
}
this.style.backgroundColor="red";
//圖片跟隨移動(dòng)
box.className="box anmint";
box.style.marginLeft=(-800*this.index)+"px";
count=this.index;
}
}
4.完善鼠標(biāo)進(jìn)入離開(kāi)代碼
效果圖:

完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS輪播停留效果</title>
<style>
*{margin: 0;padding: 0;}
html,body{width: 100%;height: 100%;}
.block{
width: 800px;
height: 400px;
margin: 80px auto;
position: relative;
border: 1px solid red;
overflow: hidden;
}
.box{
width: 5600px;
height: 400px;
float: left;
}
.anmint{
transition: all 0.5s ease-in-out;
}
img{
width: 800px;
height: 400px;
float: left;
}
.cir{
width: 150px;
height: 20px;
z-index: 7;
position: absolute;
bottom: 10px;
left: 320px;
}
.circle{
width: 10px;
height: 10px;
border: 2px solid grey;
border-radius: 50%;
float: left;
margin: 0 5px;
}
</style>
<script>
window.onload=function(){
var box=document.getElementsByClassName("box")[0];
var count=0;
//索引圈事件
var circle=document.getElementsByClassName("circle");
circle[0].style.backgroundColor="red";
var time=setInterval(function(){
move();
},2000);
//鼠標(biāo)進(jìn)入事件
var block=document.getElementsByClassName("block")[0];
block.onmouseenter=function(){
clearInterval(time);
};
//鼠標(biāo)離開(kāi)事件
block.onmouseleave=function(){
time=setInterval(function(){
move();
},2000);
};
//進(jìn)入索引圈事件
for(var j=0;j<circle.length;j++){
circle[j].index=j;
circle[j].onmouseenter=function(){
for(var k=0;k<circle.length;k++){
circle[k].style.backgroundColor="";
}
this.style.backgroundColor="red";
//圖片跟隨移動(dòng)
box.className="box anmint";
box.style.marginLeft=(-800*this.index)+"px";
count=this.index;
}
}
//輪播停留方法
function move() {
box.className = "box anmint";
circle[count].style.backgroundColor = "";
count++;
box.style.marginLeft = (-800 * count) + "px";
//最后一張走完之后,執(zhí)行一次定時(shí)器不循環(huán),卡過(guò)渡時(shí)間,消除切換
setTimeout(function () {
if (count >= 6) {
count = 0;
box.className = "box";
//marginLeft=0之前去除過(guò)渡屬性
box.style.marginLeft = "0px";
}
circle[count].style.backgroundColor = "red";
}, 500);
}
}
</script>
</head>
<body>
<div class="block">
<div class="box">
<img class="imgg" src="./image/box1.jpg">
<img class="imgg" src="./image/box2.jpg">
<img class="imgg" src="./image/box3.jpg">
<img class="imgg" src="./image/box4.jpg">
<img class="imgg" src="./image/box5.jpg">
<img class="imgg" src="./image/box6.jpg">
<img class="imgg" src="./image/box1.jpg">
</div>
<div class="cir">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
</div>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的JavaScript輪播停留效果的思路詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript實(shí)現(xiàn)sleep睡眠函數(shù)的幾種簡(jiǎn)單方法總結(jié)
sleep是一種函數(shù),他的作用是使程序暫停指定的時(shí)間,起到延時(shí)的效果,下面這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)sleep睡眠函數(shù)的幾種簡(jiǎn)單方法總結(jié),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
基于JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的事件觸發(fā)器
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的事件觸發(fā)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
探討JavaScript標(biāo)簽位置的存放與功能有無(wú)關(guān)系
在網(wǎng)頁(yè)中,我們可以將JavaScript代碼放在html文件中任何位置,但一般放在head或body標(biāo)簽里面。一般來(lái)說(shuō),<script>元素放在哪里與其的功能作用是緊密相關(guān)的,通過(guò)本文我們一起學(xué)習(xí)下2016-01-01
js編寫(xiě)三級(jí)聯(lián)動(dòng)簡(jiǎn)單案例
這篇文章主要為大家分享了JavaScript編寫(xiě)三級(jí)聯(lián)動(dòng)簡(jiǎn)單案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
js實(shí)現(xiàn)鼠標(biāo)移到鏈接文字彈出一個(gè)提示層的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)移到鏈接文字彈出一個(gè)提示層的方法,涉及javascript鼠標(biāo)事件與css樣式的相關(guān)技巧,需要的朋友可以參考下2015-05-05
js實(shí)現(xiàn)鼠標(biāo)滾輪控制圖片縮放效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)滾輪控制圖片縮放效果的方法,涉及onmousewheel事件及javascript操作圖片的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
用JavaScript實(shí)現(xiàn)用一個(gè)DIV來(lái)包裝文本元素節(jié)點(diǎn)
當(dāng)我試圖將文本(可能也包含HTML元素)用一個(gè)DIV元素包起來(lái)時(shí),可以使用下面的方法,需要的朋友可以參考下2014-09-09

