【问题标题】:Animate overlay from origin relative to an element从相对于元素的原点动画叠加
【发布时间】:2019-01-31 07:21:31
【问题描述】:

我正在尝试对叠加层进行动画处理,以从触发叠加层的 UI 元素扩展到全屏。例如,我有一个按钮,当单击它时会显示一个叠加层,我希望叠加层从按钮展开(在按钮上方或下方,此时并不重要)。

我目前正在使用类似于this question 上的第二个答案的东西。问题是position: fixed 规则从文档流中删除了覆盖(示例中为#box),因此定位是相对于整个文档的。

所以我想问题是,是否可以保持流动,或者使用相对定位来达到预期的结果?目前我能想到的唯一解决方案是使用 JS 在加载时适当地设置transform: translate 定位并调整大小以匹配按钮位置。如果有更优雅的方式来实现这一点,我会很高兴。

编辑: 下面的代码几乎是从上面的问题中逐字删除的,我想添加一个button 或类似的交互元素,使动画相对于它的位置而不是视口产生.

document.getElementById('box').addEventListener('click', (e) => e.target.classList.toggle('fullscreen'));
    #box {
        position: fixed;
        cursor: pointer;
        background-color: red;
        width: 100vw;
        height: 100vh;
        top: 0;
        left: 0;
        transform: translate(50px, -50px) scale(0.1);
        transform-origin: left bottom;
        transition: all 0.7s ease;
    }

    #box.fullscreen {
          transform: translate(0, 0) scale(1);
    }
<div id="box"></div>

编辑: 再想一想,动画背景变化,比如radial-gradient 也可以解决问题,我只需要隐藏元素直到动画完成,但它可能是一种选择。动画起源到全屏效果是重要的部分。如果我在此期间取得任何实质性进展,将会更新。非常感谢任何输入!

【问题讨论】:

  • 您能发布您所做的事情以及问题所在。我真的不明白这个问题。所以你希望覆盖从按钮开始然后传播到整个屏幕(甚至在按钮上)?是这样吗 ?如果是这样可以使用jq吗
  • 嗨@Alen.Toma!谢谢回复。到目前为止,我所拥有的正是answer linked above 中的代码,只是修改了颜色和初始尺寸。唯一的区别是我想将#box 相对于另一个元素而不是视口定位。是的,使用 JS(或 jQ)不是问题,正如我上面提到的关于转换的计算。我完全愿意接受建议。谢谢一百万!

标签: javascript css


【解决方案1】:

好的,我做了一些更改,它应该可以按您的意愿工作。请务必阅读评论,以便您了解想法。

$(".expand").on("click", function() {
  var rect = this.getBoundingClientRect();
  var box = $("#box");
  // make sure that the position of the overlay start from the clicked element
  box.css({ left:rect.left , top: rect.top + rect.height }); 
   
  box.toggleClass("fullScreen").delay(500).queue(function() {
    box.css({ left:0 , top: 0 }); // the toggle finished then reset the left and top so it overlay the whole screen 
  });

});
/* YOUR BOX */
#box{
  position: fixed;
  cursor: pointer;
  background-color: red;
  /* make the box full screen */
  /* scale it down to 0.1 (10%) initially,
     make an offset from bottom left */
  -ms-transform: translate(0px, 0px) scale(0.1); /* IE9 */
  -ms-transform-origin: left top; /* IE9 */
  transform: translate(0px, 0px) scale(0.1);
  transform-origin: left top;
  width:0;
  height:0;
  overflow:hidden;
  /* smooth transition (IE10+) */
  -webkit-transition: all 0.7s ease;
          transition: all 0.7s ease;
}

/* ADD THIS CLASS WITH JS */
#box.fullScreen {
  width: 100vw; /* IE9+ */
  height:100vh; /* IE9+ */
  -ms-transform: translate(0px, 0px) scale(1); /* IE9 */
  transform: translate(0px, 0px) scale(1);
}

.expand{

margin:auto;
width:100px;


}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="expand">Click Me</div>


<div id="box">your content</div>

【讨论】:

  • 你好@Alen.Toma!我想在接受您的回答之前稍等片刻,看看是否提交/可能提交了仅 CSS 的解决方案。不过,您的解决方案效果很好! :+1: 最后我选择使用转换创建/修改自定义选择器,但这只是个人喜好。非常感谢您抽出宝贵时间!网络学生
  • Np,很高兴我能帮上忙
猜你喜欢
  • 2021-09-16
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多