简体   繁体   English

有没有办法在不添加类的情况下在 js 中添加过渡效果(平滑/缓和)?

[英]Is there a way to add transition effect (smooth/ease) in js without adding a class?

Si I have a simple few lines of code, basically what it does is when you click on the div that has the id, it disappear (display :none). Si 我有几行简单的代码,基本上它的作用是当您单击具有 id 的 div 时,它会消失(显示:none)。 Normally I would do an addClass / removeClass but I was wondering if just with this code I could add an effect that doesn't make the div brutally disappear but maybe with a transition to the left or the top with 0.8 seconds.通常我会做一个 addClass / removeClass 但我想知道是否仅使用此代码我可以添加一个不会使 div 残酷消失的效果,但可能会在 0.8 秒内过渡到左侧或顶部。 How would you proceed?你将如何进行?

window.onload = function(){
    var divToHide = document.getElementById('divToHide');
    document.onclick = function(e){
        divToHide.style.display = 'none';
      }
  };

If you want to use jquery, you can use slide methods of the same.如果你想使用 jquery,你可以使用相同的幻灯片方法。 Constraint is that you will have to use jquery.约束是你将不得不使用 jquery。

$("#divToHide").slideDown(2000); $("#divToHide").slideDown(2000); //2000 is the ms for the effect //2000 是效果的毫秒数

However you can also apply transition-timing-function in css.但是,您也可以在 css 中应用 transition-timing-function。 If you keep the transition-timing-function: linear and go from width 100% to zero width, it will have the effect of sliding and from to to bottom, transition on height.. https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function如果你保持transition-timing-function:linear 并且从宽度100% 到零宽度,它就会有滑动的效果,从下到下,在高度上过渡.. https://developer.mozilla.org/en -US/docs/Web/CSS/transition-timing-function

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

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