<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title></title>
  <style>
   * {
    margin: 0px;
    padding: 0px;
   }
   
   #stage {
    width: 500px;
    height: 300px;
    border: 3px solid black;
    margin: 100px;
    overflow: hidden;
    position: relative;
   }
   
   #banner {
    width: 3000px;
    height: 300px;
    background-color: rosybrown;
   }
   
   .items {
    width: 500px;
    height: 300px;
    color: white;
    font-size: 150px;
    text-align: center;
    line-height: 300px;
    float: left;
   }
   
   .btn-l,
   .btn-r {
    width: 50px;
    height: 300px;
    background-color: black;
    opacity: 0.5;
    color: white;
    font-size: 40px;
    line-height: 300px;
    text-align: center;
    position: absolute;
    top: 0px;
   }
   
   .btn-l {
    left: 0px;
   }
   
   .btn-r {
    right: 0px;
   }
   
   .btn-l:hover,
   .btn-r:hover {
    cursor: pointer;
    opacity: 0.2;
   }
   
   .points {
    position: absolute;
    bottom: 10px;
    left: 200px;
    /*background-color: orangered;*/
    width: 125px;
    height: 20px;
   }
   
   .dot {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: gainsboro;
    float: left;
    margin-right: 5px;
    cursor: pointer;
   }
   
   .active {
    background-color: red;
   }
  </style>
 </head>

<body>
  <div id="stage">
   <div class="btn-l">
    <</div>
     <div class="btn-r">></div>
     <div id="banner">
      <div class="items" style="">1</div>
      <div class="items" style="">2</div>
      <div class="items" style="">3</div>
      <div class="items" style="">4</div>
      <div class="items" style="">5</div>
      <div class="items" style="">1</div>
     </div>
     <div class="points">
      <div data="1" class="dot active"></div>
      <div data="2" class="dot"></div>
      <div data="3" class="dot"></div>
      <div data="4" class="dot"></div>
      <div data="5" class="dot"></div>
     </div>
   </div>
 </body>

</html>
<script>
// 取对象
 var btn_l = document.getElementsByClassName('btn-l')[0];
 var btn_r = document.getElementsByClassName("btn-r")[0];
 var banner = document.getElementById("banner");
 var dots = document.getElementsByClassName('dot');
// 定义变量
 var count = 1;
 var arr = [];
// 右侧按钮点击事件
 btn_r.onclick = function() {
  if(count < 6) {
   count++;
   arr.push(window.setInterval("move_left()", 20));
  } else if(count == 6) {
   count = 1;
   banner.style.marginLeft = 0 + 'px';
   count++;
   arr.push(window.setInterval("move_left()", 20));
  }
  for(var i = 0; i < dots.length; i++) {
   dots[i].setAttribute("class", "dot");
  }
  dots[count - 1].setAttribute("class", "dot active");
 }
// 左侧按钮点击事件
 btn_l.onclick = function() {
  if(count > 1) {
   count--;
   arr.push(window.setInterval("move_right()", 20));
  }else if(count == 1){
   count = 6;
   banner.style.marginLeft = -2500 + 'px';   
   count--;
   arr.push(window.setInterval("move_right()", 20));
  }
  
  for(var i = 0; i < dots.length; i++) {
   dots[i].setAttribute("class", "dot");
  }
  dots[count - 1].setAttribute("class", "dot active");
 }
// 向左移动
 function move_left() {
  if(banner.offsetLeft == (count - 1) * (-500)) {
   clear();
  } else {
   banner.style.marginLeft = banner.offsetLeft - 20 + "px";
  }

}
// 向右移动
 function move_right() {
  if(banner.offsetLeft == (count - 1) * (-500)) {
   clear();
  } else {
   banner.style.marginLeft = banner.offsetLeft + 20 + "px";
  }
 }
// 清除所有间隔执行
 function clear() {
  for(var x in arr) {
   window.clearInterval(arr[x]);
  }
 }
// 批量添加点击事件
 for(var j = 0; j < dots.length; j++) {
  dots[j].onclick = function() {
   count_s = this.getAttribute("data");
   if(count > count_s) {
    count = count_s;
    arr.push(window.setInterval("move_right()", 20));
   } else if(count < count_s) {
    count = count_s;
    arr.push(window.setInterval("move_left()", 20));
   }

for(var i = 0; i < dots.length; i++) {
    dots[i].setAttribute("class", "dot");
   }
   this.setAttribute("class", "dot active");
  }
 }
// 自动轮播
 function auto_move(){
  if(count < 6) {
   count++;
   arr.push(window.setInterval("move_left()", 20));
  } else if(count == 6) {
   count = 1;
   banner.style.marginLeft = 0 + 'px';
   count++;
   arr.push(window.setInterval("move_left()", 20));
  }
  for(var i = 0; i < dots.length; i++) {
   dots[i].setAttribute("class", "dot");
  }
  dots[count - 1].setAttribute("class", "dot active");
 } 
 window.setInterval("auto_move()",3000);
</script>

JS: 图片轮播模板——左右移动,点击编码移动,自动轮播的更多相关文章

  1. js图片轮播效果实现代码

    首先给大家看一看js图片轮播效果,如下图 具体思路: 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒钟i ...

  2. JS图片轮播[左右轮播

    直接可以用,网上摘下来的! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  3. js 图片轮播简单版

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  4. js 图片轮播(一)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. js 图片轮播代码编辑

    图片轮播,将几张图片统一放在展示平台 banner上,通过banner移动将图片轮流播放. <script>// 取对象 var btn_l = document.getElementsB ...

  6. angular js 图片轮播

    搬运工: eg1: Build a Sweet AngularJS Photo Slider Pt 2 with ngTouch DEMO:http://paul-xiao.github.io/ang ...

  7. js图片轮播图

    /*焦点图*/        var Box='.carousel';//盒子        var Menu=$(Box+' .l_cursor li');//圆点菜单        var Con ...

  8. Js 图片轮播渐隐效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. 非常简洁的js图片轮播

    <div id="tupian"></div><script>var jpg =new Array();jpg[0]="url(c.j ...

随机推荐

  1. Java基础知识笔记第十章:输入输出流

    File类 文件的属性 目录 文件的创建与删除 运行可执行文件 文件字节输入流 文件字节输出流 文件字符输入输出流 缓冲流 随机流 数组流 数据流 对象流 序列化与对象克隆 使用Scanner解析文件 ...

  2. ElementUI 日期选择器 datepicker 选择范围限制

    在使用elementUI中日期选择器时,经常会遇到这样的需求——对可选择的时间范围有一定限制,比如我遇到的就是:只能选择今天以前的一年以内的日期. 查阅官方文档,我们发现它介绍的并不详细,下面我们就来 ...

  3. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  4. rails work

    4.2 Say "Hello", Rails problem weppack not install solve run the command rails webpacker:i ...

  5. DAC

    DAC的功能:可以输出电压(无触发,设置DAC转换值,DAC使能更新寄存器和启动转换)或者波形(连续转换,用DMA发送数据,定时器触发).首先有个正弦波波形的数组,内存到DAC的DMA通道不断的将正弦 ...

  6. ES6新语法概览

    简介 ES6是JavaScript语言的新一代标准,加入了一些新的功能和语法,正式发布于2015年6月,亦称ES2015:该标准由ECMA(欧洲计算机制造联合会)的第39号技术专家委员会(TC39)制 ...

  7. 设计模式课程 设计模式精讲 8-10 单例设计模式-ThreadLocal线程单例

    1 课程讲解 1.1 应用场景 2 代码演练 2.1 threadLocal应用 1 课程讲解 1.1 应用场景 多线程的时候: 使用同步锁使用时间换空间的方式,(线程排队时间比较长) 而使用thre ...

  8. Docker常用命令(命令大全)

    容器生命周期管理 1. docker run:创建一个新的容器并运行一个命令 2. Docker start/stop/restart - docker start:启动一个或多个已经被停止的容器 - ...

  9. keep-alive 用法 及activated,deactivated这两个生命周期函数

    keep-aliveProps: include - 字符串或正则表达式.只有名称匹配的组件会被缓存.exclude - 字符串或正则表达式.任何名称匹配的组件都不会被缓存.max - 数字.最多可以 ...

  10. 对于strlen()函数的一点小疑问

    看csapp时候,看一下char*[8],以为char*也是一字节,但是指针是地址,64位编译器下是8字节,所以sizeof(B)是64字节 后来又看strlen(). #include"s ...