【问题标题】:Word change in phase, CSS vertical animation loop字相变,CSS垂直动画循环
【发布时间】:2016-10-09 09:01:31
【问题描述】:

我想没有什么比这更好的解释了:


几乎用纯 CSS 完成了我想要的,但是......

  • 我必须手动设置每个单词的“位置”
  • 如果再加一个字,我得重新计算一遍
  • 我无法设置每个单词的 1 秒时间,只能设置动画总时间
  • 响应?就当我重新计算一切

代码是一个带有overflow: hidden 的div 和一个带有negative margin-top(和动画)的孩子。

现场演示

body {
  font-size: 2em;
  font-family: sans-serif;
 }
 
 .animation {
    vertical-align: sub;
    height: 1em;
    overflow: hidden;
    display: inline-block;
    line-height: 2em;
 }
 
ul {
  animation: animation 10s infinite;
  padding: 0;
  margin-top: -0.55em;
  list-style: none;
}

@keyframes animation {
    0%        { margin-top: -0.55em }
    12.5%     { margin-top: -2.54em }
    25%       { margin-top: -4.55em }
    37.5%     { margin-top: -6.55em }
    50%       { margin-top: -8.5em }
    62.5%     { margin-top: -6.55em }
    75%       { margin-top: -4.55em }
    87.5%     { margin-top: -2.54em }
    100%      { margin-top: -0.55em }
}
 Lorem ipsum dolor sit
 
    <div class="animation">
      <ul>
        <li>golfish</li>
        <li>dog</li>
        <li>cat</li>
        <li>hamster</li>
        <li>fox</li>
      </ul>
    </div>
    
, consectetur adipiscing elit. 

那么...

我认为我无法解决我的问题,因为它们是 CSS 限制,所以... 可以用 jQuery 来解决这个问题吗?

【问题讨论】:

  • 这可以通过 jQuery 实现。只需将第一项附加到 (inline-block) 列表即可进行无限循环。之后可以添加动画。
  • @JoostS 这种方法的问题在于,几乎在每个屏幕上,(真实)文本都有 2 行,因此这会导致“动画错误”。我无法准确解释,但结果并不顺利。
  • 我知道您可以在href 中添加一个元素,它会使其聚焦于点击...这有帮助吗?
  • 我真的无法理解那些对可行的解决方案投反对票并且不发表评论的人。真的很难过。可能它一定只是一个卑鄙的傻瓜。 :)

标签: javascript jquery html css animation


【解决方案1】:

这里是没有动画的代码,使用$('ul &gt; li:first-child').appendTo('ul');:http://codepen.io/anon/pen/BzjyBZ

这里添加了动画:http://codepen.io/anon/pen/qNbrva

【讨论】:

  • 现在我明白你想对你的评论说什么了。我使用 jQuery 来“删除()”和“添加()”元素——这就是为什么我说结果不是“平滑”的。您的解决方案非常有趣。谢谢。
  • 我已经更新了答案并添加了动画:一个非常简单且可扩展的解决方案。
【解决方案2】:

使用 jQuery animate() 来实现您想要实现的目标。您可以查看以下示例。

var length = $("li").length;
var p = 0;
var u = 0;
while(u<10000) {
for(i=0;i<length;i++){
  $( "ul" ).animate({
    marginTop: "-"+(i*2)+".55em"
  }, 1000 );
  p = i+1;
}

if(p==length){
for(j=p-1;j>=0;j--){
  $( "ul" ).animate({
    marginTop: "-"+(j*2)+".55em"
  }, 1000 );
}
}
u++;
}
body {
  font-size: 2em;
  font-family: sans-serif;
 }
 
 .animation {
    vertical-align: sub;
    height: 1em;
    overflow: hidden;
    display: inline-block;
    line-height: 2em;
   border: 1px solid #eee;
 }
 
ul {
  padding: 0;
  margin-top: -0.55em;
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 
 Lorem ipsum dolor sit
 
    <div class="animation">
      <ul>
        <li>golfish</li>
        <li>dog</li>
        <li>cat</li>
        <li>hamster</li>
        <li>fox</li>
      </ul>
    </div>
    
, consectetur adipiscing elit. 

这是一个包含更多元素的fiddle

【讨论】:

  • 你的答案是对的,我投了你一票。我只是没有选择“正确答案”,因为选择的答案更好地处理时间动画和“边距顶部”测量。谢谢你的时间,彼得!
  • @Hugo 谢谢。关于最佳答案的问题,这取决于你选择不给我。 :) 干杯!
【解决方案3】:

这里是 CSS 解决方案的一个镜头。不完全确定参考图像的哪些方面很重要,所以我尝试尽可能地匹配它。但是,我确实需要复制标记中的列表,以使其在滚动查看框时将颜色从 lightgray 转换为 black

还要注意动画帧和时间与列表中有多少项目密切相关。如果预计项目数量是动态的,则可能需要一些 JavaScript 干预。

body {
  font-size: 2em;
  font-family: sans-serif;
  line-height: 1.2
}

.animation {
  display: inline-block;
  vertical-align: top;
  text-align: center;
  border: 1px solid;
  overflow: visible;
  white-space: nowrap;
  color: lightgray;
  position: relative;
}

.animation ul {
  list-style: none;
  padding: 0 .5ex;
  margin: 0;
  height: 1.2em;
}

.animation li {
  position: relative;
  animation: cycle-5-items 5s ease-in-out alternate infinite;
}

.animation ul.mask {
  color: black;
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
}

@keyframes cycle-5-items {
  0%, 10% {
    transform: translateY(0%);
  }
  
  15%, 35% {
    transform: translateY(-100%);
  }
  
  40%, 60% {
    transform: translateY(-200%);
  }
  
  65%, 85% {
    transform: translateY(-300%);
  }
  
  90%, 100% {
    transform: translateY(-400%);
  }
}
Lorem ipsum dolor sit
 
    <div class="animation">
      <ul class="mask">
        <li>golfish</li>
        <li>dog</li>
        <li>cat</li>
        <li>hamster</li>
        <li>fox</li>
      </ul>
      <ul>
        <li>golfish</li>
        <li>dog</li>
        <li>cat</li>
        <li>hamster</li>
        <li>fox</li>
      </ul>
    </div>
    
, consectetur adipiscing elit.

【讨论】:

  • 我对您的解决方案感到惊讶,您甚至做了 GIF 中显示的“面具”。我完全忘记了“转换”,到目前为止,它是纯 CSS 的最佳解决方案。谢谢!
【解决方案4】:

You don't need jQuery。这是一个使用css转换的简单纯js解决方案。

function createWordListAnimation(animNode, delay){
  // Fetch the DOM elements.
  var animWindow = animNode.querySelector(".animation-window");
  var ul = animWindow.querySelector("ul");
  var lis = ul.querySelectorAll("li");

  // Copy the animation's window to create the gray list.
  var grayList = animWindow.cloneNode(true);
  var grayUl = grayList.querySelector("ul");
  grayList.classList.remove("animation-window");
  grayList.classList.add("animation-gray-list");
  animNode.insertBefore(grayList, animWindow);

  // This function shows the li number `liNum`.                     
  function goTo(liNum){
    var li = lis[liNum];
    var liTop = li.getBoundingClientRect().top;
    var ulTop = ul.getBoundingClientRect().top;
    var liOffset = liTop - ulTop;
    ul.style.top = -liOffset + "px";
    grayUl.style.top = -liOffset + "px";
  }

  // Set up an interval that changes the current li every `delay` ms.
  var current = 0;
  // We need a boolean to know if the animation is going up or down.
  var ascending = true;
  // Create the interval.
  return setInterval(function(){
    ascending = ascending && current + 1 < lis.length || current === 0;
    current = ascending ? current + 1: current - 1;
    goTo(current);
  }, delay);
}

createWordListAnimation(document.querySelector(".animation"), 1000 /* (ms) */);
body {
  font-size: 2em;
  font-family: sans-serif;
}

.animation {
  vertical-align: sub;
  display: inline-block;
  margin-top: -.1em;
  margin-bottom: -.1em;
  border-style: solid;
  border-color: lightgray;
  border-width: 1px;
  height: 1.2em;
}

.animation-window {
  position: relative;
  height: 100%;
  overflow: hidden;
  background-color: white;
}

.animation-gray-list {
  position: absolute;
  color: lightgray;
}

.animation ul {
  position: relative;
  list-style: none;
  top: 0;
  padding: 0;
  margin: 0;
  transition: top 300ms;
}

.animation li {
  margin: 0 .2em;
  text-align: center;
}
Lorem ipsum dolor sit
 
    <div class="animation">
      <div class="animation-window">
        <ul>
          <li>golfish</li>
          <li>dog</li>
          <li>cat</li>
          <li>hamster</li>
          <li>fox</li>
        </ul>
      </div>
    </div>,

consectetur adipiscing elit.

这种解决方案的缺点是窗口不能是透明的(它必须隐藏灰名单)。仍然可以使其透明,但您必须创建两个灰色列表——一个在上面,一个在下面——它开始有点复杂。

【讨论】:

  • 我想这是最好的答案,因为:不要使用 jQuery;我可以设置过渡时间和单词时间;可以在不更改 de CSS 的情况下添加项目。感谢您的宝贵时间!
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 2022-11-02
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2012-08-30
相关资源
最近更新 更多