久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

js實(shí)現(xiàn)無(wú)縫輪播圖

瀏覽:98日期:2024-05-20 11:32:16

本文實(shí)例為大家分享了js實(shí)現(xiàn)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下

CSS樣式:

<style type='text/css'> #box{width: 1000px;height: 375px;border: 3px solid black; margin: 30px auto;position: relative;overflow: hidden;} #box .img{position: absolute;left: 0;top: 0;} #box .img img{width: 1000px;height: 375px;float: left;} #box .btn input{border-radius: 50%;border: 0;width: 40px; height: 40px;font-size: 25px;z-index: 5;top: 165px;position: absolute;} #left{left: 0;} #right{right: 0;}</style>

HTML結(jié)構(gòu):

<div id='box'> <div class='img'> <img src='http://www.baoyu77737.com/bcjs/img/1.jpg' > <img src='http://www.baoyu77737.com/bcjs/img/2.jpg' > <img src='http://www.baoyu77737.com/bcjs/img/3.jpg' > <img src='http://www.baoyu77737.com/bcjs/img/4.jpg' > <img src='http://www.baoyu77737.com/bcjs/img/1.jpg' > //利用第一張圖,達(dá)到無(wú)縫輪播的視覺(jué)效果 </div> <div class='btn'> <input type='button' value='<' /> <input type='button' value='>' /> </div></div>

JavaScript:

<script> class LunBO{ constructor() { this.img = document.querySelector('.img'); this.child = this.img.children; this.left = document.getElementById('left'); this.right = document.getElementById('right'); this.index = 0; } init(){ this.img.style.width = this.child.length * this.child[0].offsetWidth + 'px'; //有多少圖片設(shè)置多少寬度 } addEvent(){ var that = this; this.left.onclick = function(){ that.changeIndex(1); //點(diǎn)擊左按鈕,設(shè)置索引 } this.right.onclick = function(){ that.changeIndex(2); //點(diǎn)擊右按鈕,設(shè)置索引 } } changeIndex(i){ if(i==1){ if(this.index == 0){ this.index = this.child.length - 2; this.img.style.left = -(this.child.length-1) * this.child[0].offsetWidth + 'px'; }else{ this.index--; } }else{ if(this.index == this.child.length-1){ this.index = 1; this.img.style.left = 0; }else{ this.index++; } } this.setSplace(); } setSplace(){ move(this.img,'left',-this.index * this.child[0].offsetWidth); //調(diào)用動(dòng)畫(huà) } } var lb = new LunBO(); lb.init(); lb.addEvent();</script>

設(shè)置動(dòng)畫(huà):

function move(ele,attr,target){ clearInterval(ele.t); ele.t=setInterval(()=>{ let iNow; if(attr=='opacity'){ iNow=getStyle(ele,attr)*100; }else{ iNow=parseInt(getStyle(ele,attr)); } let speed=(target-iNow)/8; speed=speed<0?Math.floor(speed):Math.ceil(speed); if(iNow==target){ clearInterval(ele.t); }else{ if(attr=='opacity'){ ele.style.opacity=(iNow+speed)/100; }else{ ele.style[attr]=iNow+speed+'px'; } } },30)}function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; }else{ return getComputedStyle(ele,false)[attr]; }}

精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 花莲市| 郁南县| 遂平县| 长海县| 德清县| 墨玉县| 汝南县| 新宁县| 长泰县| 资溪县| 尚志市| SHOW| 邢台市| 瑞安市| 吉隆县| 永修县| 余干县| 石楼县| 大连市| 萨迦县| 工布江达县| 仙居县| 襄城县| 峨眉山市| 织金县| 永和县| 三台县| 泽州县| 宁安市| 会泽县| 绥化市| 铜陵市| 十堰市| 军事| 邻水| 六安市| 临湘市| 安陆市| 大英县| 遂宁市| 石门县|