效果图
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标题文字轮播</title>
<style>
*{
padding: 0px;
margin: 0px;
}
.display{
height: 30px;
position: relative;
overflow: hidden;
}
p {
line-height: 30px;
position: absolute;
top: 30px;
}
.show{
top: 0px;transition: 1s top ease-out;}
.up{
top: -30px;transition: 1s top ease-out;}
p:nth-child(1){
color: fuchsia;}
p:nth-child(2){
color: lightgreen;}
p:nth-child(3){
color: blueviolet;}
p:nth-child(4){
color: limegreen;}
</style>
</head>
<body>
<div class="display">
<p class="show">这是一个标题文字轮播效果</p>
<p>使用到简单的javascript</p>
<p>你觉得不错的话可以可以看看我的主页</p>
<p class="end">最后别忘了点赞哦</p>
</div>
<script>
setInterval(function () {
let show = document.querySelector('p.show');
let next = show.nextElementSibling || document.querySelector('p:first-child');
let up = document.querySelector('p.up');
show.classList.remove('show');
show.classList.add('up');
next.classList.add('show');
if(up) {
up.classList.remove('up');
}
},2500)
</script>
</body>
</html>