[英]How do I prevent my CSS animation from making my page scroll?
I have a CSS animation that sits on a page minding its own business.我有一个 CSS 动画,它位于一个关注自己业务的页面上。 The animation loops through some words by changing the margin-top value of the first word.动画通过更改第一个单词的 margin-top 值来循环播放一些单词。
If I scroll the webpage so that the center of the animation is touching the top of the viewport, the page begins to scroll with the movement of the animation.如果我滚动网页以使动画的中心接触视口的顶部,则页面开始随着动画的移动而滚动。 Nb The page has other items on it so the scrollbar is necessary. Nb 页面上有其他项目,因此滚动条是必要的。
See this in action (animation in a padded div to emulate a scrollable page): https://jsfiddle.net/jtde9rxo/在行动中看到这个(在填充 div 中模拟可滚动页面的动画): https : //jsfiddle.net/jtde9rxo/
How can I make the page ignore this animation, rather than it trying to scroll with the animation's contents?我怎样才能让页面忽略这个动画,而不是试图滚动动画的内容?
Here's the code used in the JSFiddle:这是 JSFiddle 中使用的代码:
<div class="page">
<h1>
<span class="word-swap-container">
<span class="word-swap-mask">
<span class="word-swap word-1">Apple</span>
<span class="word-swap word-2">Orange</span>
<span class="word-swap word-3">Pear</span>
<span class="word-swap word-4">Apple</span>
</span>
<span class="word-swap-append">is a fruit</span>
</span>
</h1>
</div>
.page {
padding: 150px 0 1000px;
}
.word-swap-container {
display: block;
overflow: hidden;
}
.word-swap-mask {
overflow: hidden;
display: inline-block;
vertical-align: top;
text-align: right;
height: 1.1em;
line-height: 1;
}
.word-swap {
display: block;
height: 1.1em;
line-height: 1;
}
.word-swap-append {
display: inline-block;
vertical-align: top;
line-height: 1;
}
.word-swap.word-1 {
animation: word-swap-move-1 3s linear infinite;
}
.word-swap.word-2 {
animation: word-swap-move-2 3s linear infinite;
}
.word-swap.word-3 {
animation: word-swap-move-3 3s linear infinite;
}
.word-swap.word-4 {
animation: word-swap-move-4 3s linear infinite;
}
@keyframes word-swap-move-1 {
0% {margin-top: 0em}
27% {margin-top: 0em;opacity:1}
33% {margin-top: -1.1em;opacity:0}
60% {margin-top: -1.1em}
66% {margin-top: -2.2em}
93% {margin-top: -2.2em;opacity:0}
100% {margin-top: -3.3em;opacity:1}
}
@keyframes word-swap-move-2 {
27% {opacity:0}
33% {opacity:1}
60% {opacity:1}
66% {opacity:0}
}
@keyframes word-swap-move-3 {
60% {opacity:0}
66% {opacity:1}
93% {opacity:1}
100% {opacity:0}
}
@keyframes word-swap-move-4 {
93% {opacity:0}
100% {opacity:1}
}
Try This:尝试这个:
.word-swap-container {
display: block;
overflow: hidden;
position: absolute; // New entry
} }
By default the value of position is Static and An element with position: static;默认情况下, position 的值是Static和一个具有 position: static; 的元素。 is not positioned in any special way;没有以任何特殊方式定位; it is always positioned according to the normal flow of the page .它始终按照页面的正常流程进行定位。
So the position Absolute is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).因此绝对位置相对于最近的定位祖先定位(而不是相对于视口定位,如固定)。
Have a Great Day!祝你有美好的一天!
Make the following edits to your code:对您的代码进行以下编辑:
.page { /*padding: 150px 0 1000px;*/ } .word-swap-container { position: absolute; display: block; top: 10%; left: 10%; } .word-swap-mask { overflow: hidden; display: inline-block; vertical-align: top; text-align: right; height: 1.1em; line-height: 1; } .word-swap { display: block; height: 1.1em; line-height: 1; } .word-swap-append { display: inline-block; vertical-align: top; line-height: 1; } .word-swap.word-1 { animation: word-swap-move-1 3s linear infinite; } .word-swap.word-2 { animation: word-swap-move-2 3s linear infinite; } .word-swap.word-3 { animation: word-swap-move-3 3s linear infinite; } .word-swap.word-4 { animation: word-swap-move-4 3s linear infinite; } @keyframes word-swap-move-1 { 0% {margin-top: 0em} 27% {margin-top: 0em;opacity:1} 33% {margin-top: -1.1em;opacity:0} 60% {margin-top: -1.1em} 66% {margin-top: -2.2em} 93% {margin-top: -2.2em;opacity:0} 100% {margin-top: -3.3em;opacity:1} } @keyframes word-swap-move-2 { 27% {opacity:0} 33% {opacity:1} 60% {opacity:1} 66% {opacity:0} } @keyframes word-swap-move-3 { 60% {opacity:0} 66% {opacity:1} 93% {opacity:1} 100% {opacity:0} } @keyframes word-swap-move-4 { 93% {opacity:0} 100% {opacity:1} }
<div class="page"> <h1> <span class="word-swap-container"> <span class="word-swap-mask"> <span class="word-swap word-1">Apple</span> <span class="word-swap word-2">Orange</span> <span class="word-swap word-3">Pear</span> <span class="word-swap word-4">Apple</span> </span> <span class="word-swap-append">is a fruit</span> </span> </h1> </div>
Removes scroll bar entirely, hope this helps.完全删除滚动条,希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.