JS實(shí)現(xiàn)圖片自動(dòng)播放效果
本文實(shí)例為大家分享了JS實(shí)現(xiàn)圖片自動(dòng)播放效果的具體代碼,供大家參考,具體內(nèi)容如下
JS實(shí)現(xiàn)圖片自動(dòng)播放
1、先看效果圖

2、完整代碼
<!DOCTYPE html>
<html>
<head>
<style>
/* 定義樣式 */
body{
margin: 5% 30%;
}
.bannerimage{width:700px;height:400px;float:left;background-size:100% 100%;color:#fff;box-shadow: 0 0 12px 2px #142732;}
.box{width:700px;height:400px;margin:0px auto;overflow: hidden;}
/* box的寬度為img的個(gè)數(shù)乘以bannerimage的寬度*/
.img-g{width:4900px;height:400px;position:relative;}
.img-g img{float:left;width:700px;height:400px;}
.button-g{position:relative;top:-35px;text-align:center;}
.button-g span{display:inline-block;position:relative;z-index:10;width:10px;height:10px;margin:0 5px;border-radius:100%;cursor: pointer;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function () {
// 實(shí)現(xiàn)自動(dòng)播放
var p=document.getElementsByClassName('img-g')[0];
var button=document.querySelectorAll('.button-g span')
// 設(shè)置3秒播放
window.timer=setInterval(move,3000);
// 輪播設(shè)置
function move(){
// bannerimage的寬度乘以圖片的個(gè)數(shù)
if(parseInt(p.style.left)>-4200){
// 和bannerimage的寬度保持一致即可:700
p.style.left=parseInt(p.style.left)-700+'px'
p.style.transition='left 1s';
tog(-Math.round(parseInt(p.style.left)/700))
if(parseInt(p.style.left)<=-4200){
setTimeout(function(){
tog(0)
p.style.left='0px'
p.style.transition='left 0s';
},1000)
}
}else{
p.style.left='0px'
p.style.transition='left 0s';
}
}
// 設(shè)置小圓點(diǎn)
for(var i=0;i<button.length;i++){
// button[i].style.backgroundColor='#eee';
button[i].onclick=function(){
p.style.left=-700*this.getAttribute('data-index')+'px'
tog(this.getAttribute('data-index'))
clearInterval(window.timer)
window.timer=setInterval(move,3000);
}
}
// 設(shè)置小圓點(diǎn)
function tog(index){
if(index>5){
tog(0);
return;
}
for(var i=0;i<button.length;i++){
button[i].style.backgroundColor='rgba(255, 255, 255, 0.5)';
}
button[index].style.backgroundColor='rgb(255, 255, 255)';
}
// 鼠標(biāo)移上事件
p.onmouseover=function(){
clearInterval(window.timer)
}
// 鼠標(biāo)移除事件
p.onmouseout=function(){
window.timer=setInterval(move,3000);
}
});
</script>
</head>
<body>
<div class="bannerimage">
<div class='box'>
<div class='img-g' style='left:0px;transition:left 1s;'>
<img src="images/img_1.jpg" alt="1">
<img src="/images/img_2.jpg" alt="2">
<img src="/images/img_3.jpg" alt="3">
<img src="/images/img_4.jpg" alt="4">
<img src="/images/img_5.jpg" alt="5">
<img src="/images/img_6.jpg" alt="6">
<img src="/images/img_1.jpg" alt="1">
</div>
<div class='button-g'>
<span data-index='0' style="background-color: #eeeeee"></span>
<span data-index='1' style="background-color: rgba(255, 255, 255, 0.5)"></span>
<span data-index='2' style="background-color: rgba(255, 255, 255, 0.5)"></span>
<span data-index='3' style="background-color: rgba(255, 255, 255, 0.5)"></span>
<span data-index='4' style="background-color: rgba(255, 255, 255, 0.5)"></span>
<span data-index='5' style="background-color: rgba(255, 255, 255, 0.5)"></span>
</div>
</div>
</div>
</body>
</html>
3、關(guān)鍵代碼講解
3.1、css設(shè)置注意事項(xiàng):img-g的寬度為:img的個(gè)數(shù)乘以bannerimage的寬度,如下:
.img-g{width:4900px;height:400px;position:relative;}
3.2、輪播常量及事件設(shè)置
常量1設(shè)置為:bannerimage的寬度乘以圖片的個(gè)數(shù),如下:
if(parseInt(p.style.left)>-4200){}
常量2設(shè)置為:bannerimage的寬度保持一致即可(700),如下:
p.style.left=parseInt(p.style.left)-700+'px'
小圓點(diǎn)顯示設(shè)置:
// 設(shè)置小圓點(diǎn)
for(var i=0;i<button.length;i++){
button[i].style.backgroundColor='#eee';
button[i].onclick=function(){
p.style.left=-700*this.getAttribute('data-index')+'px'
tog(this.getAttribute('data-index'))
clearInterval(window.timer)
window.timer=setInterval(move,3000);
}
}
// 設(shè)置小圓點(diǎn)點(diǎn)擊事件
function tog(index){
// 圓點(diǎn)的個(gè)數(shù)
if(index>5){
tog(0);
return;
}
for(var i=0;i<button.length;i++){
// 默認(rèn)圓點(diǎn)的顯示顏色
button[i].style.backgroundColor='rgba(255, 255, 255, 0.5)';
}
// 當(dāng)前圓點(diǎn)的顯示顏色
button[index].style.backgroundColor='rgb(255, 255, 255)';
}
鼠標(biāo)事件:鼠標(biāo)移上停止播放,移除3秒后播放。
// 鼠標(biāo)移上事件
p.onmouseover=function(){
clearInterval(window.timer)
}
// 鼠標(biāo)移除事件
p.onmouseout=function(){
window.timer=setInterval(move,3000);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js手動(dòng)播放圖片實(shí)現(xiàn)圖片輪播效果
- JS實(shí)現(xiàn)的多張圖片輪流播放幻燈片效果
- javascript控制圖片播放的實(shí)現(xiàn)代碼
- JS特效實(shí)現(xiàn)圖片自動(dòng)播放并可控的效果
- javascript+html5實(shí)現(xiàn)仿flash滾動(dòng)播放圖片的方法
- javascript實(shí)現(xiàn)圖片循環(huán)漸顯播放的方法
- js實(shí)現(xiàn)幻燈片播放圖片示例代碼
- javascript 控制圖片播放代碼
- 比較炫的圖片播放器 js 焦點(diǎn)效果代碼
- js 新浪的一個(gè)圖片播放圖片輪換效果代碼
相關(guān)文章
js判斷當(dāng)頁(yè)面無法回退時(shí)關(guān)閉網(wǎng)頁(yè)否則就history.go(-1)
當(dāng)頁(yè)面沒有前驅(qū)歷史記錄時(shí),點(diǎn)擊返回按鈕時(shí)直接關(guān)閉頁(yè)面,否則就退回到前一頁(yè)2014-08-08
微信小程序?qū)崿F(xiàn)canvas分享朋友圈海報(bào)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)canvas分享朋友圈海報(bào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
js實(shí)現(xiàn)局部頁(yè)面打印預(yù)覽原理及示例代碼
js 如何打印預(yù)覽,實(shí)局部打印頁(yè)面很簡(jiǎn)單。就是把你需要打印的部分做一個(gè)起始標(biāo)記,下面有個(gè)示例大大家不妨參考下2014-07-07
詳解Webpack如何引入CDN鏈接來優(yōu)化編譯后的體積
這篇文章主要介紹了詳解Webpack如何引入CDN鏈接來優(yōu)化編譯后的體積,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
JavaScript之事件委托實(shí)例(附原生js和jQuery代碼)
下面小編就為大家?guī)硪黄狫avaScript之事件委托實(shí)例(附原生js和jQuery代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
如何使用less實(shí)現(xiàn)隨機(jī)下雪動(dòng)畫詳解
這篇文章主要給大家介紹了關(guān)于如何使用less實(shí)現(xiàn)隨機(jī)下雪動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
JS的encodeURI和java的URLDecoder.decode使用介紹
如果不想在url中看到有明文可以使用js的encodeURI的URLDecoder.decode一起使用一起來把url加密下,下面有個(gè)不錯(cuò)的示例,大家不妨參考下2014-05-05

