原生js輪播特效
作為一名前端工程師,手寫輪播圖應(yīng)該是最基本掌握的技能,以下是我自己原生js寫的輪播,歡迎指點(diǎn)批評(píng):

首先css代碼
a{text-decoration:none;color:#3DBBF5;}
*{
margin: 0;
padding: 0;
}
.wrapper{
width: 400px;
height: 300px;
margin: 100px auto;
}
#lunbo{
position: relative;
overflow: hidden;
}
#list{
position: relative;
white-space: nowrap; // 這塊用行元素模擬,所以才用該屬性,塊元素可修改這塊
}
#list span{
display: inline-block;
width: 400px;
height: 300px;
text-align: center;
line-height: 300px;
font-weight: bold;
font-size: 100px;
color: #fff;
}
#buttons{
position: absolute;
bottom: 0;
text-align: center;
width: 100%;
height: 40px;
line-height: 40px;
}
#buttons span{
display: inline-block;
width: 15px;
height: 5px;
background: #fff;
margin: 0 10px;
cursor: pointer;
transition: all .5s;
}
#buttons span.on{
height: 20px;
}
.arrow{
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 80px;
font-weight: bold;
color: #fff;
opacity: .3;
transition: all .5s;
}
.wrapper:hover .arrow{
opacity: 1;
}
#prev{
left: 10px;
}
#next{
right: 10px;
}
然后HTML代碼
<div class="wrapper">
<div id="lunbo">
<div id="list" style="left: -400px;">
<span style="background:yellow;">5</span><span style="background: red;">1</span><span style="background:black;">2</span><span style="background:green;">3</span><span style="background:blue;">4</span><span style="background:yellow;">5</span><span style="background: red;">1</span>
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
</div>
最后js代碼
window.onload=function () {
var lunBo = document.getElementById("lunbo");
var list = document.getElementById("list");
var btn = document.getElementById("buttons").getElementsByTagName('span');
var prev = document.getElementById("prev");
var next = document.getElementById('next');
var interval = 3000;
var timer;
var index = 1;
var animated = false;
for (var i=0;i<btn.length;i++) { //按鈕加點(diǎn)擊事件
btn[i].onclick=function () {
if(this.className=='on') //如果是狀態(tài)按鈕直接返回節(jié)約資源
{
return
};
var myIndex =parseInt(this.getAttribute('index'));//獲取按鈕的index屬性值
var offset = -400*(myIndex-index); //根據(jù)屬性值 計(jì)算偏移量
animate(offset) //輪播動(dòng)畫
index = myIndex; // 改變索引值
showBtn(); //顯示狀態(tài)按鈕
}
}
function showBtn () {
for (var i=0;i<btn.length;i++) {
btn[i].className='';
}
btn[index-1].className='on';
}
prev.onclick=function () { //上一頁(yè)事件
if (animated) { //如果是動(dòng)畫狀態(tài) 直接返回解決bug
return;
}
if (index==1) {
index =btn.length;
} else{
index-=1;
}
animate(400);
showBtn();
}
next.onclick=function () {
if (animated) {
return;
}
if (index==btn.length) {
index =1;
} else{
index+=1;
}
animate(-400);
showBtn();
}
function animate(offset) {
animated = true; //表示在動(dòng)畫狀態(tài)
var newLeft = parseInt(list.style.left) + offset; //計(jì)算新的left值
var time = 400; //設(shè)置動(dòng)畫總時(shí)間
var interval = 10; //動(dòng)畫幀時(shí)間
var speed = offset/(time/interval); //每幀運(yùn)動(dòng)距離
function go () {
if ((speed>0 && parseInt(list.style.left)<newLeft) || (speed<0 && parseInt(list.style.left)>newLeft)) { //通過條件判斷到它是否還要繼續(xù)進(jìn)行動(dòng)畫
list.style.left = parseInt(list.style.left) + speed +'px';
setTimeout(go,interval)
} else{
animated = false; //動(dòng)畫狀態(tài)結(jié)束
list.style.left = newLeft + 'px'; //現(xiàn)在的位移
if (parseInt(list.style.left)<-2000) { // 輔助假圖
list.style.left = -400 + 'px';
} else if( parseInt(list.style.left)>-400){
list.style.left = -2000 + 'px';
}
}
}
go();
}
function play () {
timer = setTimeout(function () {
next.onclick();
play();
},interval)
}
play();
function stop () {
clearTimeout(timer);
}
lunBo.onmouseover=stop;
lunBo.onmouseout=play;
}
以上是所有代碼,歡迎指點(diǎn)交流!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
easywasmplayer實(shí)現(xiàn)視頻流播放示例詳解
這篇文章主要為大家介紹了easywasmplayer實(shí)現(xiàn)視頻流播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問題
今天小編就為大家分享一篇解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
網(wǎng)頁(yè)前端登錄js按Enter回車鍵實(shí)現(xiàn)登陸的兩種方法
下面小編就為大家?guī)硪黄W(wǎng)頁(yè)前端登錄js按Enter回車鍵實(shí)現(xiàn)登陸的兩種方法。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05
原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript制作拼圖游戲的相關(guān)步驟、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
Javascript 創(chuàng)建類并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狫avascript 創(chuàng)建類并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
PHP實(shí)現(xiàn)的各種中文編碼轉(zhuǎn)換類分享
這篇文章主要介紹了PHP實(shí)現(xiàn)的各種中文編碼轉(zhuǎn)換類分享,本文類庫(kù)支持簡(jiǎn)體中文、繁體中文、GB2312、BIG5、UTF-8等多種格式之間的轉(zhuǎn)換,需要的朋友可以參考下2015-01-01
用最通俗易懂的代碼幫助新手理解javascript閉包 推薦
我同樣也是個(gè)javascript新手,怎么說呢,先學(xué)的jquery,精通之后發(fā)現(xiàn)了javascript的重要性,再回過頭來學(xué)javascript面向?qū)ο缶幊?/div> 2012-03-03
原生javascript實(shí)現(xiàn)拖動(dòng)元素示例代碼
首先改變被拖動(dòng)元素的布局屬性,接著捕捉鼠標(biāo)事件,當(dāng)觸發(fā)mousedown時(shí),記錄下當(dāng)前鼠標(biāo)在元素中的相對(duì)位置,接著處理mousemove事件2014-09-09
javascript二維數(shù)組和對(duì)象的深拷貝與淺拷貝實(shí)例分析
這篇文章主要介紹了javascript二維數(shù)組和對(duì)象的深拷貝與淺拷貝,結(jié)合實(shí)例形式分析了JavaScript針對(duì)數(shù)組與對(duì)象的深拷貝及淺拷貝相關(guān)操作技巧,需要的朋友可以參考下2019-10-10最新評(píng)論

