【问题标题】:My UI animation is sloppy using javascript to add and remove animation classes—is there a better way?我的 UI 动画使用 javascript 添加和删除动画类很草率——有没有更好的方法?
【发布时间】:2021-12-19 04:16:53
【问题描述】:

我正在尝试为博客制作此新闻项目组件界面。每个项目显示一个故事图像和文章的一些文本。滚动新闻项目应该“揉搓”图像并显示更多文章的文本。我不明白为什么当您滚动项目时我的动画不保持,然后在执行“解压”之前它完全重置。

有附加和分离到项目的关键帧动画:

@keyframes scrunch {
  from {
    height: 50%;
  }

  to {
    height: 10%;
  }
}

@keyframes unscrunch {
  from {
    height: 10%;
  }

  to {
    height: 50%;
  }
}

.scrunch {
  animation: scrunch 1s;
}

.unscrunch {
  animation: unscrunch 1s;
}

然后我只是在新闻项目类列表中添加和删除这些类:

const scrunchyBox = document.getElementById('scrunchyBox1');
const children = scrunchyBox.childNodes;
console.dir(children);
const scrunchyBoxHead = children[1];

scrunchyBox.addEventListener('pointerover', (event) => {
  scrunchyBoxHead.classList.remove('unscrunch');
  scrunchyBoxHead.classList.add('scrunch');
});

scrunchyBox.addEventListener('pointerout', (event) => {
  scrunchyBoxHead.classList.remove('scrunch'); 
  scrunchyBoxHead.classList.add('unscrunch');
});

看起来很基本,但无论我在做什么看起来都很恶心。我所做的一切都在my Codepen 结束。

【问题讨论】:

    标签: javascript user-interface css-animations


    【解决方案1】:

    您可以将transition 属性与:hover 伪类一起使用。这与您尝试使用 javascript 所做的相同。

    要实现这一点,只需在您的 css 文件中添加几行即可。

    /* new block */
    .scrunchyBox:hover .sectionHead {
      height: 10%;
    }
    /* new block */
    .scrunchyBox:hover .datedot {
      transform: scale(0.45) translate(60px, -72px);
    }
    
    .scrunchyBox > .sectionHead {
      transition: all 0.5s ease-in-out; /* new line */
    }
    .datedot {
      transition: all 0.5s ease-in-out; /* new line */
    }
    
    

    body {
      background-color: #ccc;
      font-size: 18px;
    }
    
    .scrunchyBox {
      color: #333;
      position: relative;
      background-color: #fff;
      width: 400px;
      height: 400px;
      margin: 0 auto;
      padding: 20px;
      overflow: hidden;
      filter: drop-shadow(4px 4px 4px #333);
    }
    
    /* new block */
    .scrunchyBox:hover .sectionHead {
      height: 10%;
    }
    /* new block */
    .scrunchyBox:hover .datedot {
      transform: scale(0.45) translate(60px, -72px);
    }
    
    .scrunchyBox > .sectionHead {
      width: 400px;
      height: 250px;
      background-color: #3ab7f4;
      padding: 0;
      margin: 0 auto;
      transition: all 0.5s ease-in-out; /* new line */
    }
    
    .datedot {
      position: absolute;
      top: 30px;
      right: 30px;
      background-color: rgba(0, 0, 0, 0.3);
      padding: 12px;
      border-radius: 60px;
      width: 60px;
      height: 60px;
      transition: all 0.5s ease-in-out; /* new line */
    }
    
    .datedot > span {
      font-family: 'Trebuchet MS', sans-serif;
      color: rgba(255, 255, 255, 1);
      text-align: center;
      display: block;
      margin: 0;
    }
    
    .datedot > span.day {
      font-size: 2.2em;
      font-weight: bold;
      line-height: 0.8em;
      padding: 0;
    }
    
    .datedot > span.month {
      font-size: 1.3em;
      font-weight: normal;
      text-transform: uppercase;
      padding: 0;
    }
    
    .scrunchyBox > h2 {
      font-family: 'Trebuchet MS', sans-serif;
      font-size: 1.2em;
      line-height: 1em;
      padding: 0;
    }
    
    .scrunchyBox > p {
      font-family: 'Georgia', serif;
      font-size: 1.4em;
    }
    <div id="scrunchyBox1" class="scrunchyBox">
      <div class="sectionHead">
        <div class="datedot">
          <span class="day">30</span>
          <span class="month">Oct</span>
        </div>
      </div>
      <h2>A Headline for the Scrunchy Box</h2>
      <p>
        This is some text that would normally be some text that the reader would want to see more of. It gets cut off here by other elements, but then other elements "scrunch" in order to reveal more of the text for a preview.
      </p>
    </div>

    【讨论】:

    • 是的。我知道这种方法。能不能用来引起多个子元素的变换。在我的 Codepen 示例中我还没有这样做,但我最终计划了多种效果,例如日期点缩小。
    • 是的,当您将:hover 设置为父元素时,您可以使用multiple sub-elements to transform。片段已更新。
    • 太棒了!谢谢开导。使用 :hover 可能比我想象的更强大。
    • 只是给后来出现的任何人的注意事项:您还可以使用transform-origin 将元素缩放到特定点。就我而言,transform-origin: top right; 确保圆圈中的日期向右缩小。
    【解决方案2】:
    1. 您可以使用animation-fill-mode: forwards; 将动画保留在其最终状态,如对此问题的回答所述:Maintaining the final state at end of a CSS3 animation

    2. 如果您在动画中间从盒子中移除指针,它仍然会看起来很乱。我不知道你为什么不想简单地使用 CSS :hover 和过渡。

    【讨论】:

    • 对于您的 #2,:hover 只允许我更改应用悬停的元素内的属性,而不是该元素内的元素。这不对吗?为此,使用事件侦听器来侦听鼠标悬停事件是在做同样的事情,同时允许我更改鼠标悬停元素内的元素。
    • 不,这是不对的,您可以像这样使用 CSS 嵌套:element:hover nestedElement { ... }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2019-04-15
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    相关资源
    最近更新 更多