JavaScript实现可控制方向的无缝滚动广告图
作者:异步漫步时间:2026-10-21 22:34:32浏览:352
本文介绍一种JavaScript实现的无缝滚动方案,支持左右方向控制。点击广告位可触发完整展示滑动,并记录当前滚动方向。鼠标离开后自动恢复滚动,适用于需要交互控制的广告轮播场景。
无缝滚动功能概述
此版本实现了左右方向的无缝滚动控制,并支持鼠标交互。当用户点击特定广告区域时,系统会自动执行滑动操作,确保该广告被完整展示。同时,程序会记录当前的滚动方向,即使鼠标离开,滚动仍会按照既定方向继续运行。
效果展示如下:

代码实现细节
具体实现代码见下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type = "text/css">
*{margin: 0; padding: 0;}
li { list-style: none; }
.box {
width: 800px;
height: 450px;
margin: 50px auto;
overflow: hidden;
position: relative;
}
.box span {
width: 40px;
height: 60px;
display: block;
position: absolute;
top: 225px;
margin-top: -20px;
cursor: pointer;
z-index: 1;
}
.box #left {
background: url('http://cdn.attach.qdfuns.com/notes/pics/201701/14/042311cbd7gbjd7sggkd2b.png') no-repeat;
left: 0;
display: none;
}
.box #right {
background: url('http://cdn.attach.qdfuns.com/notes/pics/201701/14/042241w8z4hx4m4pjhyjzs.png') no-repeat;
right: 0;
display: none;
}
#ad {
width: 4000px;
height: 450px;
position: absolute;
}
#ad li {
float: left;
}
</style>
<script type = "text/javascript">
window.onload = function(){
var ad = document.getElementById("ad");
var lef = document.getElementById("left");
var rig = document.getElementById("right");
var timer = null; //管理定时器
var aspect = true;
function animate(obj,speed){
//关闭上一个定时器
clearInterval(obj.timer);
//管理定时器
obj.timer = setInterval(autoAd,30);
function autoAd(){
//判断左走或者右走
if(speed > 0){
aspect = true;
if(obj.offsetLeft >= 0){
obj.style.left = -3200 + 'px';
}
}else {
aspect = false;
if(obj.offsetLeft <= -3200){
obj.style.left = 0;
}
}
//匀速动画: 盒子当前的位置 + 步长
obj.style.left = obj.offsetLeft + speed +"px";
}
}
animate(ad,-5);
//鼠标划入显示控制按钮并关闭
ad.parentNode.onmouseover = function(){
clearInterval(ad.timer);
lef.style.display = "block";
rig.style.display = "block";
};
//鼠标离开隐藏控制按钮并启动定时器
ad.parentNode.onmouseout = function(){
clearInterval(ad.timer);
clearInterval(timer);
lef.style.display = "none";
rig.style.display = "none";
if(aspect){
animate(ad,5);
}else{
animate(ad,-5);
}
};
ad.onclick = function(event){
//关闭自动轮播定时器
clearInterval(ad.timer);
clearInterval(timer);
var event = event || window.event;
if(event.target){
var target = - parseInt(event.target.alt) * 800;
}else{
var target = - parseInt(event.srcElement.alt) * 800;
}
timer = setInterval(function(){
var step = (target - ad.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step):Math.floor(step);
ad.style.left = ad.offsetLeft + step + "px";
if(ad.offsetLeft%800 == 0){
clearInterval(timer);
}
},20)
}
//右移动
lef.onclick = function(){
clearInterval(timer);
animate(ad,5);
};
//左移动
rig.onclick = function(){
clearInterval(timer);
animate(ad,-5);
};
}
</script>
</head>
<body>
<div class="box">
<ul id="ad">
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153lscsitwp7sszb6zs.jpg" alt="0"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042152pfbkbfe8vbtvulfu.jpg" alt="1"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153u3jgn0ds43ndd3dz.jpg" alt="2"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042152juuohne22z60hbsb.jpg" alt="3"></li>
<li><img src="http://cdn.attach.qdfuns.com/notes/pics/201701/14/042153lscsitwp7sszb6zs.jpg" alt="4"></li>
</ul>
<span id="left"></span>
<span id="right"></span>
</div>
</body>
</html>
以上即为该无缝滚动功能的完整实现。希望这段代码能为您的项目开发提供参考,欢迎继续支持本站获取更多技术资源。
作者最新文章
D3.js 比例尺与坐标轴的配置详解
2026-11-05 16:54:46
JavaScript怎么遍历对象的Symbol键和不可枚举属性
2026-11-05 13:13:39
封装一个健壮的JS字符串判空工具函数
2026-11-05 12:16:14
JavaScript合并数组的三种常用方法:concat、扩展运算符与push
2026-11-05 11:22:29
JavaScript立即执行函数(IIFE)是什么及基本语法
2026-11-05 10:51:19
