Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<style></style>
<body>
<div class="container">
  <div class="item">第1条</div>
  <div class="item">第2条</div>
  <div class="item">第3条</div>
  <div class="item">第4条</div>
  <div class="item">第5条</div>
  <div class="item">第6条</div>
  <div class="item">第7条</div>
</div>
<button class="button">跳转到第1条</button>
<button class="button">跳转到第5条</button>
</body>
</html>
 
*{
  padding: 0;
  margin: 0;
}
.container{
  width: 100%;
  height: 300px;
  overflow-x: hidden;
  overflow-y:auto;
}
.item{
  width100%;
  height: 100px;
  line-height: 100px;
  margin-bottom: 10px;
  text-align: center;
  color: #fff;
  background-color: green;
}
.button{
  padding: 15px;
}
 
var items = document.querySelectorAll('.item');
var button = document.querySelectorAll('.button');
button[0].addEventListener('click', function(){
  items[0].scrollIntoView();
}, false);
button[1].addEventListener('click', function(){
  items[4].scrollIntoView();
}, false);
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers