原生js實(shí)現(xiàn)焦點(diǎn)輪播圖效果
原生js焦點(diǎn)輪播圖主要注意這幾點(diǎn):
1、前后按鈕實(shí)現(xiàn)切換,同時(shí)注意輔助圖
2、中間的button隨著前后按鈕對(duì)應(yīng)切換,同時(shí)按button也能跳轉(zhuǎn)到相應(yīng)的index
3、間隔調(diào)用與無限輪播。
4、注意在動(dòng)畫時(shí)要停止按鈕,或者說上一個(gè)動(dòng)畫完畢下一個(gè)動(dòng)畫才能執(zhí)行
5、另外在切換圖片的時(shí)候,底部的Button動(dòng)畫效果,是從底部開始往上升的,要用到transform:scale()和transform-origin:0 100%兩個(gè)轉(zhuǎn)換屬性,代碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewpoint" content="width=device-width,initial-scale=1,user-scalable="no">
<title>20170101</title>
<style type="text/css">
a{text-decoration:none;color:#3DBBF5;}
.wrapper{width:750px;height:350px;background:#001032;margin:20px auto;text-align:center;box-shadow:0 0 12px 2px hsla(0,20%,30%,0.5);padding:10px 15px;position:relative;}
.effect{position:relative;cursor:pointer;}
.effect:hover{color:#02a0e9;}
.effect:before{width:100%;display:inline-block !important;position:absolute;height:1px;background:#02a0e9;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(0,1);content:'';bottom:-5px;left:0;}
.effect:hover:before{transform:scale(1);-webkit-transform:scale(1);}
#lunBo{margin-top:20px;overflow:hidden;height:300px;width:750px;position:relative;}
#list{position:absolute;z-index:22;height:300px;width:5250px;}
#list img{float:left;}
#buttons { position: absolute; height: 20px; width: 150px; z-index: 99; bottom: 20px; left: 40%;}
span { cursor: pointer; float: left; width: 10px; height: 5px; background: #333; margin-right: 10px;}
.on { background: yellow;transition:all 0.4s ease-in-out;-webkit-transition:all 0.4s ease-in-out;-moz-transition:all 0.4s ease-in-out;transform:scale(1,4);-ms-transform:scale(1,4);-moz-transform:scale(1,4);-webkit-transform:scale(1,4);transform-origin:0% 0%;-webkit-transform-origin:0% 100%;-moz-transform-origin:0% 100%;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 100px; line-height:100px;position: absolute; z-index: 92; top: 30%; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#lunBo:hover .arrow { display: block;}
#prev { left: 0px;}
#next { right: 0px;}
</style>
</head>
<body>
<div class="wrapper">
<a class="effect" href="#">2016完了,2017來了</a>
<div id="lunBo">
<div id="list" style="left:-750px;">
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856m1bhxxx1d8jfnblb.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856z48mfrrr8u064rf6.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856e95yze236lvq7y2a.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/03/175856saeagzgsnwal15n5.jpg" alt=""/>
<img src="http://cdn.attach.qdfuns.com/notes/pics/201701/02/235009drzwcaxem2wfgmdc.jpg" alt=""/>
</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>
<script>
window.onload = function(){
var lunBo = document.getElementById('lunBo');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
//console.log(buttons);
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
var animated = false;
var interval = 3000;
var timer;
//顯示按鈕的索引
function showButton(){
for(var i = 0 ; i < buttons.length ; i++){
if( buttons[i].className == 'on' ){
buttons[i].className = '';
break;
};
};
buttons[index - 1].className='on';
};
function play(){
timer = setTimeout(function () {
next.onclick();
play();
}, interval);
};
function stop(){
clearTimeout(timer);
};
//向前按鈕
next.onclick = function () {
if (animated) {
return;
}
if (index == 5) {
index = 1;
}
else {
index += 1;
}
animate(-750);
showButton();
};
prev.onclick = function () {
if (animated) {
return;
}
if (index == 1) {
index = 5;
}
else {
index -= 1;
}
animate(750);
showButton();
};
//parseInt()轉(zhuǎn)換為純數(shù)值
function animate(offset){
animated = true;
var newLeft = parseInt(list.style.left) + offset; //目標(biāo)值
var time = 300; //位移總時(shí)間為300
var interval = 10; //
var speed = offset/(Math.floor(time/interval)); //每次位移量
function go(){
if( (speed < 0 && parseInt(list.style.left) > newLeft) || ( speed > 0 && parseInt(list.style.left) < newLeft) ){
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go,interval);
}else{
animated = false;
list.style.left = newLeft+ 'px'; //現(xiàn)在的位移
if( newLeft > -750){ //假的輔助圖
list.style.left = -3750 + 'px';
}
if( newLeft < -3750){
list.style.left = -750 + 'px';
}
}
};
go();
};
//小按鈕
for(var i=0;i < buttons.length;i++){
buttons[i].onclick = function(){
if(this.className == 'on'){
return;
};
var myIndex = parseInt(this.getAttribute('index'));
var offset = -750 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
lunBo.onmouseout = play;
lunBo.onmouseover = stop;
play();
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- 原生js實(shí)現(xiàn)輪播圖的示例代碼
- 原生js實(shí)現(xiàn)無限循環(huán)輪播圖效果
- 原生js實(shí)現(xiàn)移動(dòng)開發(fā)輪播圖、相冊(cè)滑動(dòng)特效
- 原生js實(shí)現(xiàn)無縫輪播圖效果
- 原生js實(shí)現(xiàn)旋轉(zhuǎn)木馬輪播圖效果
- 原生JS實(shí)現(xiàn)層疊輪播圖
- 原生JS輪播圖插件
- 原生JS京東輪播圖代碼
- 原生js實(shí)現(xiàn)網(wǎng)易輪播圖效果
- 無限循環(huán)輪播圖之運(yùn)動(dòng)框架(原生JS實(shí)現(xiàn))
- 原生JS實(shí)現(xiàn)的輪播圖功能詳解
相關(guān)文章
JS使用正則實(shí)現(xiàn)去掉字符串左右空格的方法
這篇文章主要介紹了JS使用正則實(shí)現(xiàn)去掉字符串左右空格的方法,結(jié)合實(shí)例形式分析了JS針對(duì)首尾匹配及空格匹配的簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12
利用JS實(shí)現(xiàn)scroll自定義滾動(dòng)效果詳解
這篇文章主要給大家介紹了關(guān)于利用JS如何實(shí)現(xiàn)scroll自定義滾動(dòng)效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
微信小程序?qū)崿F(xiàn)歷史搜索功能的全過程(h5同理)
最近在使用微信小程序開發(fā)的時(shí)候遇到了一個(gè)需求,需要實(shí)現(xiàn)歷史搜索記錄的功能,所以下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)歷史搜索功能(h5同理)的相關(guān)資料,需要的朋友可以參考下2022-12-12
Next.js解決axios獲取真實(shí)ip問題方法分析
這篇文章主要介紹了Next.js解決axios獲取真實(shí)ip問題方法分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
JS實(shí)現(xiàn)移動(dòng)端雙指縮放和旋轉(zhuǎn)方法
這篇文章主要介紹了JS實(shí)現(xiàn)移動(dòng)端雙指縮放和旋轉(zhuǎn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
js實(shí)現(xiàn)的簡(jiǎn)練高效拖拽功能示例
這篇文章主要介紹了js實(shí)現(xiàn)的簡(jiǎn)練高效拖拽功能,通過對(duì)js鼠標(biāo)事件的擴(kuò)展實(shí)現(xiàn)拖拽效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-12-12
JavaScript表單即時(shí)驗(yàn)證 驗(yàn)證不成功不能提交
這篇文章主要為大家詳細(xì)介紹了JavaScript表單即時(shí)驗(yàn)證,驗(yàn)證不成功不能提交,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

