简体   繁体   English

JS中改变元素样式时如何添加animation?

[英]How to add animation when changing element style in JS?

I want to add a slide down animation to the element when display being set to block , and a slide up when it's none or '' , here's my actual js script:我想在 display 设置为block时向元素添加向下滑动 animation ,当它为none''时向上滑动,这是我的实际 js 脚本:

<script>
  function showQuick(element, icon) {
    var style = element.style
    if (style.display === '') {
      style.display = 'block';
      icon.style.color = '#00FFFF';
      icon.style.filter = 'drop-shadow(0 0 0.35rem #00FFFF)';
    } else if (style.display === 'none') {
      style.display = 'block';
      icon.style.color = '#00FFFF';
      icon.style.filter = 'drop-shadow(0 0 0.35rem #00FFFF)';
    } else {
      style.display = 'none';
      icon.style.color = '#D2042D';
      icon.style.filter = 'drop-shadow(0 0 0.35rem #D2042D)';
    };
  };
</script>

just try this, it is simple试试这个,很简单

 div { position: absolute; top: -20px; transition: top 1s; }
 <div>hello everyone!</div>

now when you set style.top = "0px";现在当你设置style.top = "0px"; in your code, you will get a sliding effect在您的代码中,您将获得滑动效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM