zoukankan      html  css  js  c++  java
  • 轮播效果

    Js实现:

      

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
          * {
                margin: 0;
                padding: 0;
                text-decoration: none;
            }
            
            body {
                padding: 20px;
            }
            
            .container {
                position: relative;
                 600px;
                height: 400px;
                border: 3px solid #333;
                overflow: hidden;
            }
            
            .list {
                position: absolute;
                z-index: 1;
                 4200px;
                height: 400px;
               
            }
            
            .list img {
                float: left;
                 600px;
                height: 400px;
            }
            
            .buttons {
                position: absolute;
                left: 250px;
                bottom: 20px;
                z-index: 2;
                height: 10px;
                 100px;
            }
            
            .buttons span {
                float: left;
                margin-right: 5px;
                 10px;
                height: 10px;
                border: 1px solid #fff;
                border-radius: 50%;
                background: #333;
                cursor: pointer;
            }
            
            .buttons .on {
                background: orangered;
            }
            
            .arrow {
                position: absolute;
                top: 180px;
                z-index: 2;
                display: none;
                 40px;
                height: 40px;
                font-size: 36px;
                font-weight: bold;
                line-height: 39px;
                text-align: center;
                color: #fff;
                background-color: RGBA(0, 0, 0, .3);
                cursor: pointer;
            }
            
            .arrow:hover {
                background-color: RGBA(0, 0, 0, .7);
            }
            
            .container:hover .arrow {
                display: block;
            }
            
            .prev {
                left: 20px;
            }
            
            .next {
                right: 20px;
            }
            
         
           
        </style>
    </head>
    
    <body>
    
        <div class="container">
            <div class="list" style="left: -600px;">
                <img src="images/img_5.jpg" class="img-active-enter" alt="1" />
                <img src="images/img_1.jpg" alt="1" />
                <img src="images/img_2.jpg" alt="2" />
                <img class="img-active-leave" src="images/img_3.jpg" alt="3" />
                <img src="images/img_4.jpg" alt="4" />
                <img src="images/img_5.jpg" alt="5" />
                <img src="images/img_1.jpg" alt="5" />
            </div>
            <div class="buttons">
                <span index="1" class="on"></span>
                <span index="2"></span>
                <span index="3"></span>
                <span index="4"></span>
                <span index="5"></span>
            </div>
            <a href="javascript:;" class="prev arrow">&lt;</a>
            <a href="javascript:;" class="next arrow">&gt;</a>
        </div>
       
        
        <script type="text/javascript">
           window.onload = function() {
              var container = document.getElementsByClassName('container')[0];
              var list = document.getElementsByClassName('list')[0];
              var prev = document.getElementsByClassName('prev')[0];
              var next = document.getElementsByClassName('next')[0];
              var buttons = document.getElementsByClassName('buttons')[0].getElementsByTagName('span');
    
              var index = 1;
              // 暂停标识
              var stopFlag = false;
              
              function buttonsShow() {
                for (var i=0; i< buttons.length; i++) {
                  if (buttons[i].className === 'on') {
                    buttons[i].className = '';
                  }
                }
                buttons[index -1].className = 'on';
              }
              
              prev.onclick = function () {
               if (stopFlag) {
                  return;
                }
                index -= 1; 
                if (index < 1) {
                  index = 5
                }
                buttonsShow();
                animate(600)
              }
              
              next.onclick = function () {
                if (stopFlag) {
                  return;
                }
                index += 1;
                if (index > 5) {
                  index = 1
                 }
                 buttonsShow();
                 animate(-600);
              }
              
              for (let i = 0; i< buttons.length; i++) {
                buttons[i].onclick = function () {
                  var clickIndex = parseInt(this.getAttribute('index'));
                  var offset = 600 * (index - clickIndex);
                  animate(offset);
                  index = clickIndex;
                  buttonsShow();
                }
              }
    
              function animate(offset) {
                  //获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
                  //且style.left获取的是字符串,需要用parseInt()取整转化为数字。
                  var newLeft = parseInt(list.style.left) + offset;
                   if(newLeft<-3000){
                      list.style.left = -3600 + 'px';
                      list.style.transition = 'left 1s';
                      stopFlag = true;
                      setTimeout(function(){
                        list.style.left= -600 + 'px'
                        list.style.transition='left 0s'; 
                        stopFlag = false;
                      },1000)
                      return;
                   }
                   if(newLeft>-600){
                       list.style.left = 0 + 'px';
                       list.style.transition = 'left 1s'
                       stopFlag = true;
                       setTimeout(function(){
                        list.style.left='-3000px'
                        list.style.transition='left 0s'; 
                        stopFlag = false;
                      },1000)
                       return;
                   }
                 
                   list.style.left = newLeft + 'px';
                   list.style.transition = 'left 1s'
              }
              
              // 创建定时器
              var timer;
              function play() {
                timer = setInterval(function () {
                  next.onclick();
                }, 1500)
              }
              
              function stop() {
                clearInterval(timer)
              }
              play();
              
              container.onmouseover = stop;
              container.onmouseout = play;
            }
            
            
        </script>
    
    </body>
    
    </html>
  • 相关阅读:
    Markdown学习笔记
    Go 学习笔记(一)
    case中定义变量
    <转>MySql 与Oracle区别
    Java 时间转换问题总结
    线程之间共享
    并发编程快速入门
    redis主从复制
    jedis操作redis
    redis持久化方案
  • 原文地址:https://www.cnblogs.com/yaobolove/p/8546830.html
Copyright © 2011-2022 走看看