轮播图是很多页面必不可少的组件。这里来使用面向对象方式开发一个插件。减去开发的痛楚

首先需要寻找对象:只有一个对象,轮播图!关键点在于找到这个对象所拥有的属性以及方法,通过代码实现出来,这是面向对象最核心的本质;

其属性有:

图片集合;按钮;角标;文本区;当前面的编号;切换速度;

方法有:

上一张()下一张()暂停播放() 自动播放()继续播放()调到指定的图片();这些方法是重中之重;

轮播图的基本结构如下

    <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面向对象的方式开发轮播图插件的更多相关文章

  1. JavaScript+HTML+CSS 无缝滚动轮播图的两种方式

    第一种方式 在轮播图最后添加第一张,一张重复的图片. 点击前一张,到了第一张,将父级oList移动到最后一张(也就是添加的重复的第一张),在进行后续动画. 点击下一张,到了最后一张(也就是添加的重复的 ...

  2. photoSlider-原生js移动开发轮播图、相册滑动插件

    详细内容请点击 在线预览   立即下载 使用方法: 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css& ...

  3. photoSlider-html5原生js移动开发轮播图-相册滑动插件

    简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ...

  4. ReactNative新手学习之路04 组件化开发轮播图swiper支持安卓和IOS

    react native 新手之路04 组件化开发轮播图swiper支持安卓和IOS npm install react-native-carousel --save git 地址Properties ...

  5. 使用JavaScript制作一个好看的轮播图

    目录 使用JavaScript制作出好看的轮播图效果 准备材料 1.图片若干张(包括轮播图和按钮的图片) 2.将按钮的图片应用到按钮上的CSS样式文件 3.实现轮播和点击跳转的JavaScript代码 ...

  6. js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽

    面向对象 对象 : (黑盒子)不了解内部结构, 知道表面的各种操作. 面向对象 : 不了解原理的情况下 会使用功能 . 面向对象是一种通用思想,并非编程中能用,任何事情都能用. 编程语言的面向对象的特 ...

  7. 原生JavaScript(js)手把手教你写轮播图插件(banner)

    ---恢复内容开始--- 1.轮播图插件 1.什么是插件: 为已有的程序增加功能 2.插件的特点(为什么要做成一个插件)与注意事项: 1.通用性,可移植性强 2.兼容性:不会对其他代码产生影响 3.创 ...

  8. 第124天:移动web端-Bootstrap轮播图插件使用

    Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ...

  9. 学习笔记: js插件 —— SuperSlide 2 (轮播图插件,PC用)

    SuperSlide 2  轮播图插件,较老.但还好用. 适用于PC,是绑定到jquery上的方法: $.slide(); 如果在实际中找不到.slide方法,请检查jquery等.js文件的引入次序 ...

随机推荐

  1. Java集合多线程安全

    线程安全与不安全集合 线程不安全集合: ArrayList LinkedList HashMap HashSet TreeMap TreeSet StringBulider 线程安全集合: Vecto ...

  2. npm vue路由配置

    npm vue路由 复习:1.README.md文件:保存如何使用的命令 (1)     npm install:拷项目时可以不拷node_modules文件,运行该命令时,会自动下载node_mod ...

  3. 火狐firebug&firepath插件安装

    火狐浏览器下掉了firebug和firepath插件,用户即使下载了火狐55以下的版本,也无法查找到这两个插件,以下方法可以解决哦 第1步:下载火狐55以内版本安装包,安装时迅速设置禁止自动更新版本, ...

  4. 和风天气WebApi使用教程

    1.首先进入和风天气开发平台,点击右上角的注册进行注册 和风天气开发平台 2.填写注册用的邮箱和密码完成注册,可能还需要手机号,按提示注册完成即可. 3.从和风天气开发平台右上角进入控制台,输入你刚刚 ...

  5. python 发送POST请求

    #博客地址:https://blog.csdn.net/qq_36374896 import urllib.request import urllib.parse url = "http:/ ...

  6. OpenCores注册步骤和成功提交

    一  OpenCores 网站简介,这个是全世界最大的FPGA开源IP核网站.由于最近在学习USB2.0host control IP,所以想去网上下载相关的IP例程学习.通过搜索发现,有两个网站十分 ...

  7. linux命令管道工作原理与使用方法

    一.管道定义 管道是一种两个进程间进行单向通信的机制.因为管道传递数据的单向性,管道又称为半双工管道.管道的这一特点决定了器使用的局限性.管道是Linux支持的最初Unix IPC形式之一,具有以下特 ...

  8. 几种常用的MOS管参数、应用电路及区别:IRF540N、IRF9540N、IRF9540

    1. IRF540N,N沟道,100V,33A,44mΩ@10V 栅极(Gate-G,也叫做门极),源极(Source-S), 漏极(Drain-D) 漏源电压(Vdss) 100V 连续漏极电流(I ...

  9. CF1430F Realistic Gameplay (贪心+DP)

    朴素做法暴力DP,O(nk)过不去... 1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 ...

  10. Lock 与 Synchronized 的区别?

    首先两者都保持了并发场景下的原子性和可见性,区别则是synchronized的释放锁机制是交由其自身控制,且互斥性在某些场景下不符合逻辑,无法进行干预,不可人为中断等.而lock常用的则有Reentr ...