简体   繁体   English

使用javascript函数在div上应用CSS过渡属性

[英]Apply CSS transition property on div with javascript function

I have used javascript code to increase and decrease the height of div on click event. 我已使用javascript代码在click事件上增加和减少div的高度。 But i also want to apply transition-duration property so that transition goes smoothly. 但我也想应用过渡持续时间属性,以便过渡顺利进行。

Here is the css code 这是CSS代码

#content {
    height: auto;
    width:400px;
    background-color:#F30;
    transition-duration:2s;
}

#content.expand {
    height: 300px;
    transition-duration:2s;
}

This is the javascript code 这是JavaScript代码

function chk()
{
    var node = document.getElementById('content');

    node.classList.toggle('expand');
}

The function is working properly. 该功能正常运行。 I have applied transition property, but that is not working. 我已经应用了transition属性,但是没有用。

According to Bugzilla, this is the Bug 571344 . 根据Bugzilla的说法,这是Bug 571344

Transition to and from auto affects Firefox (that doesn't transition at all ), but works in quirks ways in other browsers too. 过渡到从auto影响火狐(不转变的话 ),但在其他浏览器怪癖方面也起作用。

For example, in Chrome height: auto is treated like height: 0 in the transition, and you will see the red background of the example being smaller up to disappear, then restoring to the real "auto" value. 例如,在Chrome浏览器中height: auto在过渡中被视为height: 0 ,您将看到示例的红色背景逐渐变小直至消失,然后恢复为实际的“ auto”值。

Simply specify an height, and it will work good, in every browser: Running demo 只需指定一个高度,它就可以在每个浏览器中正常工作: 运行演示

Try cross browser solution like this: 试试这样的跨浏览器解决方案:

 transition-duration:2s; 
 -o-transition-duration:2s; 
 -moz-transition-duration:2s; 
 -webkit-transition-duration:2s;

instead of only transition-duration:2s; 不仅是transition-duration:2s;

It is a problem with the value transform, in Mozilla it is prefixed with -moz-transform. 值转换是一个问题,在Mozilla中以-moz-transform为前缀。

#content {
    height: auto;
    width:400px;
    background-color:#F30;
    -webkit-transition:all .1s ease;
 -moz-transition:all .1s ease;
 -ms-transition:all .1s ease;
 transition:all .1s ease;
}

#content.expand {
    height: 300px;
   -webkit-transition:all .1s ease;
 -moz-transition:all .1s ease;
 -ms-transition:all .1s ease;
 transition:all .1s ease; 
}

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

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