jQuery文字无缝横向滚动是一种网页效果,通过JavaScript和CSS实现文字在水平方向上连续滚动的效果。这种效果常用于新闻标题、广告标语等场景,以吸引用户的注意力。
<!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>
#scroll-container {
width: 100%;
overflow: hidden;
white-space: nowrap;
}
#scroll-text {
display: inline-block;
animation: scroll 20s linear infinite;
}
@keyframes scroll {
0% { transform: translateX(100%); }
100% { transform: translateX(-100%); }
}
</style>
</head>
<body>
<div id="scroll-container">
<div id="scroll-text">
这是一段需要无缝横向滚动的文字内容。这是一段需要无缝横向滚动的文字内容。
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 初始化滚动效果
$('#scroll-text').css('animation-duration', '20s');
});
</script>
</body>
</html>
overflow: hidden
和white-space: nowrap
。setInterval
或requestAnimationFrame
来控制滚动速度,确保每次滚动的距离和时间一致。通过以上方法,可以实现一个流畅且稳定的文字无缝横向滚动效果。