<!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. 微信小程序的 音频 组件

    audio:音频组件, api 接口为 wx.createInnerAudioContext audio 组件的属性: id:类型 字符串 audio 组件的唯一标识 src:类型 字符串 要播放音频 ...

  2. Xcode10报错 library not found for -lstdc++ 问题解决

    在Xcode9上正常编译的项目,在Xcode10上编译可能会遇到如下错误: library not found for -lstdc++.6.0.9 library not found for -ls ...

  3. ps 和 pstree的用法

    ps 查看某个具体的命令进程: ps -C cmd-name: 如: ps -C httpd // 如果有多个名字相同的进程, 如httpd... 应该是它下面的子进程, 这时会显示第一个进程id. ...

  4. JVM监控工具之jmap、jstat、stack、jps、jstatd、jinfo、jhat、jdb

    1.jdb(The Java Debuger) jdb 用来对core文件和正在运行的Java进程进行实时地调试,里面包含了丰富的命令帮助您进行调试,它的功能和Sun studio里面所带的dbx非常 ...

  5. Python3 字符编码到底是个什么鬼

    首先ASCII码是美国人自己给自己用的,只针对英文及一系列符号,凭想象预留了编码位置,不料有个东方大国文字过于复杂,预留根本不够,所以这个大国重新搞了个编码gb2312.gbk等,结果就是全世界各国都 ...

  6. VS2017使用dotnet命令

    添加引用Microsoft.EntityFrameworkCore.Tools 添加引用后提示未找到命令“dotnet ef”向csprog文件添加如下节点 <ItemGroup> < ...

  7. .net core 学习小结之 Cookie-based认证

    在startup中添加授权相关的管道 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  8. Java第三周课程总结&实验报告一

    第三周课程总结 1.关于面向对象的一些具体内容,明白了类与对象以及Java的封装性和构造方法以及对对象匿名的相关知识. 2.this关键字,它是表示类的成员属性(变量),使用this构造方法时必须放在 ...

  9. MySQL-快速入门(1)基本数据库、表操作语句

    1.创建数据库 create database db_name;show create database db_name\G; //查看数据创建语句show databases; //查看当前创建的数 ...

  10. QT DBUS: Not connected to D-Bus server, 注意source /etc/profile

    运行环境:ARM 运行如下代码: QDBusConnection bus = QDBusConnection::sessionBus(); if(!bus.registerService(" ...