目录
一、案例练习 - 页面轮播图
1.1 首先构建轮播图基本框架
1.2 左右按钮的显示和隐藏
1.3 下方小圆点 - 动态生成和点击事件
1.4 点击按钮滚动图片
1.5 节流阀
1.6 整体代码
1.6.1 HTML
1.6.2 CSS
1.6.3 JS
功能需求:
HTML - 构建轮播图基本框架以及引入对于的css和js文件
Document
CSS - 渲染页面框架
*{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
}
li {
list-style: none;
}
.focus {
position: relative;
width: 721px;
height: 455px;
background-color: skyblue;
}
.focus ul{
position: absolute;
top: 0;
left: 0;
width: 600%;
}
.focus ul li {
float: left;
}
.focus img{
width: 721px;
height: 455px;
}
.arrow-l,
.arrow-r {
position: absolute;
top: 50%;
margin-top: -20px;
width: 24px;
height: 40px;
background: rgba(0, 0, 0, .3);
text-align: center;
line-height: 40px;
color: #fff;
font-family: 'icomoon';
font-size: 18px;
z-index: 2;
}
.arrow-r {
right: 0;
}
.circle {
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -16px;
}
.circle li {
float: left;
width: 8px;
height: 8px;
border: 2px solid rgba(255, 255, 255, 0.5);
margin: 0 3px;
border-radius: 50%;
cursor: pointer;
}
.current {
background-color: orange;
}
左右按钮的显示和隐藏 - 案例分析:
window.addEventListener('load',function(){
// 鼠标事件 - 左右按钮显示和隐藏
var arrow_l = document.querySelector('.arrow-l');
var arrow_r = document.querySelector('.arrow-r');
var foucs = document.querySelector('.focus');
foucs.addEventListener('mouseenter',function(){
arrow_l.style.display = 'block';
arrow_r.style.display = 'block';
});
foucs.addEventListener('mouseleave',function(){
arrow_l.style.display = 'none';
arrow_r.style.display = 'none';
});
});
动态生成小圆点-案例分析:
点击小圆点改变该小圆点背景色 - 案例分析:
点击小圆点滑动图片 - 案例分析:
JS部分
// 动态生成下方小圆点
var ul = focus.querySelector('ul');
var ol = focus.querySelector('ol');
var focusWidth = focus.offsetWidth
for(var i = 0; i < ul.children.length; i++){
var li = document.createElement('li');
// 记录当前小圆点的索引号
li.setAttribute('index', i);
ol.appendChild(li);
// 生成小圆点的同时绑定点击事件
li.addEventListener('click',function(){
for(var i = 0; i < ol.children.length; i++){
ol.children[i].className = '';
}
this.className = 'current';
// 获取点击小圆点的索引号
var index = this.getAttribute('index');
console.log(index);
animate(ul, -index * focusWidth)
});
}
// 默认第一个小圆点设置current类
ol.children[0].className = 'current';
animate.js
function animate(obj, target, callback) {
clearInterval(obj.timer)
// 给不同元素指定了不同定时器
obj.timer = setInterval(function(){
// 步长值写道定时器里
// var step = Math.ceil((target - obj.offsetLeft) / 10);
var step = (target - obj.offsetLeft) / 10;
// 判断步长值的正负
step = step > 0 ? Math.ceil(step) :Math.floor(step);
if(obj.offsetLeft == target){
clearInterval(obj.timer);
// 回调函数写道定时器结束里面
// if(callback){
// acllback();
// }
callback && callback();
} else {
obj.style.left = obj.offsetLeft + step +'px';
}
},15);
}
无缝滚动 - 案例分析:
克隆第一张图片 - 案例分析
小圆点根据按钮变化 - 案例分析:
// 克隆第一张图片,放到ul最后面
var firstImg = ul.children[0].cloneNode(true);
ul.appendChild(firstImg);
// 点击右侧按钮,滚动图片
var num = 0;
var circle = 0;
arrow_r.addEventListener('click',function(){
if (num == ul.children.length -1){
ul.style.left = 0;
num = 0;
}
// 小圆点跟随按钮点击变化
num++;
animate(ul, -num * focusWidth);
circle++;
if(circle == ol.children.length){
circle = 0;
}
// 调用函数
circleChange();
});
// 点击左侧按钮,滚动图片
arrow_l.addEventListener('click',function(){
if (num == 0){
num = ul.children.length - 1;
ul.style.left = -num * focusWidth + 'px';
}
// 小圆点跟随按钮点击变化
num--;
animate(ul, -num * focusWidth);
circle--;
if(circle < 0){
circle = ol.children.length - 1;
}
// 调用函数
circleChange();
});
// 小圆点变化
function circleChange(){
for(var i = 0; i < ol.children.length; i++){
ol.children[i].className = '';
}
ol.children[circle].className = 'current';
}
自动播放功能 - 案例分析:
// 自动播放
var timer = setInterval(function(){
arrow_r.click();
},2000);
focus.addEventListener('mouseenter',function(){
arrow_l.style.display = 'block';
arrow_r.style.display = 'block';
// 停止定时器
clearInterval(timer);
timer = null;
});
focus.addEventListener('mouseleave',function(){
arrow_l.style.display = 'none';
arrow_r.style.display = 'none';
// 开始定时器
timer = setInterval(function(){
arrow_r.click();
},2000);
});
防止轮播图按钮连续点击造成播放过快
节流阀目的:当上一个函数动画内容执行完毕,再去执行下一个函数动画,让事件无法连续触发
核心思路:利用回调函数,添加一个变量来控制,锁住函数和解锁函数
开始设置一个变量 var flag = true;
if(flag){flag = flase; do something} 关闭
利用回调函数,动画执行完毕,flag = true,打开
添加到左右点击案例内
// 点击右侧按钮,滚动图片
var num = 0;
var circle = 0;
var flag = true;
arrow_r.addEventListener('click',function(){
if(flag){
// 关闭节流阀
flag = false;
if (num == ul.children.length -1){
ul.style.left = 0;
num = 0;
}
// 小圆点跟随按钮点击变化
num++;
animate(ul, -num * focusWidth,function(){
flag =true; //打开节流阀
});
circle++;
if(circle == ol.children.length){
circle = 0;
}
// 调用函数
circleChange();
}
});
// 点击左侧按钮,滚动图片
arrow_l.addEventListener('click',function(){
if(flag){
flag = false;
if (num == 0){
num = ul.children.length - 1;
ul.style.left = -num * focusWidth + 'px';
}
// 小圆点跟随按钮点击变化
num--;
animate(ul, -num * focusWidth,function(){
flag =true; //打开节流阀
});
circle--;
if(circle < 0){
circle = ol.children.length - 1;
}
// 调用函数
circleChange();
}
});
Document
*{
margin: 0;
padding: 0;
}
a{
text-decoration: none;
}
li {
list-style: none;
}
.focus {
position: relative;
width: 721px;
height: 455px;
overflow: hidden;
background-color: skyblue;
}
.focus ul{
position: absolute;
top: 0;
left: 0;
width: 600%;
}
.focus ul li {
float: left;
}
.focus img{
width: 721px;
height: 455px;
}
.arrow-l,
.arrow-r {
display: none;
position: absolute;
top: 50%;
margin-top: -20px;
width: 24px;
height: 40px;
background: rgba(0, 0, 0, .3);
text-align: center;
line-height: 40px;
color: #fff;
font-family: 'icomoon';
font-size: 18px;
z-index: 2;
}
.arrow-r {
right: 0;
}
.circle {
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -16px;
}
.circle li {
float: left;
width: 8px;
height: 8px;
border: 2px solid rgba(255, 255, 255, 0.5);
margin: 0 3px;
border-radius: 50%;
cursor: pointer;
}
.current {
background-color: orange
}
window.addEventListener('load',function(){
// 鼠标事件 - 左右按钮显示和隐藏
var arrow_l = document.querySelector('.arrow-l');
var arrow_r = document.querySelector('.arrow-r');
var focus = document.querySelector('.focus');
var focusWidth = focus.offsetWidth;
focus.addEventListener('mouseenter',function(){
arrow_l.style.display = 'block';
arrow_r.style.display = 'block';
// 停止定时器
clearInterval(timer);
timer = null;
});
focus.addEventListener('mouseleave',function(){
arrow_l.style.display = 'none';
arrow_r.style.display = 'none';
// 开始定时器
timer = setInterval(function(){
arrow_r.click();
},2000);
});
// 动态生成下方小圆点
var ul = focus.querySelector('ul');
var ol = focus.querySelector('ol');
for(var i = 0; i < ul.children.length; i++){
var li = document.createElement('li');
// 记录当前小圆点的索引号
li.setAttribute('index', i);
ol.appendChild(li);
// 生成小圆点的同时绑定点击事件
li.addEventListener('click',function(){
for(var i = 0; i < ol.children.length; i++){
ol.children[i].className = '';
}
this.className = 'current';
// 获取点击小圆点的索引号
var index = this.getAttribute('index');
num = index;
circle = index;
console.log(index);
animate(ul, -index * focusWidth)
});
}
// 默认第一个小圆点设置current类
ol.children[0].className = 'current';
// 克隆第一张图片,放到ul最后面
var firstImg = ul.children[0].cloneNode(true);
ul.appendChild(firstImg);
// 点击右侧按钮,滚动图片
var num = 0;
var circle = 0;
var flag = true;
arrow_r.addEventListener('click',function(){
if(flag){
// 关闭节流阀
flag = false;
if (num == ul.children.length -1){
ul.style.left = 0;
num = 0;
}
// 小圆点跟随按钮点击变化
num++;
animate(ul, -num * focusWidth,function(){
flag =true; //打开节流阀
});
circle++;
if(circle == ol.children.length){
circle = 0;
}
// 调用函数
circleChange();
}
});
// 点击左侧按钮,滚动图片
arrow_l.addEventListener('click',function(){
if(flag){
flag = false;
if (num == 0){
num = ul.children.length - 1;
ul.style.left = -num * focusWidth + 'px';
}
// 小圆点跟随按钮点击变化
num--;
animate(ul, -num * focusWidth,function(){
flag =true; //打开节流阀
});
circle--;
if(circle < 0){
circle = ol.children.length - 1;
}
// 调用函数
circleChange();
}
});
// 小圆点变化
function circleChange(){
for(var i = 0; i < ol.children.length; i++){
ol.children[i].className = '';
}
ol.children[circle].className = 'current';
}
// 自动播放
var timer = setInterval(function(){
arrow_r.click();
},2000);
});