javascript实现图片左右无缝滚动效果(附代码)
导读:
本文实例讲述了javascript实现的左右无缝滚动效果。即几张图片一起进行无缝滚动,这是一个常用的 js 效果。希望有用的朋友收藏起来。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>
无缝滚动——左右
</title>
<link rel="stylesheet" type="text/css" href="../css/base.css" media="all"
/>
<style type="text/css">
#scroll{width:698px;height:108px;margin:50px auto 0;position:relative;overflow:hidden;}
.btn_left{display:block;width:68px;height:68px;background:url(images/btn.jpg)
no-repeat -70px -69px;position:absolute;top:20px;left:1px;z-index:1;} .btn_left:hover{background:url(images/btn.jpg)
no-repeat -70px 0;} .btn_right{display:block;width:68px;height:68px;background:url(images/btn.jpg)
no-repeat 1px -69px;position:absolute;top:20px;right:0;z-index:1;} .btn_right:hover{background:url(images/btn.jpg)
no-repeat 1px 0;} #scroll .content{width:546px;height:108px;position:relative;overflow:hidden;margin:0
auto;} #scroll ul{position:absolute;} #scroll li{float:left;width:182px;height:108px;text-align:center;}
#scroll li a:hover{position:relative;top:2px;}
</style>
</head>
<body>
<div id="scroll">
<a href="javascript:;" class="btn_left">
</a>
<a href="javascript:;" class="btn_right">
</a>
<div class="content">
<ul>
<li>
<a href="#">
<img src="images/1.jpg" width="178" height="108" alt="" />
</a>
</li>
<li>
<a href="#">
<img src="images/2.jpg" width="178" height="108" alt="" />
</a>
</li>
<li>
<a href="#">
<img src="images/3.jpg" width="178" height="108" alt="" />
</a>
</li>
<li>
<a href="#">
<img src="images/4.jpg" width="178" height="108" alt="" />
</a>
</li>
</ul>
</div>
</div>
</body>
</html>
<script type="text/javascript">
window.onload = function() {
var oDiv = document.getElementById('scroll');
var oUl = oDiv.getElementsByTagName('ul')[0];
var aLi = oDiv.getElementsByTagName('li');
var aBtn = oDiv.getElementsByTagName('a');
var speed = -1;
var timer = null;
oUl.innerHTML += oUl.innerHTML;
oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px';
timer = setInterval(function() {
oUl.style.left = oUl.offsetLeft + speed + 'px';
if (oUl.offsetLeft < -oUl.offsetWidth / 2) {
oUl.style.left = '0';
} else if (oUl.offsetLeft > 0) {
oUl.style.left = -oUl.offsetWidth / 2 + 'px';
}
},
30);
aBtn[0].onclick = function() {
speed = -1;
};
aBtn[1].onclick = function() {
speed = 1;
};
oUl.onmouseover = function() {
clearInterval(timer);
};
oUl.onmouseout = function() {
timer = setInterval(function() {
oUl.style.left = oUl.offsetLeft + speed + 'px';
if (oUl.offsetLeft < -oUl.offsetWidth / 2) {
oUl.style.left = '0';
} else if (oUl.offsetLeft > 0) {
oUl.style.left = -oUl.offsetWidth / 2 + 'px';
}
},
30);
};
};
</script>
注释:如果想要改变移动速度,只需要改变 speed 的值。
相关推荐
记录一个合成图片插件html2canvas.js(JS前端开发)
本文记录一个前端开发中端常用的合成图片插件(html2canvas.js),在H5开发中,我们经常会遇上动态生成海报的需求,而在Web前端中,生成图片非Canvas莫属。html2canvas.js就…
2022-05-23 20:29:09 | 分类:JS | 作者:myerob | 阅读:889 | 标签:合成 图片 插件 html2canvas.js JS 前端 开发 | 收藏
javascript,JS怎么对url进行编码转换
项目中url遇到传参数传数组对象解析失败问题,采用转码方式解决,但并不清楚实际原理,研究了一下JS转码解码。Javascript语言用于编码的函数,一共有三个。escape()encodeURI()e…
2022-10-29 19:12:36 | 分类:JS | 作者:myerob | 阅读:332 | 标签:escape encodeURI encodeURIComponent | 收藏
jQuery 滚动条插件nicescroll 使用方法、常见配置和事件回调使用说明
nicescroll是一款基于jQuery的滚动条插件,使用简单,功能强大,较mCustomScrollbar而言:资源文件少,只要一个jquery.nicescroll.min.js即可,无须css…
2022-12-05 22:16:17 | 分类:JS | 作者:myerob | 阅读:218 | 标签:jQuery nicescroll | 收藏
- 阅读榜
- 1. NTC热敏电阻B值3950(温度系数对照表)
- 2. U.FL座 IPEX/IPX接头1代,2代,3代,4代,5代区分对照图
- 3. 概率统计学中95%置信区间的系数1.96怎么得到的?
- 4. 个人所得税申报系统(扣缴端)如何删除已离职人员信息
- 5. BCD码的定义以及其与十进制数据转换方法(附验证工具)
- 6. GB2312汉字编码字符集对照表
- 7. STM32 编译 Error: L6411E、L6310E 解决方法
- 8. 印花税低于1元需要交吗?是不是不用交
- 9. 离职员工申报个税删不掉 显示存在收入明细,若该人员已离职或退休,请将人员状态改为非正常。可改为非正常后还是不行
- 10. php页面最大执行时间 set_time_limit函数不起作用