Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

horizontal scrolling of div not working with scrollLeft

I tried to make horizontal scrolling with scrollLeft. I’m able to align div horizontally but it looks like the scrollLeft function is not working.

Here is my code:

function myFunction() {
  var elmnt = document.getElementById("myDIV");
  elmnt.scrollLeft += 50;
}
#myDIV {
  height: 250px;
  width: auto;
  display:inline-flex;
  gap:10px;
  overflow: auto;
}

#content {
  height: 800px;
  width: 200px;
  background-color: coral;
}
<button onclick="myFunction()">Scroll div</button><br><br>

<div id="myDIV">
  <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
</div>

Thank you for the help in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I applied the white-space and overflow style inside the #myDiv style; I set the width property to 220px to follow the scrollLeft[1].

function myFunction() {
  var element = document.getElementById("myDIV");
  element.scrollLeft += 50;
}
#myDIV {  
  white-space: nowrap;
  overflow: scroll;
  width: 220px;
  height: 250px;
  display: inline-flex;
  gap: 10px;
}
#content {
  height: 800px;
  width: 200px;
  background-color: coral;
}
<button onclick="myFunction()">Scroll div</button><br><br>

<div id="myDIV">
  <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
    <div id="content">
  Some text inside a div element.<br><br>
  Some text inside a div element.
  </div>
</div>

1 – JavaScript element.scrollLeft not working

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading