多行文字上下滚动是一种常见的网页动画效果,通常用于展示新闻、公告或其他重要信息。这种效果可以通过JavaScript结合CSS来实现。
以下是一个简单的JavaScript实现多行文字上下滚动的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-line Text Scrolling</title>
<style>
#scrollingText {
width: 300px;
height: 100px;
overflow: hidden;
border: 1px solid #ccc;
position: relative;
}
.scrolling-content {
position: absolute;
width: 100%;
transition: top 0.5s ease-in-out;
}
</style>
</head>
<body>
<div id="scrollingText">
<div class="scrolling-content">
<p>Line 1: This is the first line of scrolling text.</p>
<p>Line 2: This is the second line of scrolling text.</p>
<p>Line 3: This is the third line of scrolling text.</p>
</div>
</div>
<script>
const scrollingText = document.getElementById('scrollingText');
const content = scrollingText.querySelector('.scrolling-content');
const lines = content.querySelectorAll('p');
let currentIndex = 0;
function scrollText() {
if (currentIndex >= lines.length) {
currentIndex = 0;
content.style.top = '0px';
}
content.style.top = `-${currentIndex * 30}px`;
currentIndex++;
}
setInterval(scrollText, 2000);
</script>
</body>
</html>
setInterval
的时间间隔,优化CSS过渡效果。overflow: hidden
来隐藏溢出部分。通过上述方法,可以实现一个简单且高效的多行文字上下滚动效果。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云