Anythingslider 触摸滑动功能可阻止 IOS 和平板设备上链接的正常点击
我正在将任何滑块 jquery 插件与触摸事件一起使用。 它似乎在所有设备上都能按预期工作,允许用户通过触摸平板电脑和 iOS 设备以及在桌面上使用鼠标来“滑动”。
$('#slider').anythingSlider({
// Callback when the plugin finished initializing
onInitialized: function(e, slider) {
var time = 1000, // allow movement if < 1000 ms (1 sec)
range = 50, // swipe movement of 50 pixels triggers the slider
x = 0, t = 0, touch = "ontouchend" in document,
st = (touch) ? 'touchstart' : 'mousedown',
mv = (touch) ? 'touchmove' : 'mousemove',
en = (touch) ? 'touchend' : 'mouseup';
slider.$window
.bind(st, function(e){
// prevent image drag (Firefox)
e.preventDefault();
t = (new Date()).getTime();
x = e.originalEvent.touches ?
e.originalEvent.touches[0].pageX : e.pageX;
})
.bind(en, function(e){
t = 0; x = 0;
})
.bind(mv, function(e){
e.preventDefault();
var newx = e.originalEvent.touches ?
e.originalEvent.touches[0].pageX : e.pageX,
r = (x === 0) ? 0 : Math.abs(newx - x),
// allow if movement < 1 sec
ct = (new Date()).getTime();
if (t !== 0 && ct - t < time && r > range) {
if (newx < x) { slider.goForward(); }
if (newx > x) { slider.goBack(); }
t = 0; x = 0;
}
});
然而,我的滑块(包含链接的图像和文本)无法被平板电脑和 iOS 设备“选择”(激活链接),文本保持悬停 CSS 样式,但触摸不执行任何操作。
通过鼠标在桌面上单击仍然有效,用户可以单击查看文章。
知道如何使其在所有设备上工作吗?
即我需要能够滑动并选择滑块中的链接。
我想我的选择是: 1.将滑动效果限制在图像上,使文本框保持可点击 2.添加到jquery选项,如果有零移动激活链接 3. 将文本的 z-index 更改为“滑动覆盖”div 上方
我不知道如何编码选项 1 或 2。请提供建议?
第3项我会同时尝试。
I am using the anything slider jquery plugin with the touch events.
it appears to be working as expected on all devices allowing users to 'swipe' by touch on tablets and ios devices and by using the mouse on a desktop.
$('#slider').anythingSlider({
// Callback when the plugin finished initializing
onInitialized: function(e, slider) {
var time = 1000, // allow movement if < 1000 ms (1 sec)
range = 50, // swipe movement of 50 pixels triggers the slider
x = 0, t = 0, touch = "ontouchend" in document,
st = (touch) ? 'touchstart' : 'mousedown',
mv = (touch) ? 'touchmove' : 'mousemove',
en = (touch) ? 'touchend' : 'mouseup';
slider.$window
.bind(st, function(e){
// prevent image drag (Firefox)
e.preventDefault();
t = (new Date()).getTime();
x = e.originalEvent.touches ?
e.originalEvent.touches[0].pageX : e.pageX;
})
.bind(en, function(e){
t = 0; x = 0;
})
.bind(mv, function(e){
e.preventDefault();
var newx = e.originalEvent.touches ?
e.originalEvent.touches[0].pageX : e.pageX,
r = (x === 0) ? 0 : Math.abs(newx - x),
// allow if movement < 1 sec
ct = (new Date()).getTime();
if (t !== 0 && ct - t < time && r > range) {
if (newx < x) { slider.goForward(); }
if (newx > x) { slider.goBack(); }
t = 0; x = 0;
}
});
however my sliders, which contain both images and text that are links, can not be 'selected' (link activated) by tablets and ios devices, the text maintains the hover css styling, but touching does nothing.
clicking via a mouse on a desktop still works and users can click through to articles.
any idea on how to make this work on all devices?
i.e. i need to be able to slide and also select the links in the slider.
i think my options are:
1. limit the swipe effect to the images, leaving the text box clickable
2. add to the jquery option that if there is zero movement activate the link
3. change the z-index of the text to be above the 'swipe overlay' div
i have no idea how to code options 1 or 2. advice please?
item 3 i will try in the mean time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者(如果您不想修改其源代码),您可以将以下处理程序绑定到 TouchSwipe 的 Click 事件(我使用的是 jQuery,但您明白了):
这将使事件冒泡。
使用代码示例片段更新:
Alternatively (if you don't want to modify their source) you can bind the following handler to the Click event of TouchSwipe (I'm using jQuery but you get the idea) :
This will bubble up the event.
UPDATE WITH CODE SAMPLE SNIPPET:
您的 touchstart 事件侦听器正在调用 PreventDefault() ,这会阻止事件冒泡到单击事件。如果您删除它,但将其保留在移动和结束事件中,它应该可以工作。
You're touchstart event listener is doing calling preventDefault() which prevents the event from bubbling up to the click event. If you remove it, but keep it in the move and end events it should work.
你只需要删除这个:
You just have to remove this: