Home > Blockchain >  How to move scroll into top/up direction by using javascript?
How to move scroll into top/up direction by using javascript?

Time:10-01

I want to move the scroll to the top direction when clicking on the top arrow. the bottom arrow is working but I am not able to rectify why Top arrow is not working.

I have added a snippet to understand the issue is there any other clarification needed then please comment.

thanks in advance.

<html>
  <head>
      <style type='text/css'>
         
          .content-container
          {
            height:250px;
            width:250px;
            overflow: auto;
            border:1px solid;
            padding:0 15px;
          }

          ::-webkit-scrollbar {
            width:7px;
            height:7px;
            background: #80808045;
          }

          /* Track */
          ::-webkit-scrollbar-track {
          
          }
          
          /* Handle */
          ::-webkit-scrollbar-thumb {
            background:#4cae35; 
          }
          
          
      </style>
      <script src="https://pcbmagic.com/assets/cdn/jquery.min.js"></script>
  </head>
  <body>

      <p id="my-scroll-buttom-button">⬆️</p>
      <div class="content-container" id="content-container">
          <p>
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            .
          </p>
      </div>
      
      <p id="my-scroll-top-button">⬇️</p>
   
      <script> 
              document.getElementById( "my-scroll-top-button" ).onclick = () => {

              document.getElementById( "content-container" ).scrollTop  = 30;

              }

              document.getElementById( "my-scroll-buttom-button" ).onclick = () => {

              document.getElementById( "content-container" ).scrollBottom -= 30;

              }

      </script>
    </div>
  </body>
  </html>

CodePudding user response:

Like @Ouroborus said,

.scrollBottom should be .scrollTop

<html>
  <head>
      <style type='text/css'>
         
          .content-container
          {
            height:250px;
            width:250px;
            overflow: auto;
            border:1px solid;
            padding:0 15px;
          }

          ::-webkit-scrollbar {
            width:7px;
            height:7px;
            background: #80808045;
          }

          /* Track */
          ::-webkit-scrollbar-track {
          
          }
          
          /* Handle */
          ::-webkit-scrollbar-thumb {
            background:#4cae35; 
          }
          
          
      </style>
      <script src="https://pcbmagic.com/assets/cdn/jquery.min.js"></script>
  </head>
  <body>

      <p id="my-scroll-buttom-button">⬆️</p>
      <div class="content-container" id="content-container">
          <p>
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            .
          </p>
      </div>
      
      <p id="my-scroll-top-button">⬇️</p>
   
      <script> 
              document.getElementById( "my-scroll-top-button" ).onclick = () => {

              document.getElementById( "content-container" ).scrollTop  = 30;

              }

              document.getElementById( "my-scroll-buttom-button" ).onclick = () => {

              document.getElementById( "content-container" ).scrollTop -= 30;

              }

      </script>
    </div>
  </body>
  </html>

  • Related