<!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. GIS中DTM/DEM/DSM/DOM的含义

    DTM(Digital Terrain Model):数字地面模型,是一个表示地面特征空间分布的数据库,一般用一系列地面点坐 标(x,y,z)及地表属性(目标类别.特征等)绗成数据阵列,以此组成数字地 ...

  2. struts标签 解析html标签

    参考:http://blog.csdn.net/shuangrenyu1234/article/details/24527745

  3. tomcat启动报错failed to start component

    严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catali ...

  4. PCSearch需要管理员权限,开机自启

    1.添加Windows服务,并设为自动启动: 2.通过服务启动AutoStartSevice.exe,通过AutoStartSevice.exe运行AutoStart.bat,通过AutoStart. ...

  5. AD7705/TM7705使用注意事项

    http://bbs.eeworld.com.cn/thread-66467-1-1.html 1 空闲是SCLK=1(C POA),DIN=1(因为写寄存器都是从写通讯寄存器开始,此时第8位必须为0 ...

  6. bat批处理下如何像shell一样将命令执行的效果赋值给变量

    在bat下如何实现像shell一样,把执行命令行后的结果赋值给变量呐? 刚开始,可真难为到我了.随着对bat批处理知识熟悉的加深. 学习到了!!! 举个 栗子: svnlook  uuid  C:\R ...

  7. js 判断时间大小

    //判断结束时间一定要大于开始时间 function comparativeTime(){ var isok=true; //早餐配送时间 var breakfastScanTimeMin = $(& ...

  8. [原]JointJS流程图

    最近项目上需要用流程图来做问题定界分析,之前有同事用jsPlumb做过,但是阅读代码后觉得比较麻烦,所以自己又找了一圈,找到一个叫Dagre-D3的开源类库,画出来的效果如下图,Dagre-D3最大的 ...

  9. [CMake笔记] 初识CMake

    与CMake相遇 做C/C++的人都应该经历过,以往拿到一个开源代码,想自己编译出来,总会因为VS的版本不同而出现各种各样的问题,使编译过程困难重重,更多时候就会放弃使用它了.而这个过程反过来,也会使 ...

  10. Gamma 矫正

    参考如下链接: https://www.zhihu.com/question/27467127