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

Sample code sharing for implementing rotating carousel chart using JavaScript

黄舟
Release: 2017-08-20 10:11:54
Original
1533 people have browsed it

This article mainly introduces the implementation of rotating carousels in JavaScript in detail, which has certain reference value. Interested friends can refer to it

I have been learning JavaScript recently, and then saw the rotation In the case of the carousel chart, I tried to use js to make a simple carousel chart. Because the dynamic effect could not be displayed, I put a screenshot:

This The effect is like this: there are 7 pictures in total, and they will automatically slide to the left. Then the left and right arrows can also control the left and right sliding of the carousel. At the same time, if the mouse stops on the picture, the carousel will stop. Automatic sliding, when the mouse is moved away, the carousel will continue to the left.
First of all, I have encapsulated two functions here (because I haven’t learned jQuery yet, so I used the method of encapsulating functions to implement it). The first function is the $ function, which can be called to get the elements in html. The code is as follows:


`function $(select){
    if (typeof select != 'string') {
      console.log('传入的参数有误');     
      return null;
    }
    var firstChar = select.charAt(0);
    switch(firstChar){
      case '#':
        return document.getElementById(select.substr(1));
      break;
      case '.':
        if (document.getElementsByClassName){
          return document.getElementsByClassName(select.substr(1));
        } else {
          var result = [];
          var allElemnts = document.getElementsByTagName('*');
          console.log(allElemnts);
          for (var i = 0; i < allElemnts.length; i++){
            var e = allElemnts[i];
            var className = e.className;
            var classArr = className.split(&#39; &#39;);
            for (var j = 0; j < classArr.length; j++){
              var c = classArr[j];
              if (c == select.substr(1)) {
                result.push(e);
                break;
              }
            }
          }
          return result;
        }
      break;
      default :
        return document.getElementsByTagName(select);
    }
  }`
Copy after login

The second function is an animation function that uses json to dynamically change multiple styles to achieve an animation effect. The code is as follows: `


function animate(element, json, fun) {
  clearInterval(element.timer);
  function getStyle(element, styleName){
    if(element.currentStyle){
      //ie浏览器下 直接通过currentstyle来获取
      //return element.currentStyle.heigh;
      return element.currentStyle[styleName];
    }else{
      var computedStyle = window.getComputedStyle(element,null);
      return computedStyle[styleName];
    }
  }
  var isStop = false;
  element.timer = setInterval(function () {
    isStop = true;
    for (var key in json){
      var target = json[key];
      var current;
      if (key == &#39;opacity&#39;) {
        //当动画的类型为透明度时 获取的值应该是浮点类型
        current = parseFloat(getStyle(element, key)) || 1;
      } else {
        //其他情况 用整数类型
        current = parseInt(getStyle(element, key)) || 0;
      }      
      var step = (target - current) / 10;
      if (key != &#39;opacity&#39;) {
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
      }
      current += step;
      if (key == &#39;opacity&#39;) {
        if (Math.abs(target - current) > 0.01) {
          isStop = false;
        } else {
          current = target;
        }
        element.style.opacity = current + &#39;&#39;;
      } else {
        if (Math.abs(target-current) > Math.abs(step)) {
          isStop = false;
        } else {
          current = target;
        }
        if (key == &#39;zIndex&#39;){
          element.style.zIndex = Math.round(current);
        } else {
          element.style[key] = current + &#39;px&#39;;
        }        
      }      
    }
    if (isStop) {
      clearInterval(element.timer);
      console.log(&#39;动画完成&#39;);
      if (typeof fun == &#39;function&#39;) {
        fun();
      }
    }
  }, 30);
}`
Copy after login

The next step is to write the html part. Because there are only a few pictures, the html part is very simple:


<body>
  <p class="box">
    <p class="content">
      <ul>
        <li><a href="#"><img src="./images/1.jpg"></a></li>
        <li><a href="#"><img src="./images/2.jpg"></a></li>
        <li><a href="#"><img src="./images/3.jpg"></a></li>
        <li><a href="#"><img src="./images/4.jpg" class="current"></a></li>
        <li><a href="#"><img src="./images/5.jpg"></a></li>
        <li><a href="#"><img src="./images/6.jpg"></a></li>
        <li><a href="#"><img src="./images/7.jpg"></a></li>
      </ul>
    </p>
    <p class="control">
      <a href="javascript:;" id="prev"></a>
      <a href="javascript:;" id="next"></a>
    </p>
  </p>
</body>
Copy after login

There is not much description about the css style part.

The following is the JS part, the code is also very simple, just clarify it


 window.onload = function(){
  //定位,并给图片设置大小透明度
  var json = [{
    width: 630,
    top: 23,
    left: 0,
    zIndex: 2,
    opacity: 0
  },{
    width: 630,
    top: 23,
    left: 0,
    zIndex: 3,
    opacity: 0
  },{
    width: 630,
    top: 23,
    left: 0,
    zIndex: 4,
    opacity: 0.6
  },{
    width: 730,
    top: 0,
    left: 125,
    zIndex: 5,
    opacity: 1
  },{
    width: 630,
    top: 23,
    left: 350,
    zIndex: 4,
    opacity: 0.6
  },{
    width: 630,
    top: 23,
    left: 350,
    zIndex: 3,
    opacity: 0
  },{
    width: 630,
    top: 23,
    left: 350,
    zIndex: 2,
    opacity: 0
  }];
Copy after login


function refreshImageLocatin(index){
    //默认情况下 第i张图对应第i个位置
    //index=1时 第i个图对应i-1个位置
    //也就是第i个图对应i-index的位置
    var liArr = $(&#39;li&#39;);
    for(var i = 0; i < liArr.length; i++){
      var li = liArr[i];
      var locationIndex = i - index;
      console.log(&#39;i=&#39;+i);
      console.log(&#39;index=&#39;+index);
      console.log(&#39;locationIndex=&#39;+locationIndex);
      if(locationIndex < 0){
        locationIndex += 7;
      }
      var locationData = json[locationIndex];
      animate(li, locationData, null);
    }
  }

  refreshImageLocatin(0);

  var index = 0;
  $(&#39;#next&#39;).onclick = function(){
    index++;
    if(index == 7){
      index = 0;
    }
    refreshImageLocatin(index);
  }
  $(&#39;#prev&#39;).onclick = function(){
    index--;
    if(index < 0){
      index = 6;
    }
    refreshImageLocatin(index);
  }

  var nextImage = $(&#39;#next&#39;).onclick;
  var contentBox = $(&#39;.content&#39;)[0];
  //自动播放
  var timer = setInterval(nextImage, 3000);
  //当鼠标移动到图片上,停止播放
  contentBox.onmouseover = function(){
    clearInterval(timer);
  }
  contentBox.onmouseout = function(){
    timer = setInterval(nextImage ,3000)
  }
}
Copy after login

The above is the detailed content of Sample code sharing for implementing rotating carousel chart using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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