JavaScript如何实现轮播图?实现思路和代码看这篇
Admin 2021-04-02 群英技术资讯 1004 次浏览
轮播图是现在很多网站平台都会应用的一种展现方式,通过定时或者鼠标点击就能够切换看到多张图片,很多商都会将轮播图作为产品展示,这样的效果是用户就能更容易获取商品信息。那么轮播图是如何实现的呢?下面就以基于JavaScript实现的简单轮播图为例,为大家简单介绍下。
1、先获取元素 盒子 左右按钮
2、鼠标经过 显示/隐藏 左右侧按钮
3、动态生成小圆圈、 添加自定义属性
4、小圆圈点击事件 添加current类名
5、添加动画事件 animate 等于 -索引号*focusWidth
6、克隆第一张图片到最后面
7、添加右侧/左侧按钮点击事件
8、设置定时器 手动调用右侧按钮点击事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="css/index.css" > <script src="js/index.js"></script> </head> <body> <div class="focus"> <a href="javascript:;" class="arrow_r">></a> <a href="javascript:;" class="arrow_l"> <</a> <ul> <li> <a href=""><img src="images/focus.jpg" alt=""></a> </li> <li> <a href=""><img src="images/focus1.jpg" alt=""></a> </li> <li> <a href=""><img src="images/focus2.jpg" alt=""></a> </li> <li> <a href=""><img src="images/focus3.jpg" alt=""></a> </li> </ul> <ol class="circle"> </ol> </div> </body> </html>
css样式部分
* {
padding: 0;
margin: 0;
}
li {
list-style: none;
}
img {
border: 0;
vertical-align: top;
}
a {
text-decoration: none;
}
.focus {
position: relative;
width: 721px;
height: 455px;
margin: 100px auto;
overflow: hidden;
}
.focus ul {
position: absolute;
top: 0;
left: 0;
width: 600%;
}
.focus ul li {
float: left;
}
.arrow_r,
.arrow_l {
display: none;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 24px;
background: rgba(0, 0, 0, .2);
color: #fff;
z-index: 1;
}
.arrow_r {
right: 0;
}
.circle {
position: absolute;
bottom: 10px;
left: 50px;
}
.circle li {
float: left;
width: 8px;
height: 8px;
border: 2px solid rgba(255, 255, 255, .5);
border-radius: 50%;
margin: 0 3px;
cursor: pointer;
}
.current {
background-color: #fff;
}
javascript部分
window.addEventListener('load', function() {
//获取元素
var focus = document.querySelector('.focus');
var arrow_r = document.querySelector('.arrow_r');
var arrow_l = document.querySelector('.arrow_l');
var focusWidth = focus.offsetWidth;
// 鼠标经过focus盒子 显示/隐藏 左右侧按钮 暂停轮播
focus.addEventListener('mouseenter', function() {
arrow_r.style.display = 'block';
arrow_l.style.display = 'block';
clearInterval(timer);
timer = null;
});
focus.addEventListener('mouseleave', function() {
arrow_r.style.display = 'none';
arrow_l.style.display = 'none';
timer = setInterval(function() {
//调用点击事件
arrow_r.click();
}, 2000);
});
//动态生成小圆圈
var ul = focus.querySelector('ul');
var ol = focus.querySelector('.circle');
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 = '';
}
var index = this.getAttribute('index');
num = index;
circle = index;
this.className = 'current';
animate(ul, -index * focusWidth);
})
}
ol.children[0].className = 'current';
//克隆第一张图片放到最后一张
var fis = ul.children[0].cloneNode(true);
ul.appendChild(fis);
//右侧按钮点击事件
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 === ul.children.length - 1) {
circle = 0;
}
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
ol.children[circle].className = 'current';
});
//左侧按钮点击事件
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--;
circle = circle < 0 ? ol.children.length - 1 : circle;
// 调用函数
circleChange();
});
//清除其余小圆圈的current类名
function circleChange() {
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
// 留下当前的小圆圈的current类名
ol.children[circle].className = 'current';
}
//动画函数
function animate(obj, target, callback) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var step = (target - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (obj.offsetLeft == target) {
clearInterval(obj.timer);
callback && callback();
}
obj.style.left = obj.offsetLeft + step + 'px';
}, 15);
}
//自动轮播放轮播图
var timer = setInterval(function() {
//调用点击事件
arrow_r.click();
}, 2000);
});
以上就是关于基于JavaScript实现的轮播图的详细代码,希望对大家的学习有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
实现方法:1、给按钮绑定点击事件并指定一个事件处理函数;2、在事件处理函数中利用“document.getElementById(出现元素对象).style.display="block";”语句设置点击按钮元素显示即可。
在平时工作开发中,大部分开发人员都花费大量的时间在维护其他人员的代码。很难从头开始开发新代码,很多情况下都是以他人成果为基础的,或者新增修改需求,自己写的代码也会被其他开发人员调用,所以写好一份高质量可维护的代码就显得十分重要。
本文主要介绍了TypeScript声明合并的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
最近在做一个后台管理项目,涉及到react相关知识,项目需求需要在表单中带附件提交,怎么实现这个功能呢?下面小编给大家带来了react使用antd的上传组件实现文件表单一起提交功能,一起看看吧
jQuery如何停止函数执行,用什么语句?终止某个函数执行是很常见的需求,jQuery中我们可以使用函数来实现,下面我们具体的来看看是怎样做的,希望对大家学习jQuery的使用有帮助。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008