js 이동 단 윤 방도 효과 실현
<!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'>
 <!--      :               -->
 <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     ,              
 $(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;
 //              :            
 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();
 //       ,         :                 ,         ,               
 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)
 }
 //      ,                    
 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                 ,             ,            
 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>
경계 판단 논 리 는 아래 그림 을 참조 할 수 있다.
 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.