欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年05月09日 17:29:54   作者:le__seul  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)無(wú)縫輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用原生js實(shí)現(xiàn)無(wú)縫輪播圖,供大家參考,具體內(nèi)容如下

index.js:

var config = {
 imgWidth:380,//圖片尺寸
 dotWidth:8,//小圓點(diǎn)尺寸
 doms:{
 divImgs:document.querySelector('.imgs'),
 divDots:document.querySelector('.circle'),
 divDirection:document.querySelector('.direction'),
 divContainer:document.querySelector('.container')
 },
 curIndex:0,//實(shí)際圖片索引,0 ~ imgNumber-1
 timer:{
 duration:16,//運(yùn)動(dòng)間隔時(shí)間
 total:1000,//總時(shí)間
 id:null//計(jì)時(shí)器編號(hào)
 }
}
//圖片的數(shù)量
config.imgNumber = config.doms.divImgs.children.length;
//初始化元素尺寸
config.imgsWidth = (config.imgNumber + 2)*config.imgWidth;
config.dotsWidth = (config.imgNumber + 2)*config.dotWidth;

//初始化
function inti(){
 intiWidth();
 intiCount();
 intiElement();
 intiPosition();
 function intiWidth(){
 config.doms.divImgs.style.width = config.imgsWidth + 'px';
 config.doms.divDots.style.width = config.dotsWidth + 'px';
 }
 function intiCount(){
 for(var i = 0; i < config.imgNumber; i ++){
 var p = document.createElement('p');
 config.doms.divDots.appendChild(p);
 }
 }
 function intiElement(){
 var first = config.doms.divImgs.children[0],last = config.doms.divImgs.children[config.imgNumber-1];
 var newImg = first.cloneNode(true);//深度克隆
 config.doms.divImgs.appendChild(newImg);
 newImg = last.cloneNode(true);
 config.doms.divImgs.insertBefore(newImg,first);
 }
 function intiPosition(){
 var left = (-config.curIndex-1)*config.imgWidth;
 config.doms.divImgs.style.marginLeft = left + 'px';
 setDots();//小圓點(diǎn)的激活狀態(tài)位置設(shè)置
 }
}
inti();

//小圓點(diǎn)的激活狀態(tài)位置設(shè)置
function setDots(){
 for(var i = 0; i < config.doms.divDots.children.length; i++){
 var dot = config.doms.divDots.children[i];
 if(i === config.curIndex){
 dot.className = 'select';
 }else{
 dot.className = '';
 }
 }
}

/*
 圖片切換
 index: 圖片索引
 directions: 圖片切換方向(left,right)
*/
function switchTo(index,directions){
 if(index === config.curIndex){
 return;
 }
 if(!directions){
 directions = 'right';//默認(rèn)狀態(tài)下向右切換圖片
 }

 //最終的顯示圖片; 圖片容器的marginLeft
 var newLeft = (-index-1)*config.imgWidth;
 animateSwitch();
 //config.doms.divImgs.style.marginLeft = newLeft + 'px';
 
 //小圓點(diǎn)的激活狀態(tài)位置設(shè)置
 config.curIndex = index;
 setDots();

 //一張圖片的總運(yùn)動(dòng)次數(shù)
 var number = Math.ceil(config.timer.total/config.timer.duration);
 //當(dāng)前運(yùn)動(dòng)次數(shù)
 var curNumber = 0;
 
 var distance,//總運(yùn)動(dòng)距離
 totalWidth = config.imgNumber*config.imgWidth,
 marginLeft = parseFloat(getComputedStyle(config.doms.divImgs).marginLeft);
 if(directions === 'left'){
 if(newLeft < marginLeft){
 distance = newLeft - marginLeft;
 }else{
 distance = -(totalWidth-Math.abs(newLeft - marginLeft));
 }
 }
 if(directions === 'right'){
 if(newLeft > marginLeft){
 distance = newLeft - marginLeft;
 }else{
 distance = totalWidth-Math.abs(newLeft - marginLeft);
 }
 } 

 //每次改變的距離
 var everDistence = distance/number; 

 //逐步改變marginLeft
 function animateSwitch(){
 clearAnimate();
 config.timer.id = setInterval(function(){

 marginLeft += everDistence;
 if(directions === 'left' && Math.abs(marginLeft) > totalWidth){
 marginLeft += totalWidth;
 }
 else if(directions === 'right' && Math.abs(marginLeft) < config.imgWidth){
 marginLeft -= totalWidth;
 }
 config.doms.divImgs.style.marginLeft = marginLeft + 'px';

 curNumber ++;
 if(curNumber === number){
 clearAnimate();
 }
 },config.timer.duration);
 }

 //清空計(jì)時(shí)器
 function clearAnimate(){
 clearInterval(config.timer.id);
 config.timer.id = null;
 }
}

//默認(rèn)情況下自動(dòng)向右輪播圖片
var timer = setInterval(function(){
 toRight();
},2000);
config.doms.divContainer.onmouseleave = function() {
 timer = setInterval(function(){
 toRight();
 },2000);
}
//鼠標(biāo)移出則清空定時(shí)器
config.doms.divContainer.onmouseover = function() {
 clearInterval(timer);
}

//左右點(diǎn)擊事件
config.doms.divDirection.onclick = function(e){
 clearInterval(timer);
 if(e.target.classList.contains('left')){
 toLeft();
 }
 if(e.target.classList.contains('right')){
 toRight();
 }
}

function toLeft(){
 var index = config.curIndex - 1;
 if(index < 0){
 index = config.imgNumber - 1;
 }
 switchTo(index,'right');
}
function toRight(){
 var index = config.curIndex + 1;
 if(index > config.imgNumber - 1){
 index = 0;
 }
 switchTo(index,'left');
}

//小圓點(diǎn)點(diǎn)擊事件
config.doms.divDots.onclick = function(e){
 if(e.target.tagName === 'P'){
 var index = Array.from(this.children).indexOf(e.target);
 switchTo(index,index > config.curIndex? 'left' : 'right')
 }
}

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>無(wú)縫輪播圖</title>
 <link rel="stylesheet" href="index.css" rel="external nofollow" >
</head>
<body>
 <div class="container">
 <div class="imgs">
 <img class="item" src="../imagejpg/1.jpg" alt="">
 <img class="item" src="../imagejpg/2.jpg" alt="">
 <img class="item" src="../imagejpg/3.jpg" alt="">
 </div>
 <div class="circle">
 <!-- <p></p>
 <p class="select"></p>
 <p></p>
 <p></p>
 <p></p> -->
 </div>
 <div class="direction">
 <div class="item left">&lt;</div>
 <div class="item right">&gt;</div>
 </div>
 </div>
 <script src="./index.js">
 
 </script>
</body>
</html>

index.css:

.container{
 width:380px;
 height:250px;
 border:1px solid;
 margin:0 auto;
 position:relative;
 overflow: hidden;
}
.container .imgs{
 
}
.container .imgs .item{
 width:380px;
 height:250px;
 display:block;
 float:left;
 top:0;
}
.container .circle{
 position:absolute;
 left:0;
 right:0;
 margin:0 auto;
 background:rgba(0,0,0,.3);
 bottom:8px;
 border-radius:5px;
}
.container .circle p{
 width:8px;
 height:8px;
 background:#fff;
 border-radius:50%;
 float:left;
 margin:2px;
 cursor:pointer;
}
.container .circle p.select{
 background:#f40;
}
.container .direction .item{
 background:rgba(0,0,0,.4);
 position:absolute;
 top:120px;
 width:20px;
 height:26px;
 padding:2px;
 box-sizing:border-box;
 display:none;
 cursor:pointer;
}
.container .direction .item:hover{
 background:rgba(0,0,0,.5);
}
.container:hover .direction .item{
 display:block;
}
.container .direction .left{
 left:0;
 border-radius:0 15px 15px 0;
}
.container .direction .right{
 right:0;
 padding-left:6px;
 border-radius:15px 0 0 15px;
}

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

相關(guān)文章

  • JS幻燈片可循環(huán)播放可平滑旋轉(zhuǎn)帶滾動(dòng)導(dǎo)航(自寫)

    JS幻燈片可循環(huán)播放可平滑旋轉(zhuǎn)帶滾動(dòng)導(dǎo)航(自寫)

    本文為大家介紹下實(shí)現(xiàn)JS幻燈片可循環(huán)播放帶滾動(dòng)導(dǎo)航可平滑旋轉(zhuǎn)的全過程,效果還不錯(cuò),由需要的朋友可以參考下,希望對(duì)大家有所幫助
    2013-08-08
  • JavaScript常用數(shù)組去重的方法及對(duì)比詳解

    JavaScript常用數(shù)組去重的方法及對(duì)比詳解

    數(shù)組去重在面試和工作中都是比較容易見到的問題。這篇文章主要是來測(cè)試多個(gè)方法,對(duì)下面這個(gè)數(shù)組的去重結(jié)果進(jìn)行分析討論,需要的可以參考一下
    2022-07-07
  • LayerClose彈窗關(guān)閉刷新方法

    LayerClose彈窗關(guān)閉刷新方法

    今天小編就為大家分享一篇LayerClose彈窗關(guān)閉刷新方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • JavaScript檢查數(shù)字是否為整數(shù)或浮點(diǎn)數(shù)的方法

    JavaScript檢查數(shù)字是否為整數(shù)或浮點(diǎn)數(shù)的方法

    這篇文章主要介紹了JavaScript檢查數(shù)字是否為整數(shù)或浮點(diǎn)數(shù)的方法,涉及javascript類型判斷的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • JavaScript回調(diào)函數(shù)callback用法解析

    JavaScript回調(diào)函數(shù)callback用法解析

    這篇文章主要介紹了JavaScript回調(diào)函數(shù)callback用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 微信小程序去除左上角返回鍵的實(shí)現(xiàn)方法

    微信小程序去除左上角返回鍵的實(shí)現(xiàn)方法

    這篇文章主要介紹了微信小程序去除左上角返回鍵的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • es6函數(shù)之箭頭函數(shù)用法實(shí)例詳解

    es6函數(shù)之箭頭函數(shù)用法實(shí)例詳解

    這篇文章主要介紹了es6函數(shù)之箭頭函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了es6箭頭函數(shù)基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • js實(shí)現(xiàn)交換運(yùn)動(dòng)效果的方法

    js實(shí)現(xiàn)交換運(yùn)動(dòng)效果的方法

    這篇文章主要介紹了js實(shí)現(xiàn)交換運(yùn)動(dòng)效果的方法,涉及javascript操作頁(yè)面元素與相關(guān)樣式實(shí)現(xiàn)交換運(yùn)動(dòng)效果的技巧,需要的朋友可以參考下
    2015-04-04
  • JavaScript指定斷點(diǎn)操作實(shí)例教程

    JavaScript指定斷點(diǎn)操作實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于JavaScript指定斷點(diǎn)操作的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • JavaScript設(shè)計(jì)模式之單例模式實(shí)例

    JavaScript設(shè)計(jì)模式之單例模式實(shí)例

    這篇文章主要介紹了JavaScript設(shè)計(jì)模式之單例模式實(shí)例,本文用一個(gè)實(shí)際例子講解JavaScript中的單例模式,需要的朋友可以參考下
    2014-09-09

最新評(píng)論