javascript - jQuery touchSwipe内容滚动功能

标签 javascript jquery math mobile scroll

我正在使用 jQuery 和 touchSwipe 插件上下滚动内容。我遇到了一些与数学相关的问题,想看看社区是否可以快速帮助我解决这个问题。

touchSwipe“距离”变量仅返回从零开始的正值,因此当用户向上滑动然后决定向下滑动时问题就出现了,因为距离值从 ex:(0 initial, 15up, 32up , 20up, 5up, 10down, 40down).我创建了 4 个 if 语句来捕获所有这些情况。如何处理“方向改变”if 语句?

触摸滑动:http://labs.skinkers.com/touchSwipe/

上下滑动功能:

var _scrolling=false;
var _prev=0;
$("#list").swipe({
  swipeStatus:function(event, phase, direction, distance, fingerCount) {
    if(phase=="end"){return false;}
    if(phase=="move" && !_scrolling) {
      var t = parseInt($("#list").css("top").split('px')[0]);
      var s = 0;
      if(direction=="up" && (distance-_prev) > 0){
         //still moving up
         s = t-(distance-_prev);
         _prev=distance;
       } else if(direction=="up" && (distance-_prev) < 0){
          //direction change - moving down    
       } else if(direction=="down" && (distance-_prev) > 0){
         //still moving down
          s = t+(distance-_prev);
         _prev=distance;
       } else if(direction=="down" && (distance-_prev) < 0){
          //direction change - moving up
       }
       _scrolling=true;
       $("#list").animate({ top:s }, 70, function() {_scrolling=false;});   
   }<!--if end-->
  }<!--swipeStatus end-->
});

最佳答案

所以几天后我解决了这个问题,修改了原来的“swipeStatus”函数,并创建了一个非常高效的全功能函数来使用 touchSwipe jQuery 插件滚动绝对区域。

解决方案:

function swipeScroll(c,b,pd,f){
    $(c).swipe({//c style must be position:absolute;top:0; b style must be position:relative;
      swipeStatus:function(event, phase, direction, distance, fingerCount) {
        if(phase=="start"){pd=0;}
        if(phase=="end"){return false;}
        if(phase=="move" && pd!=distance){
          var t = parseInt($(c).css("top"),10);
          var u = (pd-distance);
          if(direction=="up" && t != u){ t+=u; }else if(direction=="down" && t != -u){ t-=u; }
          pd=distance;
          if(t > 0 || $(b).height() > $(c).height()){t=0;}//top of content
          else if(($(b).height()-t) >= $(c).height()){//bottom of content
            t=$(b).height()-$(c).height();
            if(typeof f != "undefined"){f();}
          }
          $(c).css("top",t);
        }
      }
    });
}

函数参数解释:

c:你要滚动的绝对内容 b:围绕内容“c”的相关容器 pd:变量来存储从上一个滚动移动的距离 f: 滚动到底部时调用的函数

注意:“b”必须有相对位置,“c”必须有绝对位置,top:0

示例用法:

var distance;
swipeScroll("#list","#body",distance, function() { alert("bottom"); });

研究:

而不是使用“$(c).css("top",t);”我还使用 jQuery animate() 函数对此进行了测试,以提供平滑的滑动效果,但这实际上使滚动变得缓慢。动画给出了意想不到的结果,因为“t”有时会在滑动太快时跳过“0”,并且方向会从“顶部”变为“向下”,导致正在滚动的内容突然跳转。

关于javascript - jQuery touchSwipe内容滚动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12158414/

相关文章:

javascript - 在浏览器上保存数据(作为 JS 数组)是一件坏事吗?

ios - 方程求解器/解析器

c++ - 从整数到 [0,1] 浮点区间的函数

javascript - 如何在所选文本旁边显示工具提示?

javascript - 如何在弹出窗口时聚焦facebox

javascript - 如何使用jquery删除html中的bug字符

Javascript:将以前的网址与当前网址进行比较

python - 使用 python 的 ** 运算符和 * 运算符时进行签名

javascript - 导入/更改 HTML 内容(ES6 更新后)

javascript - 使用 Javascript 将多个 HTML 表格导出到单个 Excel 文件 (xls)