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
- 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>