gpt4 book ai didi

javascript - jQuery touchSwipe内容滚动功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:34 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com