当在其包装上创建触摸和鼠标拖动事件时,如何防止锚上的单击事件
How to prevent click event on anchors when making touch and mouse drag events on its wrapper
如何防止点击事件锚(重定向到href url,在演示重定向到谷歌)内的一些包装器当使触摸和鼠标拖动事件在这个包装器?使用preventDefault和stopPropagation,我只能限制冒泡DOM,对吗?
我想在拖动时禁用链接,并在不拖动时启用单击。
下面是这个问题的演示。
var items = $('#items');
function start(event) {
event.preventDefault();
console.log('touchstart mousedown');
items.on('touchmove mousemove', move);
items.on('touchend mouseup', end);
return false;
}
function move(event) {
console.log('touchmove mousemove');
return false;
}
function end(event) {
console.log('touchend mouseup');
items
.off('touchmove mousemove')
.off('touchend mouseup');
return false;
}
items.on('touchstart mousedown', start);
https://jsfiddle.net/9oxr4quz/4/我想出了两个解决方案:
:
触发mousemove
事件时,绑定click
事件,在事件对象上调用preventDefault
,防止浏览器跟随链接。当touchstart
和/或mousedown
被触发时,关闭jquery点击处理程序。
var items = $('#items a'); // Notice I changed the selector here
function start(event) {
event.preventDefault();
console.log('touchstart mousedown');
items.off('click');
items.on('touchmove mousemove', move);
items.on('touchend mouseup', end);
return false;
}
...
function move(event) {
items.on('click', function(event){ event.preventDefault(); });
console.log('touchmove mousemove');
return false;
}
...
工作演示:http://codepen.io/anon/pen/WvjrPd
第二:自己处理点击事件,这样你就可以决定浏览器什么时候应该访问另一个站点,什么时候什么都不做。这可以通过用data-link
或data-href
属性替换href
属性来实现。
现在,当touchstart
或mousedown
事件被触发时,打开点击事件;如果这些事件中的任何一个导致mousemove
事件,则关闭click
事件:
<div id="items" class="items">
<div class="item">
<a data-link="http://google.com">Anchor</a>
</div>
<div class="item">
<a data-link="http://google.com">Anchor</a>
</div>
</div>
Javascript: var items = $('#items a'); // Notice I changed the selector here
function start(event) {
event.preventDefault();
console.log('touchstart mousedown');
items.on('click', click); // Turn on click events
items.on('touchmove mousemove', move);
items.on('touchend mouseup', end);
return false;
}
function move(event) {
items.off('click'); // Turn off click events
console.log('touchmove mousemove');
return false;
}
…
function click(event) { // Visit the corresponding link
var link = $(this).attr('data-link');
alert('Visit link: ' + link);
// window.location.href = link;
}
items.on('touchstart mousedown', start);
CSS: .item {
background-color: gray;
}
.item + .item {
margin-top: 10px;
}
.item a {
cursor: pointer;
display: inline-block;
background-color: blue;
color: white;
padding: 9px;
width: 100%;
height: 100%;
}
工作演示:http://codepen.io/anon/pen/EjmPrJ
在JavaScript中使用禁用所有指针事件的CSS方法,然后在触摸或移动时将它们添加回来。一个简单的方法是这样定义CSS:
a.prevent-me {
pointer-events: none; /* This line */
cursor: default;
}
使用jquery添加类和删除类,像下面需要在您的事件。
...
$(".item a").addClass("prevent-me");
...
...
$(".item a").removeClass("prevent-me");
...
所以整个解决方案,我还没有测试过它可能是
var items = $('#items');
function start(event) {
event.preventDefault();
console.log('touchstart mousedown');
$(".item a").addClass("prevent-me"); //remove all click events
items.on('touchmove mousemove', move);
items.on('touchend mouseup', end);
return false;
}
function move(event) {
console.log('touchmove mousemove');
return false;
}
function end(event) {
console.log('touchend mouseup');
items
.off('touchmove mousemove')
.off('touchend mouseup');
$(".item a").removeClass("prevent-me"); //enable back click
return false;
}
items.on('touchstart mousedown', start);
相关文章:
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- 针对重复发生的事件,使用moment.js防止DST偏移
- 如何在事件处理程序的回调中防止Default
- 如果鼠标在元素上快速移动和关闭,则防止.hoop事件被垃圾邮件发送
- 防止在输入字段上单击事件
- 防止默认'F1'iE11中的事件
- 如何防止onclick事件在select选项中触发
- 传单.绘制防止事件发生
- 防止动态创建的onclick事件过早触发
- 当我认为它不应该重新渲染视图时,如何防止 Meteor 在 html 选择 dom 单击事件上重新渲染视图
- 如何防止 jQuery 事件在删除元素时冒泡
- 防止对某些事件进行进一步的动画处理
- 如果类有多个事件处理程序方法,如何防止多个事件执行
- Meteor模板数据对象可防止javascript事件触发
- 通过防止窗口事件问题防止双重提交
- 添加事件以防止Inappbrowser内超时
- SAPUI5 事件总线:防止堆叠相同的订阅
- 图表拦截触摸事件并防止在 c3 js 中滚动
- 如何在触发 ondblClickRow 事件时防止 onSelectRow 事件
- DOM Level 0事件:如何防止外部点击触发