【问题标题】:Resizing rectangle transition on scroll gets cut off在滚动时调整大小的矩形过渡被切断
【发布时间】:2021-08-29 09:41:28
【问题描述】:

我试图在滚动时调整矩形的大小,当用户向下滚动时,矩形会缩小,当他们向上滚动时,矩形会恢复到原始大小。但是,我遇到了一个问题,如果用户滚动太快,矩形没有足够的时间在用户到达页面顶部/底部之前调整其高度(因此不允许 onScroll 函数更改矩形的高度)。矩形的大小调整没有添加过渡时间。

我假设这是因为矩形的高度取自包含一些文本的 div 的高度。文本也会在滚动条上调整大小,并且有 0.4 秒的过渡时间,这可能会延迟矩形。

有没有办法让矩形继续调整大小,即使在用户停止滚动后也是如此?非常感谢您的帮助!

这是我的代码框架:

    window.onscroll = function() {scrollFunction()};
    var width = window.innerWidth;

    // shrink and grow are functions that change the font size of text

    function scrollFunction() {
      document.getElementById("rect").style.height = document.getElementById("menu").offsetHeight + (width / 100) + "px";
      if (document.body.scrollTop > 80 || document.documentElement.scrollTop > 80) {
        document.getElementById("title").style.fontSize = shrink("title");
        document.getElementById("here").style.fontSize = shrink("here");
      } else {
        document.getElementById("title").style.fontSize = grow("title");
        document.getElementById("here").style.fontSize = grow("here");
      }
    }
    #rect {
      width: 100%;
      position: relative;
      height: clamp(40px, 10vw, 130px);
      background: white;
    }

    #background {
      height: 200vh;
    }

    #title {
      transition: 0.4s;
    }
    <div id="background">
      <div id="rect" style="border: 2px solid green">
        <div id="menu">
         <div id="title">title</div>
         <div id="here">here</div>
        </div>
      </div>
     </div>

【问题讨论】:

  • 更详细地说,矩形开始扩大/缩小,但如果用户停止滚动,则不会一直变形。过渡被切断。我想知道滚动停止时是否有办法继续转换。

标签: javascript html css scroll rectangles


【解决方案1】:

为您的矩形使用transform: scale(/* Your shrink size goes here, say 0.3 */)

【讨论】:

  • 您能详细说明一下吗?为了平滑过渡,文本 div 和矩形都应该同时调整高度,这不需要事先设置转换比例。
  • 请在您的答案中添加一些解释,以便其他人可以从中学习
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2012-12-15
  • 2016-01-14
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
相关资源
最近更新 更多