面向对象轮播图
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.carousel1 {
margin: 100px auto;
width: 600px;
height: 400px;
}
.carousel2 {
margin: 100px auto;
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="carousel1"></div>
<div class="carousel2"></div>
<script src="./loop.js"></script>
<script>
//获取两个存放轮播图的容器
var carousel1 = document.querySelector('.carousel1')
var carousel2 = document.querySelector('.carousel2')
//存放数组的代码
var imgArr = ['./images/1.jpeg', './images/2.jpeg', './images/3.jpeg', './images/4.jpeg', './images/5.jpeg', './images/6.jpeg'];
//创建实例对象
var l1 = new Loop(carousel1, imgArr);
var l2 = new Loop(carousel2, imgArr);
// l1.create();
// l1.addStyle();
// l1.addEvent();
</script>
</body>
</html>
var Loop = (function() {
//运动函数
function animate(dom, targetObject, duration, callback) {
// 参数默认值
duration = duration || 1000;
// 1 定义起点
var nowObject = {};
// 获取计算后样式对象
var styleObject = window.getComputedStyle(dom);
// 因为targetObject是对象 所以通过循环得到该对象的每一个属性名
for (var i in targetObject) {
// 获取对应的属性
var prop = styleObject[i];
// 为了确保能够计算 需要转为数字
nowObject[i] = parseInt(prop);
}
// nowObject在循环之后就拥有了targetObjet的所有属性 并且属性值为运动之前的值
// console.log(nowObject);
// 2 定义总距离
var distanceObject = {};
for (var i in targetObject) {
// 总距离 = 终点值 - 初始值
distanceObject[i] = targetObject[i] - nowObject[i];
}
// 3 定义间隔
var interval = 20;
// 4 定义总次数
var allCount = duration / interval;
// 5 定义计数器
var count = 0;
// 开启定时器
var timer = setInterval(function() {
// 计数器累加
count++;
// 改变每一条属性
for (var i in targetObject) {
if (i === "opacity") {
dom.style[i] = (nowObject[i] + count * distanceObject[i] / allCount);
} else {
dom.style[i] = (nowObject[i] + count * distanceObject[i] / allCount) + "px";
}
}
// 判定是否停止
if (count >= allCount) {
// 停止定时器
clearInterval(timer);
// if (callback) {
// callback();
// }
// 短路语法:且运算 有假就假 都真才真
// 当不传递callback时 为undefined 则&&后面的语句不会被执行
// 当传递callback时 为函数 函数为真 就执行了
callback && callback();
// 强行拉到终点去
// for (var i in targetObject) {
// if (i === "opacity") {
// dom.style[i] = targetObject[i];
// } else {
// dom.style[i] = targetObject[i] + "px";
// }
// }
}
}, interval);
};
//创建构造函数
function Loop(container, imgArr) {
//得到存放轮播图的容器
this.container = container;
//得到存放数组的数组
this.imgArr = imgArr;
//创建轮播图片的ul
this.imgUl = document.createElement('ul');
//创建按钮的框
this.btnContainer = document.createElement('div');
//创建左右按钮
this.leftBtn = document.createElement('span');
this.rightBtn = document.createElement('span');
//创建小圆点的ul
this.pointUl = document.createElement('ul');
//创建存放图片li的数组
this.imgLi = [];
//创建存放图片li中img的数组
this.imgDetail = [];
//创建存放小圆点的数组
this.pointLi = [];
//创建存放ul位置的数组
this.count = 0;
//创建标志位锁
this.flag = true;
//创建存放定时器的变量
this.time;
//初始化各个函数
this.init();
}
Loop.prototype = {
constructor: Loop,
init() {
this.create();
this.addStyle();
this.addEvent();
},
//构建架构
create() {
//创造轮播图片的ul
this.createImgUl();
//创建左右按钮
this.createBtn();
//创建小圆点的结构
this.createPointUl();
},
//创建ul里面的li
createImgUl() {
//根据图片数组中有多少个图片生成相应数量的li
this.imgArr.forEach(((item, index) => {
//创建li
var li = document.createElement('li');
//创建img
var img = document.createElement('img');
//赋予地址
img.src = item;
//将img存放到li中
li.appendChild(img);
//将创建的li存放到imgLi数组中
this.imgLi.push(li);
//将创建的图片存放到图片的数组中
this.imgDetail.push(img);
//将创建的li存放到ul中
this.imgUl.appendChild(li);
//将ul存放到容器中
this.container.appendChild(this.imgUl);
}))
// console.log(this.imgLi);
//复制第一个li
var li = this.imgLi[0].cloneNode(true);
//将复制的li也存放到数组中
this.imgLi.push(li);
//将复制的li中的图片也存放到图片数组中
this.imgDetail.push(li.children[0]);
// console.log(li);
//将复制的li放到ul里面
this.imgUl.appendChild(li);
},
createBtn() {
//将左右按钮存放到存放按钮的容器中
this.btnContainer.appendChild(this.leftBtn);
this.btnContainer.appendChild(this.rightBtn);
//将存放按钮的容器存放到容器中
this.container.appendChild(this.btnContainer);
},
createPointUl() {
//根据图片有多少张创建小圆点
this.imgArr.forEach((item) => {
var li = document.createElement('li');
this.pointLi.push(li);
this.pointUl.appendChild(li);
});
// console.log(this.pointLi);
this.container.appendChild(this.pointUl);
},
//添加样式
addStyle() {
//大盒子给相对定位
this.container.style.position = 'relative';
//添加大图的样式
this.imgUlStyle();
//添加按钮的样式
this.BtnStyle();
//添加小圆点的样式
this.pointStyle();
},
imgUlStyle() {
//容器部分超出隐藏
this.container.style.overflow = 'hidden'
//给ul添加样式
imgUl = {
width: this.container.clientWidth * (this.imgLi.length) + 'px',
height: this.container.clientHeight * this.imgLi.length + 'px',
listStyle: 'none',
// display: 'flex',
position: 'absolute',
// overflow: 'hidden'
};
for (var i in imgUl) {
this.imgUl.style[i] = imgUl[i];
};
//给每个li都添加样式
this.imgLi.forEach((item) => {
item.style.height = this.container.clientHeight + 'px';
item.style.width = this.container.clientWidth + 'px';
item.style.float = 'left';
});
// console.log(this.imgDetail);
//给li中的每个图片添加样式
this.imgDetail.forEach((item) => {
item.style.height = this.container.clientHeight + 'px';
item.style.width = this.container.clientWidth + 'px';
})
},
//给按钮添加样式
BtnStyle() {
//按钮的容器添加样式i
var btnContainer = {
position: 'absolute',
top: '40%',
left: 0,
width: '100%',
height: '20%',
// backgroundColor: 'red',
display: 'flex',
justifyContent: 'space-between'
}
for (var i in btnContainer) {
this.btnContainer.style[i] = btnContainer[i];
}
//给两个按钮添加样式
var Btn = {
// display: 'inline-block',
width: '10%',
height: '100%',
// backgroundColor: 'blue',
fontSize: this.container.clientWidth * 0.05 + 'px',
textAlign: 'center',
color: '#fff',
lineHeight: this.container.clientWidth * 0.125 + 'px',
backgroundColor: 'rgba(255,0,0,0.5)',
}
for (var i in Btn) {
this.leftBtn.style[i] = Btn[i];
this.rightBtn.style[i] = Btn[i];
// console.log(i, Btn[i]);
};
this.leftBtn.innerHTML = '<';
this.rightBtn.innerHTML = '>';
},
//给小圆点添加样式
pointStyle() {
//给ul添加样式
ulStyle = {
width: this.container.clientWidth + 'px',
listStyle: 'none',
display: 'flex',
justifyContent: 'space-evenly',
position: 'absolute',
bottom: this.container.clientWidth * 0.05 + 'px',
}
for (var i in ulStyle) {
this.pointUl.style[i] = ulStyle[i];
}
//给li添加样式
liStyle = {
width: this.container.clientWidth * 0.03 + 'px',
height: this.container.clientWidth * 0.03 + 'px',
backgroundColor: 'orange',
borderRadius: '50%',
float: 'left',
cursor: 'pointer'
}
this.pointLi.forEach((item) => {
for (var i in liStyle) {
item.style[i] = liStyle[i];
}
})
},
//添加事件
addEvent() {
//右按钮点击事件
this.rightEvent();
//左按钮点击事件
this.leftEvent();
//小圆点点击事件
this.pointEvent();
//小圆点样式改变事件
this.changeEvent();
//自动轮播事件
this.autoPlay();
//停止轮播事件
this.stopPlay();
//重新播放事件
this.replay();
},
//右滑事件
rightEvent() {
this.rightBtn.onclick = () => {
if (!this.flag) {
return
}
this.flag = false;
// console.log(this.imgLi);
this.count++;
// console.log(this.count);
animate(this.imgUl, { left: -this.container.clientWidth * this.count }, 1000, () => {
if (this.count >= this.imgLi.length - 1) {
this.imgUl.style.left = '0'
this.count = 0;
}
this.flag = true
})
this.changeEvent();
}
},
leftEvent() {
this.leftBtn.addEventListener('click', () => {
if (!this.flag) {
return;
}
this.flag = false;
this.count--;
if (this.count < 0) {
this.count = this.imgLi.length - 1;
this.imgUl.style.left = -this.count * this.container.clientWidth + 'px';
this.count--;
}
animate(this.imgUl, { left: -this.container.clientWidth * this.count }, 1000, () => {
this.flag = true
})
this.changeEvent();
})
},
pointEvent() {
// console.log(this.pointLi);
this.pointLi.forEach((item, index) => {
item.addEventListener('click', () => {
if (!this.flag) {
return;
}
this.flag = false;
this.count = index;
animate(this.imgUl, { left: -this.container.clientWidth * this.count }, 1000, () => {
this.flag = true
})
this.changeEvent();
})
})
},
changeEvent() {
this.pointLi.forEach((item) => {
item.style.backgroundColor = 'orange'
});
if (this.count > this.imgLi.length - 2) {
this.pointLi[0].style.backgroundColor = "red"
} else {
this.pointLi[this.count].style.backgroundColor = "red"
}
},
autoPlay() {
this.time = setInterval(() => {
this.rightBtn.onclick();
}, 1000)
},
stopPlay() {
this.container.addEventListener('mouseenter', () => {
// console.log(1);
clearInterval(this.time);
})
},
replay() {
this.container.addEventListener('mouseleave', () => {
// console.log(1);
// this.time = setInterval(() => {
// this.rightBtn.onclick();
// }, 1000)
this.autoPlay();
})
}
}
return Loop;
})();
(function() {
function aaa() {
}
function Animal() {
}
return Animal
})();