我使用了jQuery TouchSwipe插件。它在div上运行良好,但对于链接却一点也不起作用。
我希望当您点击链接时,可以获得默认的链接行为。但如果您滑动屏幕,我希望JavaScript滑动像处理div元素一样被触发。
我使用了jQuery TouchSwipe插件。它在div上运行良好,但对于链接却一点也不起作用。
我希望当您点击链接时,可以获得默认的链接行为。但如果您滑动屏幕,我希望JavaScript滑动像处理div元素一样被触发。
太棒了,这是一个很好的问题。为了解决这个问题,我查看了源代码。你应该知道,锚点滑动在默认情况下是禁用的。
版本:1.5.0 - 添加了排除元素(excludedElements),它是一个 jQuery 选择器,指定不触发滑动的子元素。默认情况下,这是一个选择器,用于移除所有表单、输入选择、按钮和锚点元素(源代码)。
默认值:
excludedElements:"label, button, input, select, textarea, a, .noSwipe"
只需传递不包含锚点标签的excludedElements
选项即可使滑动工作在链接上:
$("a").swipe({
// Generic swipe handler for all directions
swipe: function(event, direction) {
$(this).text("You swiped " + direction);
},
excludedElements: "label, button, input, select, textarea, .noSwipe"
});
解决这个问题还有一个关键点。不要将 threshold: 0
设置为零,因为它会禁用所有的点击事件和轻触事件。您可以将 threshold
设置为任何大于零的数字,或者干脆省略它。如果将其设置为threshold: 1
,则只会允许非常静止的鼠标点击,否则会被视为滑动操作。
希望这能解决你的问题。
$(function() {
// Enable swiping...
$(".test").swipe({
// Generic swipe handler for all directions
swipe: function(event, direction) {
$(this).text("You swiped " + direction);
},
excludedElements: "label, button, input, select, textarea, .noSwipe",
threshold:1
});
// Stackoverflow disables snippets opening links, so this captures clicks for a demo
$(".test").on("click", function(e){
alert($(this)[0].nodeName + " was clicked");
});
});
.test {font-size: 48px;}
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script><script src="http://labs.rampinteractive.co.uk/touchSwipe/jquery.touchSwipe.min.js"></script><a href="http://google.com" target="_blank" class="test">Please swipe me</a><br><br><div class="test">Please swipe me</div>
此版本将在手指/鼠标释放前,在手指/鼠标扫过 threshold
像素之后检测到滑动。此方法通过检测滑动并设置某些链接中的 data
,然后由第一个单击处理程序读取该数据并阻止 一个 单击事件传播。
.on("click", function(event) {
处理程序必须是 jQuery 事件链中的第一个处理程序,因此请将所有这些代码放置在页面顶部附近,最好就在加载 jQuery 的位置下方。
$(function() {
$(".test").swipe({
swipe: function(event, direction) {
$(this).text("You swiped " + direction);
},
swipeStatus: function(event, phase) {
var $this = $(this);
$this.data("stopclick", true); // Signal a temporarily click block
if(phase === $.fn.swipe.phases.PHASE_CANCEL) {
// Swipe was canceled, so unblock click handers
$this.data("stopclick", false);
}
},
excludedElements: "label, button, input, select, textarea, .noSwipe",
threshold:10,
triggerOnTouchEnd: false
})
.on("click", function(event) {
// Prevent click event propogation for one click
var $this = $(this);
if($this.data("stopclick") === true) {
event.stopImmediatePropagation();
event.preventDefault();
$this.data("stopclick", false); // Restore click propogation
}
});
// Stackoverflow disables snippets opening links, so this captures clicks for a demo
$(".test").on("click", function(e){
alert($(this)[0].nodeName + " was clicked");
});
});
.test {font-size: 48px;}
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script><script src="http://labs.rampinteractive.co.uk/touchSwipe/jquery.touchSwipe.min.js"></script><a href="http://google.com" target="_blank" class="test">Please swipe me</a><br><br><div class="test">Please swipe me</div>
threshold:1
,这样只有在静止点击时才会触发点击事件。我已经更新了上面的代码片段。希望这正是您所需要的。 - Drakes这并不是最优解,但您可以使用任何具有类似链接样式的元素,并像下面这样处理方向参数。
$("#swipable").swipe( {
swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
if (direction == null) {
location.href = 'somewhere.html'
} else {
// Do something else...
}
},
threshold:0
});
这里有一个可工作的例子 https://jsfiddle.net/q3gnt8s5/8/
PS. 刚刚注意到这篇文章已经很老了,希望无论如何它都能对你有所帮助,哈哈