<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{margin: 0;padding: 0}
#wrap{width: 600px; margin:20px auto; font-size: 14px;border: 1px solid #ccc;}
#tabs{width: 100%; height: 40px; line-height: 40px;border-bottom: 1px solid #ccc;}
#tabs a{display: block;float: left; padding: 0 20px; text-decoration: none;}
#tabs a.tabactive{background: brown; color: #fff;}
#content{width: 100%; height: 400px;}
#content p{height: 400px; display: none}
#content p.conactive{display: block;}
</style>
</head>
<body>
<div id="wrap">
<div id="tabs">
<a class="tabactive" href="javascript:;">新闻</a>
<a href="javascript:;">国内</a>
<a href="javascript:;">国外</a>
</div>
<div id="content">
<p class="conactive">新闻</p>
<p>国内</p>
<p>国外</p>
</div>
</div>
<script>
window.onload = function () {
//保存this
let that = null;
//声明构造函数
class Tabs {
//构造器
constructor(id){
this.wrap = document.getElementById(id);
this.abtns = this.wrap.getElementsByTagName('a');
this.pcon = this.wrap.getElementsByTagName('p');
this.num = 0;
this.timer = null;
this.init()
}
//初始化
init(){
//保存this对象
that = this;
//执行自动播放功能
this.autoplay();
//执行点击切换功能
this.tab();
//鼠标移入时取消定时器
this.wrap.onmouseover = function(){
clearInterval(that.timer)
}
//鼠标离开时,开启自动轮播功能
this.wrap.onmouseleave = function(){
that.autoplay();
}
}
//点击切换
tab(){
for(let i=0;i<this.abtns.length;i++){
this.abtns[i].index = i;
this.abtns[i].onclick = function(){
//注意:这个函数里的this指向了abtn元素,想要使用实例中的this就要用之前保存的that来代替
clearInterval(that.timer)
//把点击元素的index赋值给实例上的num,以保证下次自动轮播时起始点正确
that.num = this.index
that.qiehuan()
}
}
}
//自动播放
autoplay(){
this.timer = setInterval(function(){
//注意:这个函数里的this指向了window,想要使用实例中的this就要用之前保存的that来代替
that.num++;
that.num %= that.abtns.length;
that.qiehuan()
},2000)
}
//切换效果
qiehuan(){
for(let i=0;i<that.abtns.length;i++){
that.abtns[i].className = ""
that.pcon[i].className = ""
}
that.abtns[that.num].className = "tabactive"
that.pcon[that.num].className = "conactive"
}
}
//生成实例
new Tabs('wrap')
}
</script>
</body>
</html>

es6 面向对象选项卡(自动轮播功能)的更多相关文章

  1. 原生js面向对象编程-选项卡(自动轮播)

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

  2. Javascript专题(三)c.各种轮播--上下滚动轮播(面向对象版本)

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

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

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

  4. jQuery实现选项联动轮播

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

  5. jQuery仿淘宝图片无缝滚动轮播

    自己前天,也就是1月8日的时候早上自己写了一个图片滚动轮播(基于jQuery). 其实几个月以前就有朋友问过我怎么做出和淘宝上面一样的滚动轮播,一直到现在也没有真正的写好,这次写得差不多了. 但是还有 ...

  6. 原生JS编写兼容IE6,7,8浏览器无缝自动轮播(带按钮切换)

    项目要求页面兼容IE6,7,8等浏览器,我们可能会遇到这个轮播效果,轮播板块要求:无限循环.自动轮播和手动切换功能,每一次滚动一小格,网上有很多这类插件,例如:swiper等! 但是很多都是不兼容IE ...

  7. js实现轮播功能

    先上图,效果大概就是这样子: 实现的功能: 1.鼠标经过第几个正方形,就要展示第几张图片,并且正方形的颜色也发生变化 2.图片自动轮播,(这需要一个定时器) 3.鼠标经过图片,图片停止自动播放(这需要 ...

  8. JS实现自动轮播图效果(js案例)

    现在很多网站都有轮播图,这篇文章主要为大家详细介绍了js实现轮播图的完整代码及原理,需要的小伙伴可以参考一下. 1.轮播图主要功能: 1.  图片自动轮播(主图切换同时下面导航图片也会跟着变化) 2. ...

  9. ES6面向对象 动态添加标签页

    HTML <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml&quo ...

随机推荐

  1. 常用的python内建函数

    raw_input() 函数说明 函数签名:raw_input([prompt]) 使用形式如下: raw_input([prompt]) -> string 如果提供了参数prompt,就会在 ...

  2. The 'decorators' plugin requires a 'decoratorsBeforeExport' option, ...(npm start报错)

    问题描述: 在npm start启动react项目的时候,出现了如下报错: The 'decorators' plugin requires a 'decoratorsBeforeExport' op ...

  3. uni-app学习记录06-Vuex简单使用

    import Vue from 'vue' // 这里引入vuex import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Stor ...

  4. CF1B.Spreadsheets(电子表格) 题解 模拟

    作者:zifeiy 标签:模拟 题目出处:Spreadsheets 题目描述 在流行的电子表格系统中(例如,在Excel中),使用如下计算方式来对列号进行计算. 第1列对应A,第2列对应B,--,第2 ...

  5. PTA 6-2 多项式求值

    PTA 6-2 多项式求值 本题要求实现一个函数 本题要求实现一个函数,计算阶数为n,系数为a[0] ... a[n]的多项式f(x)=∑i=0n(a[i]×xi)" role=" ...

  6. 2018-8-10-WPF-好看的矢量图标

    title author date CreateTime categories WPF 好看的矢量图标 lindexi 2018-08-10 19:16:53 +0800 2018-5-16 11:4 ...

  7. 如何查看 Python 全部内置变量和内置函数?

    https://jingyan.baidu.com/article/7082dc1c071649e40a89bdb8.html Python 解释器内置了一些常量和函数,叫做内置常量(Built-in ...

  8. js基础——变量、作用域、内存

    1.new关键字创建的是引用类型: eg. var box = new Object();      box.name = "Linda";//引用类型添加属性没问题     al ...

  9. P1050 全排列

    题目描述 给定一个正整数n, 按照递增顺序打印数字1到n的所有排列. 输入格式 一个整数n(1<=n<=7). 输出格式 按照递增的顺序输出n的所有排列, 详见样例.请注意,每行末尾不能有 ...

  10. 【u201】矩形覆盖

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有N个矩形,矩形的底边边长为1,且均在X轴上,高度给出,第i个矩形的高为h[i],例如h = [3, ...