<!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. PTA的Python练习题(十一)

    从 第4章-3 猴子吃桃问题 继续 1. a=eval(input()) def count(n): b=1 for i in range(n-1): b=(b+1)*2 return b print ...

  2. P1025数的划分

    P1025数的划分 #include <iostream> using namespace std; int n,k; int cnt; void dfs(int s,int step,i ...

  3. gitlab两种连接方式:ssh和http配置介绍 --转自 散尽浮华

    gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http 1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下图 ...

  4. Keepalived+Nginx解决方案实现高可用的API网关(nginx)

    一. 采用Keepalived+Nginx解决方案实现高可用的API网关. 2.1 Nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP ...

  5. SRS命令

    下载源码: git clone https://github.com/ossrs/srs.git 编译: cd srs/trunk ./configure && make 开启服务: ...

  6. 一键GHOST使用图文教程

    一.准备工作 系统安装好以后,对系统进行及时备份,以防不测,以后恢复时三五分钟即可完成,还你一个全新的完整系统. 一键GHOST V8.3 Build 060903 硬盘版免费下载 从以上地址下载软件 ...

  7. 六 一对多关联查询&关联查询小结

    一对多关联查询:基于用户表关联查询订单表 在pojo中,一的一方方式多的一方的集合 在代理映射中配置查询方法,ResultMap一对多关系(注意:当两表有字段重名时,在一方字段设置别名,以免造成查询混 ...

  8. kafka在zookeeper默认使用/为根目录,将/更换为/kafka

    需求:kafka在zookeeper默认使用/为根目录,将/更换为/kafka 步骤:1.进入kafka的根目录: [root@node01 kafka_2.11-1.0.0]# cd /export ...

  9. JS控制输入框和文本框字数

    文本框限制字数: HTML结构: JS: $('.advert-title').each(function(){ var TXTlength = $(this).text().length; // 当 ...

  10. 关于C/C++的各种优化

    一.常量 声明常量可以方便代码的修改,提高复用性. ; +; ; 同时,声明常量也可以减少重复运算,提高代码速度,例子如下: string s; cin>>s; ;i<len;i++ ...