当前位置: 首页 > news >正文

轮播效果

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;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }
        
        .list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
           
        }
        
        .list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        
        .buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
        }
        
        .buttons span {
            float: left;
            margin-right: 5px;
            width: 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;
            width: 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>

 

转载于:https://www.cnblogs.com/yaobolove/p/8546830.html

相关文章:

  • python 获取网页内容新增网页分类+删除指定后缀数组元素功能(基于python 3.6)...
  • 我是她的奇迹
  • 解决 Boost安装:fatal error: bzlib.h: No such file or directory 问题
  • 李益:展览工程“绿色供应链”解决之道
  • 当然我在扯淡
  • 另类起床提示 模仿iPod的数字时钟
  • 《2017年人工智能行业发展研究报告白皮书》发布 以数洞见行业未来
  • 【315打假】去信任的区块链技术 难以阻止的失信项目
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • iOS 实现语音边录边传
  • chrome离线包出现的小问题
  • Linux运维学习笔记-定时任务知识总结
  • 购物车程序练习
  • 使用Promise链式调用解决多个异步回调的问题
  • JetBrains发布Kotlin 1.2.30
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • create-react-app做的留言板
  • jquery cookie
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Mysql5.6主从复制
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Yeoman_Bower_Grunt
  • 阿里云前端周刊 - 第 26 期
  • 从PHP迁移至Golang - 基础篇
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 类orAPI - 收藏集 - 掘金
  • 事件委托的小应用
  • 通过npm或yarn自动生成vue组件
  • ![CDATA[ ]] 是什么东东
  • $.each()与$(selector).each()
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (二)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .net 验证控件和javaScript的冲突问题
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .php文件都打不开,打不开php文件怎么办
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @data注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [ linux ] linux 命令英文全称及解释
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序
  • [C#基础]说说lock到底锁谁?
  • [Codeforces1137D]Cooperative Game
  • [Docker]六.Docker自动部署nodejs以及golang项目
  • [EULAR文摘] 脊柱放射学持续进展是否显著影响关节功能
  • [javaee基础] 常见的javaweb笔试选择题含答案
  • [Java算法分析与设计]--线性结构与顺序表(List)的实现应用
  • [Oh My C++ Diary]带参数的main()函数