JavaScript实现简单图片轮播效果
作者:lettle_redhat 时间:2024-04-22 13:02:05
本文实例为大家分享了js实现简单图片轮播的具体代码,最终实现效果图
代码块
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0
}
ul {
list-style: none
}
img {
vertical-align: top
}
.box {
width: 490px;
height: 170px;
margin: 100px auto;
padding: 5px;
border: 1px solid #ccc;
}
.inner {
width: 490px;
height: 170px;
background-color: pink;
overflow: hidden;
position: relative;
}
.inner ul {
width: 1000%;
position: absolute;
top: 0;
left: 0;
}
.inner li {
float: left;
}
.square {
position: absolute;
right: 10px;
bottom: 10px;
}
.square span {
display: inline-block;
width: 16px;
height: 16px;
background-color: #fff;
text-align: center;
line-height: 16px;
cursor: pointer;
}
.square span.current {
background-color: orangered;
color: #fff;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="inner">
<ul>
<li><a href="#"><img src="images/01.jpg" /></a></li>
<li><a href="#"><img src="images/02.jpg" /></a></li>
<li><a href="#"><img src="images/03.jpg" /></a></li>
<li><a href="#"><img src="images/04.jpg" /></a></li>
<li><a href="#"><img src="images/05.jpg" /></a></li>
</ul>
<div class="square">
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</div>
</div>
<script type="text/javascript">
//鼠标经过按钮 按钮排他
var box = document.getElementById("box");
var inner = box.children[0]; //获取box下的第一个元素,也就是inner
var ul = inner.children[0]; //获取inner下的ul
var squareList = inner.children[1]; //获取inner下的第二个元素
var squares = squareList.children; //获取所有的按钮
var imgWidth = inner.offsetWidth;
// alert(imgWidth);
//给每个按钮注册鼠标经过事件
for(var i=0; i<squares.length; i++){
squares[i].index = i; //把索引保存在自定义属性中
squares[i].onmouseover = function(){ //鼠标经过事件
//排他 干掉所有人
for(var j=0; j<squares.length; j++){
squares[j].className = "";
}
//留下我自己
this.className = "current";
//以动画的方式把ul移动到指定的位置
//目标 和当前按钮索引有关,和图片宽度有关 而且是负数
var target = -this.index * imgWidth; //获取到索引
animate(ul,target);
}
}
function animate(obj, target) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var step = 20;
var step = obj.offsetLeft < target ? step : -step;
if (Math.abs(obj.offsetLeft - target) > Math.abs(step)) {
obj.style.left = obj.offsetLeft + step + "px";
} else {
obj.style.left = target + "px";
clearInterval(obj.timer);
}
}, 15)
}
</script>
</body>
</html>
来源:http://blog.csdn.net/makenzie/article/details/77451563
标签:js,图片轮播
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python列表(List)知识点总结
2023-11-04 00:01:35
深入讲解SQL中的字符串拼接
2024-01-23 17:52:00
![](https://img.aspxhome.com/file/2023/9/102059_0s.png)
python使用Pycharm创建一个Django项目
2023-11-01 22:33:13
![](https://img.aspxhome.com/file/2023/4/110054_0s.png)
Pytorch dataloader在加载最后一个batch时卡死的解决
2022-09-15 06:50:34
如何在ADSI中查询用户属性?
2010-06-17 12:53:00
Python常见数字运算操作实例小结
2022-01-02 21:04:16
深入理解python中sort()与sorted()的区别
2021-08-17 11:46:49
pandas实现导出数据的四种方式
2023-01-03 14:57:57
重命名批处理python脚本
2021-04-27 22:56:38
python 判断字符串中是否含有汉字或非汉字的实例
2022-07-01 11:25:59
在Golang中使用C语言代码实例
2024-05-25 15:15:46
navicat无法远程连接mysql的解决方法
2024-01-21 13:13:51
百度“有啊”首页首次曝光 以绿色调为主
2008-10-20 12:52:00
![](https://img.aspxhome.com/file/UploadPic/200810/20/20081020125720739s.jpg)
python实现二维数组的对角线遍历
2023-05-15 06:08:01
![](https://img.aspxhome.com/file/2023/8/115218_0s.gif)
Go并发同步Mutex典型易错使用场景
2024-05-02 16:23:54
![](https://img.aspxhome.com/file/2023/1/130721_0s.jpg)
对Python信号处理模块signal详解
2021-08-20 04:06:11
Python+OpenCV人脸检测原理及示例详解
2021-07-31 19:31:51
![](https://img.aspxhome.com/file/2023/0/133480_0s.jpg)
Python利用AutoGrad实现自动计算函数斜率和梯度
2023-09-27 22:47:59
![](https://img.aspxhome.com/file/2023/2/118512_0s.png)
在SUSE10环境下安装和配置MySQL数据库
2008-12-17 15:03:00
用户体验之网页板块设计
2011-05-14 16:41:00