原生js輪播(仿慕課網(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">我是藍(lán)色第三張</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>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
JavaScript中setInterval()和setTimeout()的用法及區(qū)別
這篇文章主要給大家介紹了關(guān)于JavaScript中setInterval()和setTimeout()用法及區(qū)別的相關(guān)資料,Javascript的setTimeOut和setInterval函數(shù)應(yīng)用非常廣泛,它們都用來(lái)處理延時(shí)和定時(shí)任務(wù),需要的朋友可以參考下2023-11-11
微信小程序?qū)崿F(xiàn)animation動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)animation動(dòng)畫(huà)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
js操作輸入框提示信息且響應(yīng)鼠標(biāo)事件
注冊(cè)網(wǎng)站的輸入框就有默認(rèn)提示值,當(dāng)獲取鼠標(biāo)焦點(diǎn)的時(shí)候,默認(rèn)值被刪除,當(dāng)用戶沒(méi)輸入東西焦點(diǎn)離開(kāi)的時(shí)候,又恢復(fù)默認(rèn)提示值2014-03-03
JavaScript將坐標(biāo)字符串轉(zhuǎn)為數(shù)組的項(xiàng)目實(shí)踐
本文主要介紹了JavaScript將坐標(biāo)字符串轉(zhuǎn)為數(shù)組的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
javascript css在IE和Firefox中區(qū)別分析
我們討論的主題CSS網(wǎng)頁(yè)布局,最令大家頭疼的問(wèn)題就是瀏覽器兼容性,雖然52CSS.com介紹過(guò)很多這方向的知識(shí),但依然讓很多開(kāi)發(fā)人員暈頭轉(zhuǎn)向,今天的這篇文章,將列出css和javascript在IE和Firefox中二十三個(gè)不同點(diǎn),希望對(duì)大家的學(xué)習(xí)有所幫助。2009-02-02
用js將long型數(shù)據(jù)轉(zhuǎn)換成date型或datetime型的實(shí)例
下面小編就為大家?guī)?lái)一篇用js將long型數(shù)據(jù)轉(zhuǎn)換成date型或datetime型的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
JS?中的類(lèi)Public,Private?和?Protected詳解
這篇文章主要介紹了JS中的類(lèi)Public,Private和Protected詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
javascript實(shí)現(xiàn)獲取一個(gè)日期段內(nèi)每天不同的價(jià)格(計(jì)算入住總價(jià)格)
這篇文章主要介紹了javascript實(shí)現(xiàn)獲取一個(gè)日期段內(nèi)每天不同的價(jià)格(計(jì)算入住總價(jià)格)的相關(guān)資料,需要的朋友可以參考下2018-02-02
localStorage實(shí)現(xiàn)便簽小程序
這篇文章主要為大家詳細(xì)介紹了localStorage實(shí)現(xiàn)便簽小程序的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
JS獲取鼠標(biāo)坐標(biāo)的實(shí)例方法
這篇文章介紹了JS獲取鼠標(biāo)坐標(biāo)的實(shí)例方法,有需要的朋友可以參考一下2013-07-07

