JavaScript面向对象的方式开发轮播图插件
轮播图是很多页面必不可少的组件。这里来使用面向对象方式开发一个插件。减去开发的痛楚
首先需要寻找对象:只有一个对象,轮播图!关键点在于找到这个对象所拥有的属性以及方法,通过代码实现出来,这是面向对象最核心的本质;
其属性有:
图片集合;按钮;角标;文本区;当前面的编号;切换速度;
方法有:
上一张()下一张()暂停播放() 自动播放()继续播放()调到指定的图片();这些方法是重中之重;
轮播图的基本结构如下
<div class="slider" data-speed="3000" data-currentIndex="1" data-isTxt="true" data-isAuto="true">
<ul class="slider-content">
<li><a href=""><img src="./img/less.jpg" alt="让css可以编程"></a></li>
<li><a href=""><img src="./img/nodejs.jpg" alt="nodejs.jpg"></a></li>
<li><a href=""><img src="./img/react.jpg" alt="react.jpg"></a></li>
<li><a href=""><img src="./img/vue.jpg" alt="vuejs.jpg"></a></li>
</ul>
</div>
轮播图的css如下
    ul,li,ol,p{list-style: none;margin:0;padding:0;}
        .slider{position:relative;width: 800px;height: 400px;margin:50px auto;}
        .slider-content li{position: absolute;left:0;top:0;transition: opacity .5s;}
        img{width: 800px;height: 400px;}
        .btn{position: absolute;width:50px;height: 50px;border-radius: 25px;top:50%;
            transform: translateY(-50%);
            /*margin-top:-25px; */
            background-color: #fff;color:black; }
        .btn-left{left:30px;}
        .btn-right{right:30px;}
        ol{text-align:center;position: absolute;height: 20px;background-color: rgba(255,255,255,.5);bottom:15px;}
        ol li{display: inline-block;margin:3px 5px 0;width: 14px;height: 14px;border-radius: 7px;background-color: #000;font-size: 12px;transition: transform .5s}
        .txt{position: absolute;bottom:0px;text-indent:20px;line-height:40px;height: 40px;width: 100%;background-color:rgba(0,0,0,.5); color: #fff;font-size: 20px; }
        .current{background-color: red;transform: scale(2);}/*当前的角标*/
接下来就是对轮播图的js部分方法和属性的封装
;(function(){
	function extend(defaultSetting,userSetting){
		//创建一个空对象
		var obj={};
		for(var p in defaultSetting){
			if(userSetting.hasOwnProperty(p)){ //(1)
				obj[p] = userSetting[p];
			}
			else{							   //(2)
				obj[p] = defaultSetting[p];
			}
		}
		return obj;
	}
	function Slider(contentId,userSetting={}) {
		var defaultSetting = {
			speed:2000,
			currentIndex:0,
			isAuto:true,
			isTxt:true,
			directorPos:"center"
		}
		this.setting = extend(defaultSetting,userSetting);//综合考虑用户的设置和默认值后得到的。
		this.timer = null;		//定时器
		// 获取整体轮播图的容器
		this.container = document.getElementById(contentId);
		if(this.container == null){
			console.info("没有这个id,你看清楚再来");
			return {};
		}
		// 获取图片集合
		this.lis          = this.container.querySelector(".slider-content").querySelectorAll("li");
		this.len          = this.lis.length;//图片的张数
		this.directors    = []; //角标;
		this.currentIndex = this.setting.currentIndex;			//当前显示的那张图的编号
		this.speed        = this.setting.speed; 				// 每隔2s,切换一张图 2000ms 单位是ms
		//this.dom();
		dom.call(this);				//动态创建dom元素
		//this.init();
		init.call(this);			//this.初始化,绑定一些事件
		this.goto(this.currentIndex);	//显示当前图片
		// if(this.setting.isAuto)
		// {
		// 	this.auto();
		// }
		this.setting.isAuto &&  this.auto(); //开始自动播放
	}
	function dom(){
		var that = this;
		//创建按钮,设置属性,添加事件响应,添加到外部的容器中
		this.btnRight     = document.createElement("span");
		this.btnRight.className = "btn btn-right";
		this.btnRight.innerHTML = "后";
		this.btnRight.onclick =function(){
			that.next();
		}
		this.btnLeft     = document.createElement("span");
		this.btnLeft.className = "btn btn-left";
		this.btnLeft.innerHTML = "前";
		this.btnLeft.onclick =function(){
			that.prev();
		}
		this.container.appendChild(this.btnRight);
		this.container.appendChild(this.btnLeft);
		//创建文字区域,设置属性,添加到外部的容器中
		if( this.setting.isTxt){
			this.txtArea = document.createElement("p");
			this.txtArea.className = "txt";
			this.txtArea.innerHTML = "";
			this.container.appendChild(this.txtArea);
		}
		//先创建角标的容器 ol
		var ol = document.createElement("ol");
		ol.className = "slider-director";
		if(this.setting.directorPos == "center"){
			ol.style.left = "50%";
			ol.style.right="auto";
			ol.style.transform = "translateX(-50%) ";
		}
		else if(this.setting.directorPos == "right"){
			ol.style.left = "auto";
			ol.style.right="20px";
		}
		else{
			ol.style.left = "50%";
			ol.style.right="auto";
			ol.style.transform = "translateX(-50%) ";
		}
		for(var i=0;i<this.len;i++){
			//用循环去创建多个li
			var li = document.createElement("li");
			li.innerHTML = i+1;
			this.directors.push(li);
			//添加到ol中
			ol.appendChild(li);
		}
		//ol添加到轮播图的容器中
		this.container.appendChild(ol);
	}
function init(){
		console.info("init",this)
		var that = this;
		for (var i = 0; i < this.directors.length; i++) {
			this.directors[i].onmouseover = function(abc){
				return function(){
					that.goto(abc);
				}
			}(i)
			// this.directors[i].abc = i;
			// this.directors[i].onmouseover = function(){
			// 	that.goto(this.abc);
			// }
		}
		this.container.onmouseenter = function(){
			that.pause();
		}
		this.container.onmouseleave = function(){
			that.continue();
		}
	}
// 上一张()
	Slider.prototype.prev = function(){
		//更新currentIndex
		this.currentIndex -= 1;
		if(this.currentIndex < 0)
			this.currentIndex = this.len - 1;
		this.goto(this.currentIndex)
	}
	//下一张()
	Slider.prototype.next = function(){
		//更新currentIndex
		this.currentIndex += 1;
		if(this.currentIndex >= this.len)
			this.currentIndex = 0;
		this.goto(this.currentIndex);
	}
	//跳到指定张  n:[0,this.len-1]
	Slider.prototype.goto = function (n) {
		this.currentIndex = n ;	//更新currentIndex
		//console.info(this.currentIndex);
		// 把所有的图片改成display:none
		for(var i = 0;i<this.len;i++){
			this.lis[i].style.opacity = 0;
			//this.lis[i].style.display="none";
		}
		// 把当前currentIndex改成display:block;
		// this.lis[n].style.display = "block";
		this.lis[n].style.opacity = 1;
		for(var i =0; i<this.len;i++){
			this.directors[i].className = ""; //清除所有的className
		}
		this.directors[n].className="current";
		//更新p标签中的内容
		this.txtArea && ( this.txtArea.innerHTML = this.lis[n].querySelector("img").getAttribute("alt") );//当前图片中的alt属性
	}
	//自动播放()
	Slider.prototype.auto = function () {
		var that = this;
		clearInterval(this.timer);
		this.timer = setInterval(function(){
			that.next();
		},this.speed);
	}
	//暂停播放()
	Slider.prototype.pause = function(){
		clearInterval(this.timer)
	}
	//继续方法()
	Slider.prototype.continue = function(){
		this.auto();
	}
	window.Slider = Slider;
})();
用户自定义配置的js
function extend(defaultSetting,userSetting){
	//创建一个空对象
	var obj={};
	for(var p in defaultSetting){
		if(userSetting.hasOwnProperty(p)){ //(1)
			obj[p] = userSetting[p];
		}
		else{							   //(2)
			obj[p] = defaultSetting[p];
		}
	}
	return obj;
}
JavaScript面向对象的方式开发轮播图插件的更多相关文章
- JavaScript+HTML+CSS 无缝滚动轮播图的两种方式
		第一种方式 在轮播图最后添加第一张,一张重复的图片. 点击前一张,到了第一张,将父级oList移动到最后一张(也就是添加的重复的第一张),在进行后续动画. 点击下一张,到了最后一张(也就是添加的重复的 ... 
- photoSlider-原生js移动开发轮播图、相册滑动插件
		详细内容请点击 在线预览 立即下载 使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css& ... 
- photoSlider-html5原生js移动开发轮播图-相册滑动插件
		简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ... 
- ReactNative新手学习之路04 组件化开发轮播图swiper支持安卓和IOS
		react native 新手之路04 组件化开发轮播图swiper支持安卓和IOS npm install react-native-carousel --save git 地址Properties ... 
- 使用JavaScript制作一个好看的轮播图
		目录 使用JavaScript制作出好看的轮播图效果 准备材料 1.图片若干张(包括轮播图和按钮的图片) 2.将按钮的图片应用到按钮上的CSS样式文件 3.实现轮播和点击跳转的JavaScript代码 ... 
- js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽
		面向对象 对象 : (黑盒子)不了解内部结构, 知道表面的各种操作. 面向对象 : 不了解原理的情况下 会使用功能 . 面向对象是一种通用思想,并非编程中能用,任何事情都能用. 编程语言的面向对象的特 ... 
- 原生JavaScript(js)手把手教你写轮播图插件(banner)
		---恢复内容开始--- 1.轮播图插件 1.什么是插件: 为已有的程序增加功能 2.插件的特点(为什么要做成一个插件)与注意事项: 1.通用性,可移植性强 2.兼容性:不会对其他代码产生影响 3.创 ... 
- 第124天:移动web端-Bootstrap轮播图插件使用
		Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ... 
- 学习笔记:    js插件 ——  SuperSlide 2 (轮播图插件,PC用)
		SuperSlide 2 轮播图插件,较老.但还好用. 适用于PC,是绑定到jquery上的方法: $.slide(); 如果在实际中找不到.slide方法,请检查jquery等.js文件的引入次序 ... 
随机推荐
- linux echo用法和实例
			echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下 ... 
- 表格的td行利用css显示...
			默认超过指定长度以...显示, 鼠标放到文本上显示全 代码如下 .fh{ max-width:220px; word-wrap:break-word; text-overflow:ellipsis ... 
- tp5 git 常见命令
			git clone git add . # 跟踪所有改动过的文件 git commit -m "commit message" # 提交所有更新过的文件 git checkout ... 
- CodeReview规范
			目标和原则 提高代码质量,及早发现潜在缺陷,降低修改/弥补缺陷的成本 促进团队内部知识共享,提高团队整体水平 评审过程对于评审人员来说,也是一种思路重构的过程,帮助更多的人理解系统 是一个传递知识的手 ... 
- MacBook苹果电脑绕过BootCamp安装Win7双系统
			上周老婆的一个朋友的笔记本电脑说电脑太慢说帮忙清理一下电脑,我一看是MACBOOKPRO 的笔记装的双系统,之前一直接触的都是WINDOWS居多.想着干脆装个WIN10吧,谁知道一下就跳坑里了,一分区 ... 
- ArcMap操作随记(14)
			1.ArcMap中模型转为Python脚本 [模型]→右键→[编辑]→[模型]→[导出]→[至Python脚本] 2.一般来说,植被指数NDVI,-1<=NDVI<=1. 3.用lands ... 
- NET经典书籍必读
			C#与.NET框架,入门 + 进阶 + 精通,外加并发编程实例,10本C#图书,一本都不能少. 1.<Learning hard C#学习笔记> 作者:李志 书号:978-7-115-3 ... 
- 阿里云开源镜像站支持IPv6访问
			阿里云开源镜像站在国内企业镜像站中率先支持IPv6访问! 点击立即试用https://developer.aliyun.com/mirror/ 同时基于阿里云OpenSearch的搜索能力,开源镜像站 ... 
- 如何在vscode中编写.net core 项目(vscode)
			1.下载拓展 .NET Core Extension Pack (作者:保哥) 这个里面将需要的插件都打包了小白一键下载就好了 2.下载扩展 vscode-solution-explorer ... 
- JavaScript01 js基础语法,数据类型
			JavaScript的概述: 1.组成 三部分组成 ecmaScript 基础语法 (es5) dom document object model 文档对象模型 (操作html文档内容) bom bo ... 
