Home > Web Front-end > JS Tutorial > body text

js to achieve seamless scrolling effects_javascript skills

WBOY
Release: 2016-05-16 15:24:30
Original
1131 people have browsed it

The example in this article introduces the functions that js needs to achieve seamless scrolling effects, as well as the key js code, and shares it with you for your reference. The specific content is as follows

Operation rendering:

Combined with the knowledge learned below, make a simulated comprehensive extended exercise~~ The general function is as follows:

  • 1. After clicking on the html, the picture will automatically move and display
  • 2. Click the left and right directions to change the direction of the picture movement (change the value of left, positive or negative)
  • 3. After the mouse moves in and out of the image, the image will pause (setInterval, clearInterval)
  • 4. Move the mouse over the picture and highlight it (a:hover)
  • 5. Click on the small picture, the big picture below will change
  • 6. The text area changes as the picture changes (unsuccessful, needs to be improved)

Specific code:

window.onload = function(){

  //声明部分( 现在的习惯是要写什么声明什么, 一起写容易搞忘了。。也不知道好不好这样)

  var oDiv = document.getElementById('box');

  var oUl = oDiv.getElementsByTagName('ul');

  var oLi = oUl.getElementsByTagName('li');

  var speed = 2;

  var timer = null;

   

  //让ul的内容增一倍,从而实现无缝滚动

  oUl.innerHTML = oUl.innerHTML + oUl.innerHTML;

  oUl.style.width = oLi[1].offsetWidth * oLi.length + 'px';

  //move函数

  function move(){
    oUl.style.left = oUl.offsetLeft + speed + 'px';
    //控制左
    if(oUl.offsetLeft < -oUl.offsetWidth/2){
    oUl.style.left = 0;
    }

    //控制右
    if(oUl.offsetLeft > 0){
    oUl.style.left = -oUl.offsetWidth/2 + 'px';
    }

  }

    //图标点击~ 控制移动方向

    var oLeft = document.getElementById('jt_left');
    var oRight= document.getElementById('jt_right');

    oLeft.onclick = function(){
    speed = -2;
    }

    oRight.onclick = function(){
    speed = 2;
    }

    //鼠标移入移出效果

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

    oDiv.onmouseout = function(){
      timer = setInterval(move,20);
    }
    timer = setInterval(move,20); 

    //点击获取大图

    

    var aA = oDiv.getElementsByTagName('a');
    for(var i=0;i<aA.length;i++){
      aA[i].onclick = function(){
        showPic(this);
        return false;
      }
    }
    
    function showPic(whichpic){
      var source = whichpic.href;
      var placeholder = document.getElementById('placeholder');
      placeholder.src = source;
    }

}
Copy after login

For the final text replacement effect, I wanted to use pictures to make the numbers corresponding to the text box change~~ It failed. I don’t know the reason. This aspect still needs to be improved. I hope everyone can provide some good suggestions. However, seamless scrolling in js is still implemented normally. I hope it can be helpful to everyone.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!