原生js滑动轮播封装
编程学习 2021-07-04 14:06www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了原生js滑动轮播封装,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了原生js滑动轮播的具体代码,供大家参考,具体内容如下
封装滑动轮播
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>cmm无缝轮播</title>
<style type="text/css">
*{margin: 0 ;padding : 0}
#container{
height: 470px;
width: 590px;
border: 1px solid red;
position: relative;
margin: 50px auto;
}
#box{
position: absolute;
list-style: none;
}
#box li{
float: left;
}
#pages {
width: 100%;
height: 30px;
background: #ccc;
position: absolute;
bottom: 0;
}
#pages i {
width: 20px;
height: 20px;
display: inline-block;
border-radius: 10px;
background: #fff;
margin: 5px;
}
#pages i.current {
background: #f00;
}
#prev, #next {
width: 45px;
height: 100px;
position: absolute;
top: 0;
bottom: 0;
margin: auto;
background: #ccc;
line-height: 100px;
text-align: center;
font-size: 40px;
color: #fff;
}
#next {
right: 0;
}
</style>
</head>
<body>
<div id="container">
<ul id="box">
<li><img src="images/1.jpg"></li>
<li><img src="images/2.jpg"></li>
<li><img src="images/3.jpg"></li>
<li><img src="images/4.jpg"></li>
</ul>
<div id="pages"></div>
<div id="prev"><</div>
<div id="next">></div>
</div>
<script src="js/tools.js"></script>
<script>
var lis = $("li"),
length = lis.length,
liWidth = lis[0].clientWidth,
currentIndex = 0,
nextIndex = 1,
timer = null,
move = null,
circls = null,
durations = 2000;
// 动态设置ul宽度
$("#box").style.width = length * liWidth + "px";
// 动态设置小圆点
var html = "";
for(var i = 0 ;i <length ;i++){
html += "<i></i>"
}
$("#pages").innerHTML= html;
circls = $("i");
circls[0].className = "current";
// 切换动画
move = function(){
// 设置box运动终点值
var _left = -1 * nextIndex * liWidth;
// 开始动画
animate($("#box"),{left:_left},200)
// 修改小圆点样式
circls[currentIndex].className = "";
circls[nextIndex].className = "current";
// 修改索引
currentIndex = nextIndex;
nextIndex++;
if(nextIndex >= length){
nextIndex = 0;
}
}
// 自动动画
timer = setInterval(move, durations)
// container中鼠标移入,移出事件
on($("#container"),"mouseenter",function(){
clearInterval(timer);
})
on($("#container"),"mouseleaver",function(){
timer = setInterval(move, durations);
})
// 点击小圆点,切换至对应的图片
on($("#pages"),"click",function(e){
e = e || event;
var src = e.target || src.Element;
if(src.nodeName === "I"){
var _index = Array.from(circls).indexOf(src);
if(_index === currentIndex){
return
}
nextIndex = _index;
move();
}
})
// 点击翻页进行切换
on($("#prev"),"click",function(){
nextIndex = currentIndex - 1;
if(nextIndex < 0){
nextIndex = length;
}
move();
})
on($("#next"),"click",function(){
move();
})
</script>
</body>
</html>
更多关于轮播图效果的专题,请点击下方链接查看学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- ThinkPad 笔记本如何调节LCD屏幕亮度
- Dreamweaver制作网页打开特效教程
- Win10 Mobile 10586升级后无限重启怎么办 硬重启帮您
- Win8系统提示音频设备有问题有一个或多个音频服
- Xbox One版Win10首个预览版9月份发布
- 如何在textarea文本输入区内实现换行
- Win10 Build 9901系统更新 预览版新版本下载
- McAfee Framework存在远程格式串处理漏洞
- Win10家庭版今日(7月30)正式在中国官方商城开卖
- Win10 Mobile预览版更新完10536.1000后才收到10536.100
- Win10 RS2更新了什么-Win10 RS2最终版本号1704首曝
- Windows7如何查看回收站对应的文件夹有哪些方法
- Win10让Charms栏回归桌面的方法教程
- 取消Windows XP系统开机启动画面的小技巧
- win8系统怎么下载安装USB百兆网卡?
- XP系统下磁盘空间变少了怎么办?XP系统磁盘空间