Home > Web Front-end > JS Tutorial > The improved version of seamless scrolling supports up, down, left and right scrolling (encapsulated into functions)_javascript skills

The improved version of seamless scrolling supports up, down, left and right scrolling (encapsulated into functions)_javascript skills

WBOY
Release: 2016-05-16 17:47:18
Original
1129 people have browsed it

复制代码 代码如下:




   
    无缝滚动——上下
   


   

       
       
       

           

                   
  • 111

  •                
  • 222

  •                
  • 333

  •                
  • 444

  •            

       

   




           

scroll.js:

复制代码 代码如下:

/**********
Function: Achieve horizontal or vertical seamless scrolling
Parameters: direction, a total of 4 values: left, right, top, bottom
Speed ​​moving distance
iTime How much time will it take to start moving? If you don’t write it, the page will start moving after loading
**********/
function scroll(direction,speed,iTime){
    var oDiv = document.getElementById('scroll');
    var oUl = oDiv.getElementsByTagName('ul')[0];
    var aLi = oDiv.getElementsByTagName('li');
    var aBtn = oDiv.getElementsByTagName('a');
    var timer = null;
    var iSpeed = 0;
    var flag = true;    //判断水平移动还是垂直移动

    oUl.innerHTML = oUl.innerHTML;

    switch(direction){
        case 'left':
            iSpeed = -speed;
            oUl.style.width = aLi[0].offsetWidth * aLi.length 'px';
            flag = true;
            break;
        case 'right':
            iSpeed = speed;
            oUl.style.width = aLi[0].offsetWidth * aLi.length 'px';
            flag = true;
            break;
        case 'top':
            iSpeed = -speed;
            flag = false;
            break;
        case 'bottom':
            iSpeed = speed;
            flag = false;
            break;
    };

    setTimeout(move,iTime);

    //左按钮和上按钮
    aBtn[0].onclick = function(){
        clearInterval(timer);
        iSpeed = -speed;
        move();
    };

    //右按钮和下按钮
    aBtn[1].onclick = function(){
        clearInterval(timer);
        iSpeed = speed;
        move();
    };

    oUl.onmouseover = function(){
        clearInterval(timer);
    };

    oUl.onmouseout = function(){
        move();
    };

    function move(){   
        timer = setInterval(function(){
            if(flag){
                oUl.style.left = oUl.offsetLeft iSpeed 'px';
                if(oUl.offsetLeft < -oUl.offsetWidth / 2){
                    oUl.style.left = '0';
                }else if(oUl.offsetLeft > 0){
                    oUl.style.left = - oUl.offsetWidth / 2 'px';
                }
            }else{
          oUl.style.top = oUl.offsetTop                                                                                           Ul.style.top = '0' ;
             }else if(oUl.offsetTop >= 0){
                                                                                                                                                                  ; 🎜>                                                        ,30);
};
};


It should be noted that the html structure must be like the above structure.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template