javascript实现图片切换、自动走、鼠标移入会停止,移出继续走
2024-10-09 21:33:39
要实现以上效果并不难,把功能分开一步一步来实现就变得简单了,录制动态图不流畅,看代码意会吧!
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
* {margin:0; padding: 0}
button {
width: 50px;
}
p {
text-align: center;
}
.active {
background-color: yellow;
}
#wrap {
width:210px;
overflow: hidden;
margin:0 auto;
}
#inner {
width:9999px;
overflow: hidden;
position: relative;
left:0;
transition: left 0.6s;
}
#inner a {
float: left;
}
#inner img {
display: block;
width:210px;
}
</style> </head>
<body> <div id="wrap">
<div id="inner">
<a href="###"><img src="img/1.jpg"></a>
<a href="###"><img src="img/2.jpg"></a>
<a href="###"><img src="img/3.jpg"></a>
<a href="###"><img src="img/4.jpg"></a>
<a href="###"><img src="img/5.jpg"></a>
<a href="###"><img src="img/6.jpg"></a>
<a href="###"><img src="img/7.jpg"></a>
<a href="###"><img src="img/8.jpg"></a>
<a href="###"><img src="img/9.jpg"></a>
<a href="###"><img src="img/10.jpg"></a>
<a href="###"><img src="img/11.jpg"></a>
<a href="###"><img src="img/12.jpg"></a>
<a href="###"><img src="img/13.jpg"></a>
<a href="###"><img src="img/14.jpg"></a>
</div>
</div>
<p>
<button class="active">1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>7</button>
<button>8</button>
<button>9</button>
<button>10</button>
<button>11</button>
<button>12</button>
<button>13</button>
<button>14</button>
</p>
<script type="text/javascript">
//1.找节点
var buttonList = document.getElementsByTagName("button");
var inner = document.getElementById("inner");
var perWidth = inner.children[0].offsetWidth; function tab() {
inner.style.left = - perWidth * index + "px";
for(var j = 0; j < buttonList.length; j++) {
buttonList[j].className = "";
}
buttonList[index].className = "active";
}
for(var i = 0; i < buttonList.length; i++) {
buttonList[i].index = i;
buttonList[i].onclick = function() {
index = this.index;
tab();
}
}
var index = 0;
function next() {
index ++;
if(index > buttonList.length - 1) {
index = 0;
}
tab();
}
var timer = setInterval(next,1000); inner.onmouseover = function() {
// alert("鼠标移入");
clearInterval(timer);
}
inner.onmouseout = function() {
timer = setInterval(next,1000);
}
</script>
</body>
</html>
以后还会继续添加些效果补充完整的....
最新文章
- TypeScript 素描-变量声明
- 一个java源文件中为什么只能有一个public类。
- java和c#使用hessian通信
- 正则表达式入门(六)匹配unicode和其他字符
- Android ORM应用开发框架KJFrameForAndroid使用详解
- C# 表达式学习积累
- Photoshop 使用可选颜色
- Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
- 【转】 Spinner的常用技巧
- C#图像处理(1):在图片上加文字和改变文字的方向
- uvc摄像头代码解析5
- ecshop中getAll ,getOne ,getRow的区别
- MEF初体验之二:定义组合部件和契约
- WebApi 跨域问题解决方案:CORS
- 201621123040《Java程序设计》第5周学习总结
- spring boot Websocket(使用笔记)
- [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II
- Swagger Annotation 详解(建议收藏)
- CentOS_mini下安装docker 之 yum mount
- python 对时间操作