jQuery 横向跑马灯是一种网页效果,通过 jQuery 库实现图片或内容在水平方向上循环滚动显示。这种效果常用于新闻、广告、图片展示等场景。
animation
和 transform
属性实现。animate
方法和定时器实现。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 横向跑马灯</title>
<style>
#marquee {
width: 100%;
overflow: hidden;
position: relative;
}
#marquee ul {
list-style: none;
padding: 0;
margin: 0;
white-space: nowrap;
}
#marquee ul li {
display: inline-block;
padding: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="marquee">
<ul>
<li>新闻1</li>
<li>新闻2</li>
<li>新闻3</li>
<li>新闻4</li>
</ul>
</div>
<script>
$(document).ready(function() {
function marquee() {
$('#marquee ul').animate({
marginLeft: '-100%'
}, 5000, function() {
$(this).css({ marginLeft: '0' }).find('li:first').appendTo(this);
});
}
setInterval(marquee, 5000);
});
</script>
</body>
</html>
marginLeft
或其他定位属性设置错误。animate
方法中的速度参数设置不当。animate
方法中的速度参数,使其符合预期效果。通过以上方法,可以有效地实现和优化 jQuery 横向跑马灯效果。
领取专属 10元无门槛券
手把手带您无忧上云