【问题标题】:Prevent animation jump when making div fixed修复 div 时防止动画跳转
【发布时间】:2018-12-31 23:54:19
【问题描述】:

我正在研究浮动 div 效果,当向下滚动时,div 将移动到顶部底部。问题是当您向下滚动并单击按钮以使 div 固定时,动画只是跳出顺序。它应该更平滑地移动到旧位置。

<div id="container">
  <button id="toggleFix" onclick="document.querySelector('#element').classList.toggle('fixed')">Make the div fixed</button>
  <div id="element"></div>
</div>

js:

var el = document.querySelector("#element"),
elPos = el.getBoundingClientRect();

el.style.left = elPos.left + "px";
el.style.right = elPos.right + "px";
el.style.top = elPos.top + "px";
el.style.bottom = elPos.bottom + "px";

https://codepen.io/anon/pen/ajWaaM

var el = document.querySelector("#element"),
  elPos = el.getBoundingClientRect();

el.style.left = elPos.left + "px";
el.style.right = elPos.right + "px";
el.style.top = elPos.top + "px";
el.style.bottom = elPos.bottom + "px";
body {
  height: 150vh;
}

#container {
  max-width: 1200px;
  margin: auto;
}

#toggleFix {
  margin-bottom: 1em;
  font-size: 20px;
  border: none;
  padding: 10px 20px;
  font-weight: bold;
  position: fixed;
  right: 20px;
  bottom: 0;
}

#element {
  background: black;
  transition: all 0.3s ease;
  width: 400px;
  height: 300px;
}

#element.fixed {
  position: fixed;
  top: 845px !important;
  left: 10px !important;
  right: auto !important;
  bottom: 10px !important;
  width: 200px !important;
  height: 150px !important;
}
<div id="container">
  <button id="toggleFix" onclick="document.querySelector('#element').classList.toggle('fixed')">Make the div fixed</button>
  <div id="element"></div>
</div>

【问题讨论】:

  • 您能否更准确地解释一下按下按钮的结果?您的 JS 和 CSS 似乎相互矛盾。
  • 我只是试图修复页面左下角的 div,当删除使其无法正常运行的类时,动画完全无序

标签: javascript css transition floating


【解决方案1】:

目前您正在将元素移出屏幕 (top: 845px !important)。

将其替换为top: auto !important 会将元素移动到左下角并且即使在滚动时它也会保留在那里。

注意:这可能/将会影响页面布局的其余部分。

【讨论】:

  • 我想这是想要的。 OPs 的问题是,让它恢复的动画并不流畅。它只是跳到位置并变大。我正在尝试解决这个 atm。
【解决方案2】:

为了有效地transition 位置,您必须至少设置四个位置之一 (top,left,right or bottom)。

另外,我认为您不需要 JS。我设法在没有 js 的情况下实现了这一点。

Here's the CodePen to play with

body{
 height: 150vh; 
}
#toggleFix {
  margin-bottom: 1em;
  font-size: 20px;
  border: none;
  padding: 10px 20px;
  font-weight: bold;
  position: fixed;
  right: 20px;
  bottom: 0;
}
#element{
  position: absolute;
  top: 8px;
  left: 450px;
  background: black;
  width: 400px;
  height: 300px;
  transition: all 0.3s ease;
}
#element.fixed{
  position: fixed;
  top: 845px ;
  left: 10px ;
  right: auto ;
  bottom: 10px ;
  width: 200px ;
  height: 150px ;
}
<div id="container">
  <button id="toggleFix" onclick="document.querySelector('#element').classList.toggle('fixed')">Make the div fixed</button>
  <div id="element">
  </div>
</div>

编辑:您可以为您的 div 使用绝对位置,但这样容器将毫无用处。不幸的是,这是我目前让它工作的唯一方法。为了进一步解决,我必须更深入地研究它。

【讨论】:

  • 谢谢,但是如果 div 在容器中,页面会再次跳转
  • @Eli 你能帮我澄清一下吗?我不明白,你的意思。你想让元素完全消失,还是你想要达到什么目的?
  • 我明白了,问题是,一旦你改变了position-属性,元素就会从容器中释放出来并跳转到相对位置,但相对于身体,而不是容器。我会尝试看看我们如何解决这个问题。
  • 我编辑了答案和CodePen。我使用position:absolute 从容器中释放元素,但我认为这不符合您的利益。目前这是一种解决方法,但如果您出于某种原因依赖容器,那么您必须再次查看它。
猜你喜欢
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多