Home  >  Article  >  Web Front-end  >  Seamless scrolling effect up and down implemented by javascript

Seamless scrolling effect up and down implemented by javascript

高洛峰
高洛峰Original
2017-01-18 13:41:401282browse

The example in this article describes the up and down seamless scrolling effect achieved by javascript. Share it with everyone for your reference, the details are as follows:

I introduced the JS left and right seamless scrolling effect before, now I will do the seamless scrolling-up and down effect. The other codes are similar to the left and right, except that the value of offsetTop is changed, and there is no need to calculate the overall width of ul.

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="UTF-8">
  <title>无缝滚动——上下</title>
  <style type="text/css">
  *{margin:0;padding:0;}
  li{list-style:none;}
  img{border:0;}
  #scroll{width:178px;margin:50px auto;position:relative;}
  .btn{display:block;width:27px;height:27px;margin-left:auto;margin-right:auto;cursor:pointer;}
  .up{background:url(images/up.gif);}
  .down{background:url(images/down.gif);}
  .content{margin:10px 0;height:440px;overflow:hidden;position:relative;}
  .content ul{position:absolute;top:0;left:0;}
  .content li{height:110px;}
  </style>
</head>
<body>
  <div id="scroll">
    <a href="javascript:;" id="up" class="btn up"></a>
    <div>
      <ul>
        <li><a href="#" title="111"><img src="images/1.jpg" alt="111" width="178" height="108"/></a></li>
        <li><a href="#" title="222"><img src="images/2.jpg" alt="222" width="178" height="108"/></a></li>
        <li><a href="#" title="333"><img src="images/3.jpg" alt="333" width="178" height="108"/></a></li>
        <li><a href="#" title="444"><img src="images/4.jpg" alt="444" width="178" height="108"/></a></li>
      </ul>
    </div>
    <a class="btn down" href="javascript:;" id="down"></a>
  </div>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
  var oDiv = document.getElementById(&#39;scroll&#39;);
  var btnUp = document.getElementById(&#39;up&#39;);
  var btnDown = document.getElementById(&#39;down&#39;);
  var oUl = oDiv.getElementsByTagName(&#39;ul&#39;)[0];
  var timer = null;
  var speed = -1;
  oUl.innerHTML += oUl.innerHTML;
  setTimeout(move,1500);
  btnUp.onclick = function(){
    clearInterval(timer);
    speed = -1;
    move();
  };
  btnDown.onclick = function(){
    clearInterval(timer);
    speed = 1;
    move();
  };
  oUl.onmouseover = function(){
    clearInterval(timer);
  };
  oUl.onmouseout = function(){
    move();
  };
  function move(){
    timer = setInterval(function(){
      oUl.style.top = oUl.offsetTop + speed + &#39;px&#39;;
      if(oUl.offsetTop <= - oUl.offsetHeight / 2){
        oUl.style.top = &#39;0&#39;;
      }else if(oUl.offsetTop >= 0){
        oUl.style.top = - oUl.offsetHeight / 2 + &#39;px&#39;;
      };
    },30);
  };
};
</script>

If you want to change the movement speed, just modify the value of speed. The initial default is to move upward, that is, the speed is a negative number.

I hope this article will be helpful to everyone in JavaScript programming.

For more articles related to the up and down seamless scrolling effect implemented by javascript, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn