jQuery文字无缝滚动是一种网页特效,通过使用jQuery库来实现文字或内容的连续滚动效果。这种效果常用于新闻、公告等需要展示大量信息的场景,以节省页面空间并提高用户体验。
以下是一个简单的jQuery文字垂直滚动的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文字无缝滚动</title>
<style>
#scrollContainer {
width: 300px;
height: 100px;
overflow: hidden;
border: 1px solid #ccc;
position: relative;
}
#scrollContent {
position: absolute;
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div id="scrollContainer">
<div id="scrollContent">
这是一段需要滚动的文字示例。这是一段需要滚动的文字示例。
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var $scrollContent = $('#scrollContent');
var contentHeight = $scrollContent.height();
var containerHeight = $('#scrollContainer').height();
$scrollContent.css('top', -contentHeight);
function scrollText() {
$scrollContent.animate({
top: containerHeight
}, 5000, 'linear', function() {
$scrollContent.css('top', -contentHeight);
scrollText();
});
}
scrollText();
});
</script>
</body>
</html>
animate
方法中的时间参数。#scrollContent
的高度是内容的实际高度,并且在动画结束后正确重置位置。通过以上方法,可以有效实现并优化jQuery文字无缝滚动效果。
领取专属 10元无门槛券
手把手带您无忧上云