iOS mousedown event triggering issue
# FrontendiOS has issues with the mousedown handler not functioning correctly in certain versions; however, on other devices (like Android, etc.), mousedown works perfectly fine.
The solution is to avoid using the mousedown handler on mobile devices and instead use touchstart. But will this affect the trigger order? In the original click event, blur is triggered first (if it exists), followed by the click event. To bypass this, mousedown was used as a replacement.
Here’s a simple experiment conducted on CodePen:
See the Pen touchstart-test by 愷開 (@kjj6198) on CodePen.
It turns out the trigger order is touchstart > mousedown > blur. It seems that when both touchstart and mousedown events are added, and both are supported, the events occur in the order of touchstart > mousedown.
After doing some research online, I found that the mousedown event is not triggered on iOS versions prior to 13; it works normally on iOS 13 and later. Additionally, for iPads, it appears that you can distinguish whether the touch is coming from a finger or an Apple Pencil.
Conclusion
- Be cautious when binding
mousedownevents on iOS; it’s best to use touch-related events instead. - iOS 13 serves as a dividing line; additional testing is necessary for versions below 13.
I’ll document this here for now and will add detailed behaviors later.
Related Posts
- CSS field-sizing — Auto-Resize Form Elements with One Line of CSSMaking a textarea auto-resize used to require JavaScript to watch scrollHeight. CSS field-sizing: content replaces all of that in one line, supporting textarea, input, and select.
- Make Your Hyperlink Underlines Look Better: text-underline-offsetBy default, underlines sit very close to the text, and some designers dislike this style. Personally, I don’t think it looks very good either.
- Why Web Design Shouldn’t Chase Pixel PerfectOnly pay attention to Pixel Perfect when it really matters; otherwise, it often leads to a lose-lose situation.
- Let’s Write Colors with CSS HSL! (And a Better Way)In web development, the traditional HEX and RGB color notations are widely used, but they are not very readable or intuitive, and their capabilities are limited in wider color spaces such as P3. HSL (Hue, Saturation, Lightness) provides a more intuitive way to define colors, making it easier for developers to understand and adjust them. By describing colors through the three dimensions of hue, saturation, and lightness, HSL makes color adjustment more human-friendly. In design systems in particular, HSL can better represent lightness variations in a color palette.