0

could you please tell me how to scroll an element using javascript? Actually, I have created a demo application. I can be left when "left is positive" but not able to scroll when the left is negative.

I created two cases. First cases working fine. When I click on the button it scrolls to 100 px. But in the second case, I am not able to scroll negative why? here is my code

function handler(){
 
  document.querySelector(".p").scroll({
    left:100,
    behavior: 'smooth'
  })
  
}

document.getElementById("button").addEventListener("click", handler, false)


function handler2(){
 
  document.querySelector(".p2").scroll({
    left:-100,
    behavior: 'smooth'
  })
  
}

document.getElementById("button2").addEventListener("click", handler2, false)

https://jsbin.com/mahohoquge/edit?html,js,output

  1. case 1: is working fine

**

1. 2. case 2: not working fine

**

any idea? how to scroll

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  case : 1
  <div class="p">
    <div class="b r">1</div>
    <div class="b g">1</div>
     <div class="b bl">1</div>
  </div>
  <button id="button">move</button>
  <hr/>
  
    case : 2
  <div class="p2">
    <div class="b r2">1</div>
    <div class="b g2">1</div>
     <div class="b bl2">1</div>
  </div>
  <button id="button2">move left</button>
</body>
</html>
7
  • Could you please elaborate a little more about what is the effect you want to achieve? Commented Mar 3, 2021 at 11:13
  • no effect , but move to previous slide same as example one Commented Mar 3, 2021 at 11:15
  • if you see first example next slide display in button click.can we do the same thing in second example Commented Mar 3, 2021 at 11:16
  • In otherwords blue is coming from right in example one.I want it should come from left in second example Commented Mar 3, 2021 at 11:17
  • I am not sure but the jquery double scroll is may be useful for you. [jqueryscript.net/other/… Commented Mar 3, 2021 at 11:38

2 Answers 2

2

if you're trying to achieve the carousel effect, it would be much effective to use the transform: translate() function instead of using the scroll. https://jsbin.com/kiditahado/edit?html,css,js,output this edited snippet from your original one shows how you could do it.

let start = 0;
function handler(step){
 const p = document.querySelectorAll(".p div");
  start+=step
  p.forEach(el=>{

  el.style.transform =`translate(${start}px,0)`

  })

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

Comments

0

The underlying problem is that u can't scoll into negative values. The only solution witch comes to my mind is to reverse the blocks and scroll from the right side.

So css becomes:

.p2{
  position:relative;
  width:100px;
  height:100px;
  overflow:hidden;
  left:50%;
}
.b2{
  width:100px;
  height:100px;
  position:absolute
}
.r2 {
  background-color:red;
  left:200px;
}

.bl2 {
  background-color:blue;
  left:100px;
}

.g2 {
  background-color:green;
  left:0px;
}

And JS becomes:

function handler2(){  
  document.querySelector(".p2").scroll({
    left:100,
    behavior: 'smooth'
  })     
}

// start from the other side
document.querySelector(".p2").scroll({left:200});

document.getElementById("button2").addEventListener("click", handler2, false)

4 Comments

any better way ..! ?
Sure, skip javascript and use CSS animations instead.
can you also share that example also
Well, it still really depends on what is your use case. Do you want it for some button animation, banner animation? ...or do you want to move whole page content with it?

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.