<!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. selenium webdriver 屏幕滚动

    //山寨法 String setscroll = "document.documentElement.scrollTop=100"; JavascriptExecutor jse= ...

  2. 常用FTP命令汇总

    FTP是File Transport Protocol的简称,其作用是使连接到服务器上的客户可以在服务器和客户机间传输文件.除WWW服务外,FTP也算是使用最广泛的一种服务了.在cmd中ftp命令很多 ...

  3. Atom离线插件安装

    1.下载原始的插件包 2.解压放入atom的packages文件夹中 3.通过nodejs的npm指令进行安装 运行->cmd 4.重启atom就好了.

  4. CentOS 7 启动流程

    1.UEFI或BIOS初始化,运行POST开机自检 2.选择启动设备,Boot Sequence 系统启动次序一般含有:光驱.U盘.硬盘A.硬盘B.按次序查找各引导设备,第一个有引导程序的设备即为本次 ...

  5. vscode调试开发C/C++程序

    https://www.cnblogs.com/TAMING/p/8560253.html

  6. 58按之字形顺序打印二叉树 +队列访问使用front和back,栈才是top

    题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.   思路:最暴力的方法就是使用队列进行层次遍 ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示一个警告的操作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. SSH和screen服务

    SSH是一种能够以安全的方式提供远程登录的协议,目前远程管理的首选方式,sshd是基于SSH协议开发的一款远程管理服务程序,在Linux系统中需要部署sshd服务程序才能使用SSH协议来进行远程管理, ...

  9. Linux中{ }的用法

    一.生成序列 格式:{#..#},按照ASCII表的顺序进行生成,如{a..c}表示a b c,也可以{c..a}倒叙的形式生成c b a # ..} # echo {z..a} z y x w v ...

  10. JS截取腾讯视频和去除视频广告

    一:腾讯视频截取 H5视频播放除了video标签以外,还有iframe嵌套视频 项目需求是用户输入腾讯视频的html链接,如 https://v.qq.com/x/page/y0116k2vspw.h ...