0

I have a scrollable <div> and I need to prevent the user from scrolling. But at the same time, JavaScript should be allowed to scroll it.

Previously, I've tried setting overflow: hidden; but in some browsers/os it didn't let JavaScript scroll. I've tried setting overflow: auto; and hiding the scrollbar and then in JavaScript listening for wheel events and calling preventDefault() but that did nothing. I can't listen for scroll events because then it prevents JavaScript from scrolling too.

const btn = document.querySelector('button#scroll');
const cnt = document.querySelector('div#container');

btn.addEventListener('click', e => {
  cnt.scrollTo(20, 500);
});
div#container {
  width: 400px;
  height: 400px;
  overflow-y: auto;
  scroll-behavior: smooth;
  -ms-overflow-style: none;
  scrollbar-width: none;
}
div#container::-webkit-scrollbar {
  display: none;
}
<button id="scroll">Click to scroll</button>

<div id="container">
  <img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br>
  <img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br><img src="https://p.turbosquid.com/ts-thumb/bK/x6a3nk/nlMteHHm/r1/jpg/1590782525/600x600/fit_q87/f469b1b09d4935ef73b0746eb4b77fbe06b57293/r1.jpg">
  <br>
</div>

Any ideas? TYIA

4
  • 1
    Do you have some set-up code we can use? I can't be bothered to spend 5 minutes writing it myself. It's also in how to ask. Commented Oct 22, 2022 at 13:42
  • @caTS I've added a snippet now Commented Oct 22, 2022 at 13:54
  • devforhelp.com/code2.html can you check here but the problem is it scrolling only once. Do you want to scroll every time onClick ? also I have tested on iPhone 13 it seems working Commented Oct 22, 2022 at 14:11
  • @devforhelp that's good, if you post the code in an answer I'll accept it Commented Oct 22, 2022 at 14:16

2 Answers 2

2

You can use overflow-y: hidden to make sure the scrolling position can only be changed by javascript.

Sign up to request clarification or add additional context in comments.

Comments

1

view-source:https://devforhelp.com/code2.html

Please open the link and check the source code

2 Comments

Please put the code, plus an explanation of how it solves the problem, into your answer, otherwise there is a danger that your answer will be useless to future readers e.g.should your personal site disappear.
Thank you and I will make sure from next time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.