jQuery Event How to - Click to do mousewheel up and down








Question

We would like to know how to click to do mousewheel up and down.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.4.js'></script>
<style type='text/css'>
div {<!--from w w  w.j  a  v a  2 s . c o  m-->
  width: 200px;
  height: 200px;
  background: grey;
  overflow: scroll;
}

div span {
  width: 200px;
  height: 1000px;
  display: block;
}

a {
  cursor: pointer;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $('#up').click(function() {
        $('div').stop().animate({scrollTop: '-=100'}, 300); 
    });
    $('#down').click(function() {
        $('div').stop().animate({scrollTop: '+=100'}, 300); 
    });
});
</script>
</head>
<body>
  <div>
    <span></span>
  </div>
  <a id="up">up</a>
  <a id="down"> down</a>
</body>
</html>

The code above is rendered as follows: