【问题标题】:How to make 'rainbow' border movement on scroll如何在滚动条上进行“彩虹”边框移动
【发布时间】:2019-11-18 00:47:49
【问题描述】:

我正在尝试重新创建类似于桌面上https://codepen.io/“开始编码”按钮(移动的彩虹边框)上的悬停状态的效果,但是我希望在滚动期间发生移动,然后在滚动时停止用户停止滚动。

codepen“开始编码”按钮由两个组件组成,给人以彩虹边框的印象。彩虹 div 和其中的黑色跨度。跨度比div小,给人彩虹边框的印象。

【问题讨论】:

  • 如果您能告诉我们您到目前为止尝试了什么,将会很有帮助。
  • 仅供参考,您的链接已损坏

标签: css scroll background-image border linear-gradients


【解决方案1】:

只有在滚动时才需要使用 javascript 来做某事。我可以向您展示如何使用 jquery。也许其他人可以用 vanilla js 做到这一点。

您应该将 css 动画帧添加到您的元素 (more info) 并将它们置于暂停状态 animation-play-state: paused;。然后在滚动时只需设置animation-play-state: running;,如果用户在短时间内没有滚动,则将其设置回animation-play-state: paused; 我们可以使用jquery 使用.scroll() 这样做,如下所示:

$(document).ready(function(){

    //animation plays only when window scrolls
    //1 catch window scrolling
    $( window ).scroll(function() {
        //2 change play state to running
        $( '.scrollcolors' ).css( 'animation-play-state', 'running' );
        //3 when window stops scrolling
        $(window).scroll(function() {
            clearTimeout($.data(this, 'scrollTimer'));
            $.data(this, 'scrollTimer', setTimeout(function() {
                //4 set play state back to paused
                $( '.scrollcolors' ).css( 'animation-play-state', 'paused' );
            //5 set time without scrolling
            }, 500));
        });
    });

});

此处的工作示例(包括 html 和 css):jsfiddle
jsfiddle 使用窗口进行滚动。

并包含在此 sn-p 使用容器进行滚动

$(document).ready(function(){

    //animation plays only when window scrolls
    //1 catch window scrolling
    $('.container').scroll(function() {
        //2 change play state to running
        $( '.scrollcolors' ).css( 'animation-play-state', 'running' );
        //3 when window stops scrolling
        $('.container').scroll(function() {
            clearTimeout($.data(this, 'scrollTimer'));
            $.data(this, 'scrollTimer', setTimeout(function() {
                //4 set play state to paused
                $( '.scrollcolors' ).css( 'animation-play-state', 'paused' );
            //5 set time without scrolling
            }, 500));
        });
    });
    
});
.container {
  width: 300px;
  height: 300px;
  overflow-y: scroll;
}
.content {
  position: relative;
  height: 600px;
}
.scrollcolors span {
  position: absolute;
  top: 5px;
  bottom: 5px;
  left: 5px;
  right: 5px;
  border-radius: 3px;
  background-color: black;
  color: white;
  text-align: center;
  padding: 0.6em;
}
.sc1{
  top: 20px;
}
.sc2{
  top: 170px;
}
.sc3{
  top: 320px;
}
.scrollcolors {
  position: absolute;
  width: 200px;
  height: 50px;
  margin: 10px;
  border-radius: 6px;
  background-color: yellow;
  background-image: repeating-linear-gradient(-45deg, #cf2525, #e0c71b 1rem, #0caa0d 2rem, #234ac1 3rem, #7e1cc2 4rem, #cf2525 5rem);
  background-position: 0% 0%;
  background-size: 1000%;
  background-repeat: repeat;
  animation-name: colors;
  animation-duration: 2s;
  animation-iteration-count: infinite;
  animation-direction: normal;
  animation-play-state: paused;
  animation-timing-function: linear;
}

@keyframes colors {
  0% {
    background-position: 0% 0%;
  }
  100% {
    background-position: 18.9% 18.9%;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="content">
    <div class="scrollcolors sc1">
    <span>scroll to animate</span>
    </div>
    <div class="scrollcolors sc2">
    <span>scroll to animate</span>
    </div>
    <div class="scrollcolors sc3">
    <span>scroll to animate</span>
    </div>
  </div>
</div>

注意事项:

  • 我没有精确复制 codepen 的渐变颜色,我只是做了一个快速的通用彩虹渐变。

  • 可能有更好的方法来设置无缝重复的动画渐变。我将背景大小设置为 1000%,并将位置设置为与渐变相同的 45 度角移动,以避免任何明显的接缝。

【讨论】:

  • 嘿,谢谢你。这是一个很好的解决方案。你的机器有轻微的延迟吗?就像当你停止滚动时,它似乎会运行一段时间然后停止移动?
  • 是的。请参阅脚本中的注释,其中说//5 set time without scrolling 下面的行具有超时功能的编号。那是用户在注册之前不能滚动的时间。它以毫秒为单位设置。它设置为 500,因此是半秒。您可以将其设置为 200 或 100,它几乎是即时的。我不建议将其设置为低于 100。
  • 谢谢。为什么不低于100?在我的脑海中,我将其降至 0...(编辑:我在 jsfiddle 中设置为零,这是我想要的完美效果。感谢您的帮助!)
  • 因为它会尝试每秒检查一百万次,这将导致它运行不佳。从技术上讲,计算机上的一切都是数字的。所以滚动发送滚动停止滚动停止滚动,打开和关闭。因此,它可能会不必要地多次重新运行整个功能。最好限制检查窗口大小调整滚动的功能,这些功能似乎是连续的。对于这样的事情,十分之一秒(100 毫秒)的响应速度已经足够快了。
  • 嗯好的,这是一个很好的观点。您能想出一种方法来提高与 0 相同级别的响应能力的性能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 2022-06-10
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多