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文件的引入次序 ...
随机推荐
- 【一】工程配置与电机控制part1
前言 学校发的无刷电机: 我们准备的有刷电机: 带霍尔编码器! 电机参数: 名称:驰名电机(直流减速电机) 型号:JGA25-370 电压:12V 转数:1360r/min 做云台,核心是使用PID控 ...
- python之pyc
pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后, 加载的速度有所提高,而且pyc是一种跨平台的字节码,是由Python的虚拟机来执行的, ...
- jdbc model 代码示例
package com.gylhaut.model; import java.util.Date; public class Goddess { @Override public String toS ...
- Flink域名处理
概述 最近做了一个小任务,要使用Flink处理域名数据,在4GB的域名文档中求出每个域名的顶级域名,最后输出每个顶级域名下的前10个子级域名.一个比较简单的入门级Flink应用,代码很容易写,主要用到 ...
- 使用Github Action自动填写疫情通
使用Github Action自动填写疫情通 西电晨午晚检一天三次,通过企业号功能进行填写.实际上,西电企业号大部分功能是以网页模式工作的,通过构造connection发送合适的request,设置计 ...
- dotnet 委托的实现解析(2)开放委托和封闭委托 (Open Delegates vs. Closed Delegates)
前言 这是个人对委托的理解系列第二篇,部分翻译自 Open Delegates vs. Closed Delegates – SLaks.Blog,好像还没人翻译过,加上部分个人理解.希望能对大家理解 ...
- Matplotlib库基础_一
Matplotlib库基础 •pyplot绘制坐标 plt.plot(x,y,format_string,**kwargs) x:x轴数据,列表或数组,可选 y:y轴数据,列表或数组 format_s ...
- kernel热补丁
kernel正在运行的函数,如何实现地址替换的,高并发情况下,如何打热补丁
- C#: .net序列化及反序列化 [XmlElement(“节点名称”)] [XmlAttribute(“节点属性”)] (下篇)
介绍 XML 序列化 .NET Framework 开发员指南 序列化是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间 ...
- SpringBoot:自定义注解实现后台接收Json参数
0.需求 在实际的开发过程中,服务间调用一般使用Json传参的模式,SpringBoot项目无法使用@RequestParam接收Json传参 只有@RequestBody支持Json,但是每次为了一 ...