I have a div
, fixed height, and scroll.
But inside this div, have an input range. When I fire the wheel event then the scroll of the div is also triggered, so is there any way to block this scrolling while I’m wheeling?
Here is an example of this
import { Box } from "@mui/material";
import { useState } from "react";
export default function App() {
const [volume, setVolume] = useState(0);
const onwheelVolumne = (e) => {
if (e.deltaY < 0) {
setVolume(volume + 10 > 100 ? 100 : volume + 10);
} else {
setVolume(volume - 10 < 0 ? 0 : volume - 10);
}
return false;
};
return (
<Box
border="1px solid #ccc"
padding={2}
display="flex"
flexDirection="column"
height={200}
overflow="auto"
>
<input type="range" value={volume} onWheel={onwheelVolumne} />
{Array(10)
.fill(10)
.map((i, idx) => (
<p key={idx}>samnple</p>
))}
</Box>
);
}
Sandbox: https://codesandbox.io/s/modern-sun-lfyhlr?file=/src/App.js:23-738
2
Answers
I think you can use
onMouseEnter
andonMouseLeave
to toggle the scroll event of parent div:Using
onMouseEnter
to detect and add event to prevent scroll