原生js輪播(仿慕課網(wǎng))
更新時間:2017年02月15日 10:33:50 作者:完美續(xù)航
本文主要分享了原生js實現(xiàn)仿慕課網(wǎng)的輪播效果。具有很好的參考價值,下面跟著小編一起來看下吧
效果如下:

代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript</title>
<style>
*{margin:0;padding:0;border:0;}
a{text-decoration:none;color:#fff;font-size:40px;line-height:200px;display:none;text-align:center;}
#container{width:300px;height:200px;margin:50px auto;position:relative;overflow:hidden;}
#list{width:2100px;height:200px;position:absolute;top:0;}
#list span{width:300px;height:200px;display:inline-block;text-align:center;font-size:22px;float:left;color:#fff;}
.one{background:red;}
.two{background:orange;}
.three{background:blue;}
.four{background:green;}
.five{background:black;}
#buttons{width:200px;height:30px;position:absolute;bottom:0px;left:100px;z-index:9;}
#buttons span{display:inline-block;cursor:pointer;width:12px;height:12px;border-radius:6px;background: #2a2a2a}
#prev{width:40px;height:200px;position:absolute;left:0px;}
#next{width:40px;height:200px;position:absolute;right:0px;}
#container .on{background:#fff;}
</style>
</head>
<body>
<div id="container">
<div id="list" style="left:-300px">
<span class="five">我是黑色第五張</span>
<span class="one">我是紅色第一張</span>
<span class="two">我是黃色第二張</span>
<span class="three">我是藍色第三張</span>
<span class="four">我是綠色第四張</span>
<span class="five">我是黑色第五張</span>
<span class="one">我是紅色第一張</span>
</div>
<div id="buttons">
<span class="on" index="1"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a id="prev" href="javascript:;" rel="external nofollow" rel="external nofollow" ><</a>
<a id="next" href="javascript:;" rel="external nofollow" rel="external nofollow" >></a>
</div>
<script>
var container = document.getElementById('container'),
list = document.getElementById('list'),
buttons = document.getElementById('buttons').getElementsByTagName('span'),
prev = document.getElementById('prev'),
next = document.getElementById('next'),
index = 1,
len = 5,
interval = 3000,
animated = false,
timer;
function animate(offset){
if(offset == 0) return;
animated = true;
var time = 150,
inter = 5,
speed = offset/(time/inter),
left = parseInt(list.style.left) + offset;
var go = function(){
if((speed>0 && parseInt(list.style.left)<left) || (speed<0 && parseInt(list.style.left)>left)){
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go,inter);
}else{
list.style.left = left + 'px';
if(left > -100){
list.style.left = -300*len + 'px';
}
if(left < (-300*len)){
list.style.left = '-300px'
}
animated = false;
}
}
go();
}
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++;
}
animate(-300);
showButton();
}
prev.onclick = function(){
if(animated) {
return;
}
if(index == 1){
index = 5;
}else{
index--;
}
animate(300);
showButton();
}
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function () {
if (animated) {
return;
}
if(this.className == 'on') {
return;
}
var myIndex = parseInt(this.getAttribute('index'));
var offset = -300 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
container.onmouseover = function(){
prev.style.display = next.style.display = 'block';
stop();
}
container.onmouseout = function(){
prev.style.display = next.style.display = 'none';
play();
}
play();
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
JavaScript中setInterval()和setTimeout()的用法及區(qū)別
這篇文章主要給大家介紹了關(guān)于JavaScript中setInterval()和setTimeout()用法及區(qū)別的相關(guān)資料,Javascript的setTimeOut和setInterval函數(shù)應(yīng)用非常廣泛,它們都用來處理延時和定時任務(wù),需要的朋友可以參考下2023-11-11
JavaScript將坐標字符串轉(zhuǎn)為數(shù)組的項目實踐
本文主要介紹了JavaScript將坐標字符串轉(zhuǎn)為數(shù)組的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
javascript css在IE和Firefox中區(qū)別分析
我們討論的主題CSS網(wǎng)頁布局,最令大家頭疼的問題就是瀏覽器兼容性,雖然52CSS.com介紹過很多這方向的知識,但依然讓很多開發(fā)人員暈頭轉(zhuǎn)向,今天的這篇文章,將列出css和javascript在IE和Firefox中二十三個不同點,希望對大家的學(xué)習(xí)有所幫助。2009-02-02
用js將long型數(shù)據(jù)轉(zhuǎn)換成date型或datetime型的實例
下面小編就為大家?guī)硪黄胘s將long型數(shù)據(jù)轉(zhuǎn)換成date型或datetime型的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
JS?中的類Public,Private?和?Protected詳解
這篇文章主要介紹了JS中的類Public,Private和Protected詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
javascript實現(xiàn)獲取一個日期段內(nèi)每天不同的價格(計算入住總價格)
這篇文章主要介紹了javascript實現(xiàn)獲取一個日期段內(nèi)每天不同的價格(計算入住總價格)的相關(guān)資料,需要的朋友可以參考下2018-02-02

