admin管理员组文章数量:1292317
The concept is simple, when the text gets too long for the container, it would infinitely animate (scroll) from left to right, and repeat this processes after it reaches the end.
How should I go about making this?
<!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" />
</head>
<body>
<div class="auto-scroll">
<p>
This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi modi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem
nostrum sint modi velit, ut distinctio. And return to beginning.
</p>
</div>
<style>
.auto-scroll {
background: black;
padding: 18px;
}
.auto-scroll p {
font-family: monospace;
font-size: 18px;
color: white;
margin: 0;
white-space: nowrap;
overflow: auto;
}
.auto-scroll ::-webkit-scrollbar {
height: 10px; /* will be 0px */
}
.auto-scroll ::-webkit-scrollbar-track {
background: #000;
}
.auto-scroll ::-webkit-scrollbar-thumb {
background: #fff;
}
</style>
</body>
</html>
The concept is simple, when the text gets too long for the container, it would infinitely animate (scroll) from left to right, and repeat this processes after it reaches the end.
How should I go about making this?
<!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" />
</head>
<body>
<div class="auto-scroll">
<p>
This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi modi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem
nostrum sint modi velit, ut distinctio. And return to beginning.
</p>
</div>
<style>
.auto-scroll {
background: black;
padding: 18px;
}
.auto-scroll p {
font-family: monospace;
font-size: 18px;
color: white;
margin: 0;
white-space: nowrap;
overflow: auto;
}
.auto-scroll ::-webkit-scrollbar {
height: 10px; /* will be 0px */
}
.auto-scroll ::-webkit-scrollbar-track {
background: #000;
}
.auto-scroll ::-webkit-scrollbar-thumb {
background: #fff;
}
</style>
</body>
</html>
Share
Improve this question
asked Sep 17, 2021 at 19:31
jakey_devjakey_dev
2334 silver badges12 bronze badges
2 Answers
Reset to default 9You don't need javascript you can make it in css like this following code
div{
width: 100%;
background-color: black;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
}
p{
display: inline-block;
color: #FFF;
padding-left: 100%;
animation: move 25s linear infinite;
}
@keyframes move {
0% { transform: translate(0, 0); }
100% { transform: translate(-100%, 0); }
}
<div>
<p>This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi modi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem nostrum sint modi velit, ut distinctio. And return to beginning.</p>
</div>
Improved upon xhxe's answer. This version doesn't show empty space in the beginning and the end, it also gives the user time to read the beginning and the end of the text. Another difference is the change from linear
to ease-in-out
easing.
div{
width: 100%;
background-color: black;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
position: relative;
}
p{
display: inline-block;
color: #FFF;
animation: move 14s ease-in-out infinite;
position: relative;
padding-inline: 1rem;
}
@keyframes move {
25% { transform: translate(0, 0); left: 0%; }
75%, 100% { transform: translate(-100%, 0); left: 100%; }
}
<div>
<p>This text should scroll. Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi modi magni quo officia error ab veniam aspernatur. Quos ducimus, hic, maxime dicta optio quidem nostrum sint modi velit, ut distinctio. And return to beginning.</p>
</div>
To do this I gave position: relative
to both elements and animated the left
property alongside translate
. It works because width in translate
is relative to the element's own width, while width in left
is relative to the element's parent's width.
本文标签: javascriptHow can I make overflow text animate from left to rightStack Overflow
版权声明:本文标题:javascript - How can I make overflow text animate from left to right? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741555150a2385108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论