IOS设备上网页中的页面滚动效果模拟

可能咋一看不知道我说的是个啥,因为iOS本来就用这功能的啊,还模拟它干啥?先听我说下项目背景哈

我现在开发的是一个webapp,主要是用在ipad上,这个app的大小是固定大小的,为了防止触摸它出现弹性滚动,我加个句代码:

[javascript] view plain copy
 
  1. $(document).bind('touchmove', function(e) {           
  2.     e.preventDefault();  
  3. });  


这样这个页面就被我锁死了,不会出现讨厌的弹性滚动了,再加上一些meta属性(我的blog里有这些)全屏啥的,基本上跟nativeapp无异了。

但是呢,问题出来了,里面有个div里面内容比较多,需要用到滚动条,现在的问题是,我的网页里设置这个div的overflow为scroll后,触摸这个div不能滚动了!我先试着把上面的代码注释点,发现可以滚动了,但是效果很差,没有ios自带的那种手离开后还会滚动一会的那种“刹车”的效果,于是呢,就想着自己搞一个出来,于是就写了一个jQuery插件,实现了上下左右滑动和轻拍(因为点击事件在ipad上有半秒延迟),下面会有下载地址。关于滚动效果呢,是我花了一整天时间研究出来的,觉得收获很大,所以拿出来跟大家分享下,以下是一些核心代码:

[javascript] view plain copy
 
  1. (function show(time){         
  2.                             if(!source.animation){return;}  
  3.                             source.animation = window.setTimeout(function(){  
  4.                                 if(time > 90){ return; }                               
  5.                                 X && $self.scrollLeft($self.scrollLeft() - ha(time,speedX) * aa );  
  6.                                 Y && $self.scrollTop($self.scrollTop() - ha(time,speedY) * aa );                                  
  7.                                 show(++time);  
  8.                             },aa);  
  9.                         })(1);  


函数ha:

[javascript] view plain copy
 
  1. function ha(x,maxSpeed){  
  2.                             //return maxSpeed / x;//y = 100 / x;  
  3.                             //return maxSpeed - maxSpeed / 100 * x;// y = -x + 100;  
  4.                             return (1 - Math.sqrt(10000-(x-100)*(x-100)) / 100) * maxSpeed ;//y = -sqrt(10000-(x-100)2) + 100  
  5.                         }  

上面第一段代码的意思是在手指离开屏幕之后要执行的一个连续动画,可以看到,随着时间速度越来越慢,速度的变化由函数ha决定,一开始我用y = 1/x的速度,发现速度变化太快,动画执行完一半就基本上等于结束了,后来就是在慢慢的蹭一秒多,县的很不流畅。。然后就换,换成了第二个,y = -x + 100,这个效果好一点了,但是太流畅了。。刷一下飞好远。。用aa调整灵敏度后效果还是差强人意,于是看了半天苹果自带的效果,发现速度在由快变慢的过程中,加速度绝对值的变化程度好像是小--大--小的一个状态,于是我就试着用y = x2 - 10x(具体多少忘记了)之类的函数去试,效果还好,不过还是有点快,最后我选中了四分之一圆,这个弧度性感又均衡,在灵敏度调整为20后,终于实现了和ios相差无几的滚动效果,心里那个高兴啊。。呵呵,。

由于代码不长,就直接贴这里了,简单的API如下:

$(selecor).swipeleft(fn).swiperight(fn).swipeup(fn).swipedown(fn).tap(fn).scrollX().scrollY().scroll('xy');注意滚动和swipe会冲突,尽量避免一起使用~

jQueqy.tomTouch.js

[javascript] view plain copy
 
    1. (function($){  
    2.             //if (!document.documentElement.ontouchstart){return;}  
    3.               
    4.             var events = "touchmove,tap,swiperight,swipeleft,swipeup,swipedown";  
    5.               
    6.             var proto = function($e,type,fn){         
    7.                 this.$e = $e;         
    8.                 this.type = type;  
    9.                 this.fn = fn;  
    10.             };  
    11.             proto.prototype = {  
    12.                 swipeDistance:40,  
    13.                 startX:0,  
    14.                 startY:0,  
    15.                 tempX:0,  
    16.                 tempY:0,  
    17.                 startTime:undefined,  
    18.                 animation:undefined,  
    19.                   
    20.                 touchmove:function(e){            
    21.                     var self = e.data;                    
    22.                     e = e.originalEvent;  
    23.                     if (e.targetTouches.length >= 1) {  
    24.                         var touch = e.targetTouches[0];                   
    25.                                                   
    26.                         if(self._touchmove){                              
    27.                             self.fn.touchmove.call(this,{  
    28.                                 deltaX:touch.pageX - self.tempX,  
    29.                                 deltaY:touch.pageY - self.tempY,  
    30.                                 x:touch.pageX,  
    31.                                 y:touch.pageY,  
    32.                                 tomSource:self  
    33.                             });  
    34.                         }  
    35.                           
    36.                         self.tempX = touch.pageX;  
    37.                         self.tempY = touch.pageY;  
    38.                     }  
    39.                 },  
    40.                 touchstart:function(e){  
    41.                     var self = e.data;                    
    42.                     e = e.originalEvent;  
    43.                     if (e.targetTouches.length >= 1) {  
    44.                         var touch = e.targetTouches[0];       
    45.                         self.tempX = self.startX = touch.pageX;  
    46.                         self.tempY = self.startY = touch.pageY;                       
    47.                         self.animation = undefined;  
    48.                         self.startTime = +new Date;  
    49.                         self.fn.touchstart &&   
    50.                         self.fn.touchstart.call(this,{x:e.pageX,y:e.pageY,tomSource:self});  
    51.                     }                     
    52.                 },  
    53.                 touchend:function(e){                     
    54.                     var self = e.data;                    
    55.                     e = e.originalEvent;                      
    56.                       
    57.                     if (e.changedTouches.length >= 1) {  
    58.                         self.animation = true;  
    59.                         var touch = e.changedTouches[0]  
    60.                            ,now = +new Date()  
    61.                            ,dX = touch.pageX - self.startX  
    62.                            ,dY = touch.pageY - self.startY  
    63.                            ,AdX = Math.abs(dX)  
    64.                            ,AdY = Math.abs(dY)  
    65.                            ,timeD = now - self.startTime;  
    66.                              
    67.                         if(  
    68.                             (timeD < 100 && AdX < 15 && AdY < 15 && self._tap) ||   
    69.                             (dX > self.swipeDistance && AdX > AdY && self._swiperight) ||  
    70.                             (-dX > self.swipeDistance && AdX > AdY && self._swipeleft) ||  
    71.                             (dY > self.swipeDistance && AdY > AdX && self._swipedown)  ||  
    72.                             (-dY > self.swipeDistance && AdY > AdX && self._swipeup)){                              
    73.                             self.fn.call(this,{});  
    74.                         }                     
    75.                           
    76.                         var speedX = dX / timeD;  
    77.                         var speedY = dY / timeD;  
    78.                         //d(self.startY + "," + touch.pageY);  
    79.                         self.fn.touchend &&  
    80.                         self.fn.touchend.call(this,{  
    81.                             x:touch.pageX,  
    82.                             y:touch.pageY,  
    83.                             speedX:speedX,  
    84.                             speedY:speedY,  
    85.                             tomSource:self  
    86.                         });                           
    87.                     }  
    88.                 },  
    89.                 handle:function(){  
    90.                     var self = this;  
    91.                       
    92.                     $.each(events.split(',')  
    93.                         ,function(i,item){  
    94.                             if(item == self.type){  
    95.                                 self[ "_" + item ] = true;  
    96.                             }  
    97.                     });  
    98.                       
    99.                     self.$e.bind("touchmove",self,self.touchmove);  
    100.                     self.$e.bind("touchstart",self,self.touchstart);  
    101.                     self.$e.bind("touchend",self,self.touchend);  
    102.                 }  
    103.             };            
    104.               
    105.             $.each(events.split(","),function(i,name){  
    106.                 $.fn[name] = function(fn){  
    107.                     var touches = new proto($(this),name,fn);     
    108.                     touches.handle();  
    109.                     return $(this);  
    110.                 }                 
    111.             });           
    112.               
    113.             $.fn.touchScroll = function(direction){               
    114.                 var X = /x/gi.test(direction)  
    115.                    ,Y = /y/gi.test(direction)  
    116.                    ,self = this  
    117.                    ;  
    118.                   
    119.                 $(this).touchmove({  
    120.                     touchmove:function(ex){                           
    121.                         X && $(this).scrollLeft($(this).scrollLeft() - ex.deltaX);  
    122.                         Y && $(this).scrollTop($(this).scrollTop() - ex.deltaY);                          
    123.                     },  
    124.                     touchend:function(e){  
    125.                         var $self = $(this)  
    126.                            ,timeDuration = 3000  
    127.                            ,aa = 20  
    128.                            ,speedX = e.speedX  
    129.                            ,speedY = e.speedY  
    130.                            ,source = e.tomSource  
    131.                            ;                          
    132.                         //d(speedY);  
    133.                         ///*  
    134.                         (function show(time){         
    135.                             if(!source.animation){return;}  
    136.                             source.animation = window.setTimeout(function(){  
    137.                                 if(time > 90){ return; }                               
    138.                                 X && $self.scrollLeft($self.scrollLeft() - ha(time,speedX) * aa );  
    139.                                 Y && $self.scrollTop($self.scrollTop() - ha(time,speedY) * aa );                                  
    140.                                 show(++time);  
    141.                             },aa);  
    142.                         })(1);  
    143.                           
    144.                         function ha(x,maxSpeed){  
    145.                             //return maxSpeed / x;//y = 100 / x;  
    146.                             //return maxSpeed - maxSpeed / 100 * x;// y = -x + 100;  
    147.                             return (1 - Math.sqrt(10000-(x-100)*(x-100)) / 100) * maxSpeed ;//y = -sqrt(10000-(x-100)2) + 100  
    148.                         }  
    149.                           
    150.                     }  
    151.                 });               
    152.                   
    153.                 return $(this);  
    154.             }  
    155.             $.fn.touchScrollX = function(){$(this).touchScroll("x");};  
    156.             $.fn.touchScrollY = function(){$(this).touchScroll("y");};  
    157.         })(jQuery);   
    158.           
原文地址:https://www.cnblogs.com/susanws/p/5599651.html