简体   繁体   English

如何防止我的 CSS 动画使我的页面滚动?

[英]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.

相关问题 如何防止滚动效果使屏幕在滚动时闪烁 - How to prevent scroll effect from making my screen flicker on scroll 如何防止CSS过渡影响页面上的其他图标? - How do I prevent a CSS transition from affecting my other icons on my page? 如何在 HTML CSS 中使我的滚动始终位于页面的末尾。 我希望我的卷轴总是在页面的末尾 - How do I make my scroll always in the end of the page in HTML CSS. I want my scroll to always be at the end of the page 如何防止我的页面在提交时刷新 - How do I prevent my page from refreshing on submit 如何让我的 CSS '打字机' animation 在我完成“输入”我的文本时停止,而不是仅仅在我的页面上移动? - How do I make my CSS 'typewriter' animation from stopping when my it has finished 'typing' out my text, instead of just moving all across my page? 如何防止我的平滑滚动jquery函数修改div元素的内容? - How do I prevent my smooth-scroll jquery function from modifying the contents of my div elements? 如何防止PHP删除背景图片CSS语句的斜线? - How do I prevent PHP from stripping my Slashes of my background-image CSS statement? 如何防止我的桌子随着滚动而移动以及如何使其居中? - How do I prevent my table from moving with the scroll & How to keep it centered? 如何防止我的网站汉堡菜单让人们水平滚动 - How do I prevent my websites hamburger menu from letting people scroll horizontally 如何防止按钮在页面上重叠文本? - How do I prevent my button from overlapping text on my page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM