This is definitely an interesting find! You’d think this would be impossible since you set overflow-y to none. Nonetheless you can prevent this behavior and disallow clicks inside the parent using this JavScript:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
By adding preventDefault() to the event triggered on mousedown, you prevent the browser from evaluating any further events related to that click, including dragging. Here it is in action:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
Using css you can give pointer-events:none; to avoid any events occurring while clicking it.
Problem :
Take a look at this codepen
There’s red square in the corner.
If you click (using mouse or touchpad) this square and drag it down – in Safari on Mac .parent element will scroll down. Even though .parent has overflow-y: hidden;
No scrolling occurs in all other browsers.
How can I prevent this scrolling on safari?
As a rough solution, I can listen to the scroll and override the scrollTop if it’s not zero, but perhaps there is a more elegant solution?
Comments
Comment posted by Brandon McConnell
Hi Denis, please see my solution when you have a moment.
Comment posted by Denis
Nice find with first solution, hacky but almost css-only.
Comment posted by Brandon McConnell
I corrected a typo
Comment posted by Moorthy G
Ah yes, thanks for pointing out! the second solution doesn’t work, Because I’ve pointer-events: auto to child. I’ve removed it now. This can be used if there is no interactions required for the descendants
Comment posted by Denis
Hello, thanks, this is a good solution, better than checking and overriding scrollTop. Let’s wait a few more days, if there are no better ideas, I will accept this answer.
Comment posted by Brandon McConnell
@Denis thanks! Let me know if there’s anything else specifically you’re looking for which I can add, or are you just waiting to see if there’s a CSS-only solution to this? I too would be very interested to see one.
Comment posted by Denis
I need it to scroll horizontally, that’s why there is an overflow-x: auto in the example