有没有办法给 jQuery 添加一个 smooth/ease 过渡效果?

Is there a way to add a smooth/ease transition effect to jQuery?

我有这段代码可以在用户滚动页面的某个位置时修改我页面上元素的位置:

<script type="text/javascript">
jQuery(window).scroll(function() {
  if(jQuery(window).scrollTop() > 150) {
    jQuery("#tab_toggle #tab_toggle_bg").attr("style","top: 45% !important"); 
  } else {
    jQuery("#tab_toggle #tab_toggle_bg").removeAttr("style"); 
  }
});
</script>

有没有办法在scrollTop效果触发的时候给这个添加类似CSStransition: all 0.5s ease的效果?

只要您有权访问 CSS 文件并允许对其进行编辑,实现缓动效果的最简单方法就是通过以下方式向元素添加所需的 transition 设置CSS。通过 CSS 添加到元素的任何过渡设置将仍然适用于通过 JavaScript 完成的 属性 更改。

jQuery(window).scroll(function() {
  if (jQuery(window).scrollTop() > 50) {
    jQuery("#tab_toggle #tab_toggle_bg").attr("style", "top: 45% !important");
  } else {
    jQuery("#tab_toggle #tab_toggle_bg").removeAttr("style");
  }
});
#tab_toggle {
  width: 100%;
  height: 500px;
  position: relative;
}
#tab_toggle_bg {
  position: absolute;
  background: red;
  color: white;
  text-align: center;
  line-height: 140px;
  width: 140px;
  height: 140px;
  top: 0%; /* an initial value for the property that needs to be transitioned is also required */
  transition: all 0.5s ease;  /* add the required timing function - ease, ease-in, ease-in-out, linear */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tab_toggle">
  <div id="tab_toggle_bg">
    Scroll the page for more than 50px.
  </div>
</div>