在触摸设备上处理OnMouseOver javascript事件有什么更好的替代方法或最佳实践吗? 我所能想到的就是将所有事件转换为OnMouseClick。不幸的是,这会混淆由悬停光标触发的事件和由单击光标触发的事件之间的差异。
有没有其他替代方案或解决方法可以减少对同时使用鼠标设备和触摸设备的网页的用户体验产生影响?
在触摸设备上处理OnMouseOver javascript事件有什么更好的替代方法或最佳实践吗? 我所能想到的就是将所有事件转换为OnMouseClick。不幸的是,这会混淆由悬停光标触发的事件和由单击光标触发的事件之间的差异。
有没有其他替代方案或解决方法可以减少对同时使用鼠标设备和触摸设备的网页的用户体验产生影响?
触摸事件在不同平台上也存在不一致性。如果你想进行任何移动/触摸相关的工作,请参考ppk的工作,http://quirksmode.org/mobile/tableTouch.html
很不幸,我不知道在触摸设备上最佳实践或首选替代onmouseover
是什么,但在遇到相同的问题时,我开发了这个vanillaJS解决方案,在onmouseenter
和onclick
之间计算毫秒数,从而能够区分桌面点击和移动点击。
在研究桌面与移动环境中的这两个事件时,我发现移动触摸会立即触发两个事件(都在零毫秒内),而桌面需要等待几十毫秒,具体时间取决于用户的操作速度。
;(function(){
let
hover_on_mobile = {
timer: 0,
// I don't trust the timer with this,
// so I'm counting myself:
milliseconds: 0,
// also cover the case of the user
// mouseentering, reading or otherwise
// waiting, and clicking *then*.
is_counting: false,
},
item = document.querySelector('.mobile-hoverable')
;
hover_on_mobile.reset = function(){
clearInterval(hover_on_mobile.timer);
hover_on_mobile.milliseconds = 0;
hover_on_mobile.is_counting = false;
};
// hover.
item.onmouseenter = function(){
// preparation for onclick's touch-click detection.
hover_on_mobile.is_counting = true;
// count the milliseconds starting on each
// mouseenter anew.
hover_on_mobile.timer = window.setInterval(function() {
// we only need the first few milliseconds for
// our touch-click detection.
if (hover_on_mobile.milliseconds > 50) {
hover_on_mobile.reset();
} else {
hover_on_mobile.milliseconds++;
}
}, 1);
hover_behavior();
};
// click.
item.onclick = function(ev){
let
condition1 = hover_on_mobile.milliseconds < 10,
condition2 = hover_on_mobile.is_counting
;
console.log('clicked', {
condition1: condition1,
condition2: condition2,
timer: hover_on_mobile.timer,
milliseconds: hover_on_mobile.milliseconds,
is_counting: hover_on_mobile.is_counting,
});
// touch-click detection.
if (condition1 && condition2) {
// don't do anything; let the onmouseenter
// do the hover routine unhinderedly.
//
// if this was an onclick event on an ‹a› tag,
// the ev.preventDefault(); call would go here.
} else {
click_behavior();
}
hover_on_mobile.reset();
};
// ----------------------------------------
// fiddle-specfic.
// reset indicator, not hover_on_mobile.
item.onmouseout = reset_indicator;
function click_behavior() {
document.querySelector('#indicator .click-text').innerText = 'clicked';
}
function hover_behavior() {
document.querySelector('#indicator .hover-text').innerText = 'hovered';
}
function reset_indicator() {
document.querySelector('#indicator .hover-text').innerText = '-';
document.querySelector('#indicator .click-text').innerText = '-';
}
document.querySelector('#indicator .reset').onclick = reset_indicator;
})();
h1 {
font-size: 20px;
line-height: 26px;
}
#indicator {
margin-top: 15px;
padding: 20px;
background-color: #ddd;
}
.mobile-hoverable {
cursor: pointer;
background-color: antiquewhite;
border: 1px outset blanchedalmond;
border-radius: 4px;
padding: 10px;
}
.notes {
font-style: italic;
font-size: 14px;
}
<div class="root">
<h1>Imagine you wanted mobile users to click once in order to simulate a desktop-hover and twice for a desktop-click</h1>
<div class="mobile-hoverable">Hover me, click me, compare with mobile-touch device mode.</div>
<div id="indicator">
<button class="reset">Reset</button>
<span class="hover-text">-</span>
<span class="click-text">-</span>
</div>
<ul class="notes">
<li>Don't forget to reload the page after changing the mode, for optimal testing evaluation.</li>
<li>Click event console.logs hover_on_mobile object.</li>
<li>The fiddle's CSS is irrelevant for this feature.</li>
<li>Relevant JavaScript is bundled together; irrelevant JavaScript at the end.</li>
<li>Didn't test browser compatibility specifically for this fiddle but the feature works in Chrome, Firefox, Safari, IE10+.</li>
<li>Subsequent clicks without onmouseout will only fire the click event, in both environments.</li>
</ul>
</div>
(… 或者作为一个fiddle的替代方案)
这里有另一个fiddle示例,专门展示了桌面和移动环境之间的时差差异。
根据您的需求和目标用户,您可能会对可用的触摸JS API感兴趣,至少在移动版Safari和Chrome上。请查看http://backtothecode.blogspot.com/2009/10/javascript-touch-and-gesture-events.html,了解快速介绍(也许有点过时)。我并没有广泛使用这些API(只针对iPhone),但到目前为止,我对所得到的结果非常满意。