【问题标题】:How to animate CSS transition effect via a scroll?如何通过滚动动画 CSS 过渡效果?
【发布时间】:2019-06-30 11:20:57
【问题描述】:

我有一个元素想要“扩展”并更改页面背景的背景颜色。当用户滚动时,中心的一个点将扩展以用新的背景颜色填充页面。我看到了如何更改背景的示例,但没有看到如何“扩展”它。 I have attached a jsfiddle of the CSS animation effect I'm looking for. 这个例子展示了它的外观,但只适用于悬停。如果您滚动示例并将鼠标悬停在白点上,您可以看到它应该是什么样子。1

最好我想用 css 动画来完成这个,但我不反对用 javascript 来尝试。 I've been fiddling around with that here.

其次,我一直在使用假元素来获取示例,但是有没有一种方法可以在不需要元素并且只使用容器的背景颜色的情况下实现这种效果?

这是我想要实现的效果示例的 HTML。

<div class="container">
        <span class="white"></span>
</div>

这是 CSS:

.container {height:500px;width:100%;background:#ed565d;position:relative;}
.container span {display:block;}
.white {background:#ffffff;height:10px;width:10px;margin:auto;border-radius:100%;position:absolute;top:50%;left:50%;}
.container:hover .white {
    width:300%;
    height:300%;
    -moz-transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    -webkit-transition: all 0.5s ease-out;
    transition:all 0.5s ease-out;
    top:-100%;
    left:-100%;
}

【问题讨论】:

    标签: javascript css animation css-animations


    【解决方案1】:

    如果您希望动画与用户在页面上滚动的百分比直接相关,则需要 JavaScript。

    首先,获取滚动百分比。这是一个很好的答案:https://stackoverflow.com/a/8028584/2957677

    const scrollTop = $(window).scrollTop();
    const documentHeight = $(document).height();
    const windowHeight = $(window).height();
    const scrollPercent = (scrollTop / (documentHeight - windowHeight)) * 100;
    

    然后您可以定义一个动画函数,该函数接受用户滚动的百分比,并将圆圈上的样式设置为动画开始时的 CSS 值和结束时的 CSS 值之间的百分比动画。

    function growAnimation($element, animationPercentage) {
      const animationDecimal = animationPercentage / 100;
    
      // Your existing .grow CSS values
      const startPositionPercent = 50; // top/left at start of animation
      const finishSizePercent = 300; // width/height at end of animation
      const finishPositionPercent = -100; // top/left at end of animation
    
      // The current CSS values, based on how far the user has scrolled
      const currentSizePercent = getProgressFromTo(0, finishSizePercent, animationDecimal);
      const currentPositionPercent = getProgressFromTo(startPositionPercent, finishPositionPercent, animationDecimal);
    
    
      $element.css({
        width: `${currentSizePercent}%`,
        height: `${currentSizePercent}%`,
        top: `${currentPositionPercent}%`,
        left: `${currentPositionPercent}%`
      });
    }
    
    // A util function to get the progress between two values
    // e.g. 50% between 0 and 10 is 5
    function getProgressFromTo(from, to, animationDecimal) {
      return from + (to - from) * animationDecimal;
    }
    

    这是一个小提琴:https://jsfiddle.net/owazk8y1

    动画曲线

    您可以查看动画曲线以使动画看起来更平滑。在贝塞尔曲线函数中环绕animationDecimal。下面是一些示例函数: https://gist.github.com/gre/1650294 https://jsfiddle.net/owazk8y1/1

    【讨论】:

      【解决方案2】:

      这是我在这里和那里犯过的不同想法的混合...... 有一小部分 JS,在 CSS 中进行试点

      PS :transition 命令必须在元素上设置

      const storeScroll=()=>{
        document.documentElement.dataset.scroll = window.scrollY;
      }
      
      window.onscroll=e=>{  // called when the window is scrolled.  
        storeScroll()
      }
      
      storeScroll()   // first attempt
      .container {
        position   : relative;
        height     : 500px;
        width      : 100%;
        background : #ed565d;
        overflow   : hidden;  /* added */
      }
      .white {
        display       : block;
        position      : absolute;
        background    : #fff;
        height        : 10px;
        width         : 10px;
        margin        : auto;
        border-radius : 100%;
        top           : 50%;
        left          : 50%;
        -moz-transition    : all 0.5s ease-out;
        -o-transition      : all 0.5s ease-out;
        -webkit-transition : all 0.5s ease-out;
        transition         : all 0.5s ease-out;
      }
      html:not([data-scroll='0']) .white {
        width              : 300%;
        height             : 300%;
        top                : -100%;
        left               : -100%;
      }
      <div class="container">
        <span class="white"></span>
      </div>

      【讨论】:

      • 这很接近。我会尝试使用它,但我想要的是用户看到展开,所以它不应该在用户看到点可见之前开始。感谢您的回答。
      • 非常漂亮的css风格。我立即集成到我的 Sass 代码中。非常感谢。
      猜你喜欢
      • 2020-10-14
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2020-03-20
      • 2017-05-15
      相关资源
      最近更新 更多