我在尝试合并 1 个函数时遇到touchstart问题mousedown。我使用了一个a标签作为函数的目标元素,当我触摸或单击标签时直接转到链接。
问题是当我touch在标签中间时a,链接没有响应。它仅在我click元素或触摸标签边缘a并且输出触发时才有效mousedown。
在移动模式下,尽可能多地点击a标签边缘,如上图中的灰点。我创建了一个CodePen示例,以便更好地查看、测试和理解。
我将如何解决这个问题?
class Slider {
constructor($el, paragraph) {
this.$el = $el;
this.paragraph = paragraph;
}
start(e) {
e.preventDefault();
var type = e.type;
if (type === 'touchstart' || type === 'mousedown') this.paragraph.text(this.paragraph.text() + ' ' + type);
return false;
}
apply() {
this.$el.bind('touchstart mousedown', (e) => this.start(e));
}
}
const setSlider = new Slider($('#anchor'), $('.textbox'), {passive: false});
setSlider.apply();
a {
display: block;
width: 100px;
height: 100px;
background-color: orange;
}
<a id="anchor" href="https://google.co.uk">Tap or Click Me</a>
<p class="textbox"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
=========进度更新==========
我刚刚添加了move&end功能,然后我必须单击两次才能转到链接的网站。它不断变得更糟,不知道如何解决这个问题。
class Slider {
constructor($el, paragraph) {
this.$el = $el;
this.paragraph = paragraph;
}
start(e) {
e.preventDefault();
var type = e.type;
if (type === 'touchstart' || type === 'mousedown') this.paragraph.text(this.paragraph.text() + ' ' + type);
this.$el.bind('touchmove mousemove', (e) => this.move(e));
this.$el.bind('touchend mouseup', (e) => this.end(e));
return false;
}
move(e) {
var type = e.type;
if (type === 'touchstart' || type === 'mousedown') this.paragraph.text(this.paragraph.text() + ' ' + type);
return false;
}
end(e) {
console.log('test');
this.$el.on('click');
this.$el.off('touchstart touchend');
return false;
}
apply() {
this.$el.bind('touchstart || mousedown', (e) => this.start(e));
}
}
const setSlider = new Slider($('#anchor'), $('.textbox'));
setSlider.apply();
======== 赏金后更新进度(最新) ========
经过数十次尝试,我终于弄清楚并解决了以前的问题,但我遇到了一个无法立即拖动和重定向的新问题。
当我preventDefault在 start 函数中使用时,所有事件都可以正常工作。这种情况的唯一问题是拖动不会阻止从a标签重定向链接。无论以哪种方式调用功能,单击或拖动,它总是将我发送到网站。
当我不使用时preventDefault,拖动不起作用。它仅适用于单击元素。
我的最终目标是防止a从两个事件中重定向标签链接,touchmove并且mousemove. 我已经在谷歌上搜索了很多次,但没有任何线索。
我在Codepen中写了一个例子,这就是我到目前为止所做的:
class Slider {
constructor($el, paragraph) {
this.$el = $el;
this.paragraph = paragraph;
}
start(e) {
var type = e.type;
if (type === 'touchstart') {
this.paragraph.text(this.paragraph.text() + ' ' + type);
} else if (type === 'mousedown') {
this.paragraph.text(this.paragraph.text() + ' ' + type);
}
}
move(e) {
var type = e.type;
}
end(e) {
var type = e.type;
if (type === 'touchend') {
console.log('touchstart enabled');
} else if (type === 'mouseup') {
console.log('mousedown enabled');
}
}
apply() {
this.$el.bind({
touchstart: (e) => this.start(e),
touchmove: (e) => this.move(e),
touchend: (e) => this.end(e),
mousedown:(e) => this.start(e),
onmousemove: (e) => this.move(e),
mouseup: (e) => this.end(e)
});
}
}
const setSlider = new Slider($('#anchor'), $('.textbox'));
setSlider.apply();
a {
display: block;
width: 100px;
height: 100px;
background-color: orange;
}
<a id="anchor" href="https://google.co.uk">Tap or Click Me</a>
<p class="textbox"></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
