<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
  margin: 0;
  padding: 0;
}
.hezi {
  width: 500px;
  height: 300px;
  padding: 10px;
  margin: 100px auto;
  position: relative;
}
#tupian {
  width: 500px;
  height: 300px;
  position: relative;
  overflow: hidden;
}
ul {
  width: 500px;
  height: 300px;
  position: absolute;
}
#tupian ul li {
  list-style: none;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  transition: all 0.8s;
}
.btnBox {
  width: 500px;
  height: 50px;
  position: absolute;
  left: 10px;
  top: 75px;
}
#leftBtn {
  position: absolute;
  left: 0;
  top: 0;
  width: 25px;
  height: 50px;
  background-color: #ccc;
  text-align: center;
  line-height: 50px;
  font-size: 50px;
  font-family: "宋体";
  color: #fff;
  text-decoration: none;
}
#rightBtn {
  position: absolute;
  right: 0;
  top: 0;
  width: 25px;
  height: 50px;
  background-color: #ccc;
  text-align: center;
  line-height: 50px;
  font-family: "宋体";
  font-size: 50px;
  color: #fff;
  text-decoration: none;
}
#tupian ol {
  width: 125px;
  height: 15px;
  position: absolute;
  z-index: 100;
  right: 10px;
  bottom: 5px;
}
#tupian ol li {
  width: 10px;
  height: 10px;
  padding: 5px;
  background-color: #fff;
  text-align: center;
  line-height: 10px;
  list-style: none;
  margin-right: 5px;
  float: left;
}
#tupian ol li.cur {
  background-color: yellow;
}
</style>
</head>
<body>
<div class="hezi" id="hezi">
  <div id="tupian">
    <ul>
      <li style="opacity:1;"><img src="img/1.jpg" alt=""></li>
      <li><img src="img/2.jpg" alt=""></li>
      <li><img src="img/3.jpg" alt=""></li>
      <li><img src="img/4.jpg" alt=""></li>
      <li><img src="img/5.jpg" alt=""></li>
    </ul>
    <ol>
      <li class="cur">1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ol>
  </div>
  <div class="btnBox">
    <a href="javascript:void(0);" id="leftBtn">
    <</a>
    <a href="javascript:;" id="rightBtn">>
    </a>
  </div>
</div>
</body>
<script>
  function Zhuanzhaun(bigEle, smilEle, l, r) {
    this.hezi = document.getElementById(bigEle);
    this.tupian = document.getElementById(smilEle);
    this.ul = tupian.children[0];
    this.ulLiArr = this.ul.children;
    this.ol = tupian.children[1];
    this.olLiArr = this.ol.children;
    this.leftBtn = document.getElementById(l);
    this.rightBtn = document.getElementById(r);
    this.k = 0;
    this.timer = null;
    this.init();
  }
  Zhuanzhaun.prototype = {
    init: function () {
    this.autoplay();
    this.bindRClick();
    this.bindOver();
    this.dianji();
    this.bindEnter();
    this.bindOut();
  },
  autoplay: function () {
    this.timer = setInterval(() => {
      this.autostep();
    }, 2000)
  },
  autostep: function () {
    this.k++;
    if (this.k > 4) {
      this.k = 0;
    }
    for (var i = 0; i < this.ulLiArr.length; i++) {
      this.ulLiArr[i].style.opacity = 0;
      this.olLiArr[i].className = "";
    }
    this.ulLiArr[this.k].style.opacity = 1;
    this.olLiArr[this.k].className = "cur";
  },
  bindEnter: function () {
    var that = this;
    this.hezi.onmouseenter = function () {
      clearInterval(that.timer);
    }
  },
  bindOut: function () {
    var that = this;
    this.hezi.onmouseleave = function () {
      that.autoplay();
    }
  },
  autostep1: function () {
    var _this = this;
    this.k--;
    if (this.k < 0) {
      this.k = 4;
    }
    for (var i = 0; i < _this.ulLiArr.length; i++) {
      _this.ulLiArr[i].style.opacity = 0;
      _this.olLiArr[i].className = "";
    }
    _this.ulLiArr[this.k].style.opacity = 1;
    _this.olLiArr[this.k].className = "cur";
  },
 
    dianji: function () {
      var that = this
      this.leftBtn.onclick = function () {
      that.autostep1();
    }
  },
  bindClick: function () {
    var that = this;
    this.leftBtn.onclick = function () {
    that.k--;
    that.autostep();
    if (that.k < 0) {
      that.k = 4;
    }
    for (var i = 0; i < that.ulLiArr.length; i++) {
      that.ulLiArr[i].style.opacity = 0;
      that.olLiArr[i].className = "";
    }
    that.ulLiArr[that.k].style.opacity = 1;
    that.olLiArr[that.k].className = "cur";
    }
  },
  bindRClick: function () {
    var that = this;
    this.rightBtn.onclick = function () {
      that.autostep();
    }
  },
  bindOver: function () {
    var that = this;
    for (var i = 0; i < this.olLiArr.length; i++) {
    this.olLiArr[i].onmouseover = function () {
      for (var j = 0; j < that.olLiArr.length; j++) {
        that.olLiArr[j].className = "";
        that.ulLiArr[j].style.opacity = 0;
        if (this == that.olLiArr[j]) {
          that.k = j;
          that.ulLiArr[that.k].style.opacity = 1;
          this.className = "cur";
         }
       }
     }
    }
   }
  }
new Zhuanzhaun("hezi","tupian","leftBtn","rightBtn");
</script>
</html>

面向对象原生js轮播图的更多相关文章

  1. javascript原生js轮播图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 原生js轮播图

    //用原生js实现了一个简单的轮播图效果 <!DOCTYPE html><html> <head> <meta charset="UTF-8&quo ...

  3. 手把手原生js轮播图

    在团队带人,突然被人问到轮播图如何实现,进入前端领域有一年多了,但很久没自己写过,一直是用大牛写的插件,今天就写个简单的适合入门者学习的小教程.当然,轮播图的实现原理与设计模式有很多种,我这里讲的是用 ...

  4. 用require.js封装原生js轮播图

    index.html页面: <!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...

  5. 原生js轮播图实现

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

  6. 原生JS面向对象思想封装轮播图组件

    原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...

  7. js原生实现轮播图效果(面向对象编程)

    面向对象编程js原生实现轮播图效果 1.先看效果图 2.需要实现的功能: 自动轮播 点击左右箭头按钮无缝轮播 点击数字按钮切换图片 分析:如何实现无缝轮播? 在一个固定大小的相框里有一个ul标签,其长 ...

  8. 原生JS轮播-各种效果的极简实现(二)面向对象版本的实现和优化

    之前写了一篇原生JS轮播,不过是非面向对象的,并且也没有添加上自动轮播.这里来写一下如何优化和进阶. 这里简单地介绍一下之前的代码,这是html结构 <body> <div clas ...

  9. js___原生js轮播

    原生js轮播 作为一名前端工程师,手写轮播图应该是最基本掌握的技能,以下是我自己原生js写的轮播,欢迎指点批评: 首先css代码 a{text-decoration:none;color:#3DBBF ...

随机推荐

  1. 测开之路一百五十四:ajax+json前后台数据交互

    在实际工作中,前后端数据交互大部分都是用的json格式,后端把数据处理完后,把json传给前端,前端再解析 项目结构 models里面加入把数据转为字典的方法 from datetime import ...

  2. virtualbox压缩虚拟机硬盘文件vhd

    命令如下: VBoxManage modifyhd D:\pc1\pc1.vhd  --compact 当提示以下内容时,将整个虚拟机文件夹拷贝盘符根目录下,将[D:\pc1\pc1.vhd]改为相应 ...

  3. 关于WordPress中字体加载慢的问题解决方案(转)

    2016-04-15 最近发现Wordpress有时候加载的特别慢,于是就想办法找了下原因.之前听网上说是因为wordpress用的是Google的字体库,而且是每次都要加载,导致访问慢的,于是当时装 ...

  4. 【HANA系列】SAP HANA日期函数总结

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA日期函数总结   ...

  5. Scratch少儿编程系列:(三)第一个例子 潜水员

    一. 选择背景 在上一节系统界面的介绍中,选择"6角色"的左边,点击"从背景库中选择背景". 选择主题水下中的"underwater3". ...

  6. 【Qt开发】【Linux开发】Qt程序在嵌入式设备(arm) 上运行,鼠标擦除界面的解决方案

    笔者最近想在arm开发板上,开发一个应用程序,经过网上查询发现qt作为跨平台开发软件很不错,于是便选择了qt开发,笔者的qt版本是4.8.6的.由于arm的主频太低,在arm上进行开发编译,效率会大大 ...

  7. linux/linux学习笔记-vim文本编辑器(mooc)

    vim文本编辑器 vim与vi的区别:( vim=vi +IMproved) VIM是一个Unix以及类unix文本编辑器 特点:功能强大,高度可定制 vim编辑器的三种模式:一般模式.编辑模式和命令 ...

  8. linux sed 命令 实现对文件的增删改替换查 实验

    1. 统一实验文本 # 创建包含下面内容的文件,后面的操作都会使用这个文件 [root@MongoDB ~]# cat person.txt ,mike,CEO ,jack,CTO ,yy,CFO , ...

  9. Java json框架简介

    Gson,FastJson,Jackson,Json-lib性能比较 https://www.xncoding.com/2018/01/09/java/jsons.html 结论:FastJson虽然 ...

  10. 字符串中的replace方法

    String.prototype.replace() 该方法作为字符串中非常常用的方法, 今天我们具体介绍一下它的用法 语法格式 someString.replace(regxp | substr, ...