本文實(shí)例為大家分享了移動(dòng)端輪播圖效果展示的具體代碼,供大家參考,具體內(nèi)容如下
在尋烏等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站制作,尋烏網(wǎng)站建設(shè)費(fèi)用合理。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="reset.css" rel="external nofollow" > <style> html,body{ width:100%; overflow-x:hidden; } html{ font-size:100px; } .banner{ position:relative; height:3rem; overflow:hidden; } .banner .wrapper{ position:absolute; top:0; left:-100%; height:100%; } .banner .wrapper .slide{ float:left; height:100%; background:#eee; } .banner .wrapper .slide img{ display:none; width:100%; height:100%; } .tip{ position:absolute; left:0; bottom:.1rem; width:100%; height:.16rem; text-align:center; } .tip li{ display:inline-block; margin:0 .03rem; width:.16rem; height:.16rem; background:rgba(0,0,0,0.2); border-radius:50%; vertical-align:top; } .tip li.bg{ background:#007aff; } </style> </head> <body> <section class='banner'> <div class='wrapper'> <!--實(shí)現(xiàn)無縫滾動(dòng):把第一張放末尾 最后一張放開頭--> <div class='slide'><img data-src="img/banner5.jpg" alt=""></div> <div class='slide'><img data-src="img/banner1.jpg" alt=""></div> <div class='slide'><img data-src="img/banner2.jpg" alt=""></div> <div class='slide'><img data-src="img/banner3.jpg" alt=""></div> <div class='slide'><img data-src="img/banner4.jpg" alt=""></div> <div class='slide'><img data-src="img/banner5.jpg" alt=""></div> <div class='slide'><img data-src="img/banner1.jpg" alt=""></div> </div> <ul class='tip'> <li class='bg'></li> <li></li> <li></li> <li></li> <li></li> </ul> </section> <script charset='utf-8' src='zepto.min.js'></script> <script charset='utf-8'> //REM ~function(){ document.documentElement.style.fontSize = document.documentElement.clientWidth/640*100 + 'px'; }() //頁面中如果自己使用了TOUCH MOVE等原生事件,需要把瀏覽器的默認(rèn)行為阻止掉 $(document).on('touchmove touchstart touchend',function(ev){ ev.preventDefault(); }) //BANNER var bannerRender = (function(){ var winW = document.documentElement.clientWidth, maxL = 0, minL = 0; var $banner = $('.banner'), $wrapper = $banner.children('.wrapper'), $slideList = $wrapper.children('.slide'), $imgList = $wrapper.find('img'); var step = 1, count = 0, followTimer = null; //public fn function isSwipe(strX,strY,endX,endY){ return Math.abs(endX - strX)>30 || Math.abs(endY - strY) > 30) } function swipeDir(strX,strY,endX,endY){ return Math.abs(endX - strX)>=Math.abs(endY - strY)?(endX - strX>0?'right':'left'):(endY - strY>0?'down':'up'); } //touch start function dragStart(ev){ var point = ev.touches[0]; $wrapper.attr({ strL:parseFloat($wrapper.css('left')), strX:point.clientX, strY:point.clientY, isMove:false, dir:null, changeX:null }) } //touch move function dragIng(ev){ var point = ev.touches[0]; var endX = point.clientX, endY = point.clientY, strX = parseFloat($wrapper.attr('strX')), strY = parseFloat($wrapper.attr('strY')), strL = parseFloat($wrapper.attr('strL')), changeX = endX - strX; //計(jì)算出是否滑動(dòng)以及滑動(dòng)的方向:只有是左右滑動(dòng)才進(jìn)行處理 var isMove = isSwipe(strX,strY,endX,endY), dir = swipeDir(strX,strY,endX,endY); if(isMove && /(left|right)/i.test(dir)){ $wrapper.attr({ isMove:true, dir:dir, changeX:changeX }); var curL = strL+changeX; curL = curL>maxL?maxL:(curL<minL?minL:curL); $wrapper[0].style.webkitTransitionDuration = '0s'; $wrapper.css('left',curL); } } //touch end function dragEnd(){ var isMove = $wrapper.attr('isMove'), dir = $wrapper.attr('dir'), changeX = parseFloat($wrapper.attr('changeX')); if(isMove && /(left|right)/i.test(dir)){ if(Math.abs(changeX)>=winW/2){ if(dir==='left'){ step++; }else{ step--; } } } $wrapper[0].style.webkitTransitionDuration = '.2s'; $wrapper.css('left',-step*winW); lazyImg(); //動(dòng)畫運(yùn)動(dòng)過程中,我們監(jiān)聽一個(gè)定時(shí)器:動(dòng)畫運(yùn)動(dòng)完成判斷當(dāng)前是否運(yùn)動(dòng)到邊界,如果運(yùn)動(dòng)到達(dá)了邊界,我們讓其立馬回到自己的真實(shí)位置 window.clearTimeout(followTimer) followTimer = window.setTimeout(function(){ if(step===0){ $wrapper[0].style.webkitTransitionDuration = '0s'; $wrapper.css('left',-(count-2)*winW); step = count-2; lazyImg(); } if(step===count-1){ $wrapper[0].style.webkitTransitionDuration = '0s'; $wrapper.css('left',-winW); step = 1; lazyImg(); } window.clearTimeout(followTimer) },200) } //圖片延遲加載,讓當(dāng)前的活動(dòng)塊及相鄰的兩個(gè)活動(dòng)塊進(jìn)行加載 function lazyImg(){ var $cur = $slideList.eq(step), $tar = $cur.add($cur.prev()).add($cur.next()); $tar.each(function(index,item){ var $img = $(item).children('img'); if($img.attr('isLoad')==='true'){ //ATTR存儲(chǔ)或者獲取的屬性值都是一個(gè)字符串,如果當(dāng)前的圖片已經(jīng)加載過了,我們就不需要重新的加載了 return; } var oImg = new Image; oImg.src = $img.attr('data-src'); oImg.onload = function(){ $img.attr({ src:this.src, isLoad:true }).css('display','block') oImg = null; } }) } return{ init:function(){ //init css style count = $slideList.length; minL = -($slideList.length-1)*winW; $wrapper.css('width',$slideList.length*winW); $slideList.css('width',winW); //lazy img lazyImg(); $banner.on('touchstart',dragStart).on('touchmove',dragIng).on('touchend',dragEnd) } } })() bannerRender.init(); </script> </body> </html>
邊界判斷邏輯可參照下圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文名稱:js實(shí)現(xiàn)移動(dòng)端輪播圖效果
網(wǎng)頁URL:http://jinyejixie.com/article38/ghddpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)、小程序開發(fā)、App設(shè)計(jì)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)