Kalan's Blog

Kalan 頭像照片,在淡水拍攝,淺藍背景

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

我會把一些不成文的筆記或是最近的生活雜感放在短筆記,如果有興趣的話可以來看看唷!

Please notice that currenly most of posts are translated by AI automatically and might contain lots of confusion. I'll gradually translate the post ASAP

iOS mousedown event triggering issue

In some versions of iOS, the mousedown event handler does not work correctly, but it still works properly on other devices like Android.

The solution is to avoid using the mousedown event handler on mobile devices and use touchstart instead. However, it is uncertain if the triggering order will be affected. Originally, in the click event, the blur event would trigger first (if present) and then the click event would be triggered. The reason mousedown was used instead was to avoid this point.

Perform a simple experiment on CodePen:

<p class="codepen" data-height="265" data-theme-id="light" data-default-tab="js,result" data-user="kjj6198" data-slug-hash="jOrVygy" style="height: 265px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="touchstart-test">
  <span>See the Pen <a href="https://codepen.io/kjj6198/pen/jOrVygy">
  touchstart-test</a> by 愷開 (<a href="https://codepen.io/kjj6198">@kjj6198</a>)
  on <a href="https://codepen.io">CodePen</a>.</span>
</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

It can be observed that the triggering order is touchstart > mousedown > blur. It seems that if both touchstart and mousedown events are added and both events are supported, they will occur in the order of touchstart > mousedown.

I searched online and found that mousedown events are not triggered only in iOS versions earlier than 13, and they work normally in iOS 13 and later. Additionally, on iPad, it seems possible to determine whether the touch is from a finger or an Apple Pencil.

Conclusion

  • Be cautious when using mousedown or similar bindings on iOS, and try to use touch-related events instead.
  • iOS 13 and iOS versions earlier than 13 are a dividing point and require separate testing.

I'm noting this down here for now, and will add more detailed behavior later.

Prev

Explanation of regular expression to add a number to comma

Next

站在中間的那群人 / 做出選擇的那群人

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee