简体   繁体   English

CSS 高度未在 Javascript 中过渡

[英]CSS height not transitioning in Javascript

help me out here with why my CSS transition isn't working.帮我看看为什么我的 CSS 转换不起作用。

I'm trying to animate a height change to a <div> element, either growing it or shrinking it, through a Javascript function.我正在尝试通过 Javascript function 对 <div> 元素的高度变化进行动画处理,无论是增长它还是缩小它。 My markup is this:我的标记是这样的:

<div class="post series expandcollapse" id="id1" style="height: 32px;">
    <p class="expandcollapse_link" id="expandcollapselink1" name="expandcollapselink1" onclick="toggleCollapsedItem('1');"><img id="expandcollapseimage1" name="expandcollapseimage1" class="expandcollapse" src="images/icons/expandcollapse.png" alt=""></p>
    <h2 onclick="toggleCollapsedItem('1');">Some random heading here</h2>
    <p>Some random text here</p>
</div>

The CSS attached to the <div>, <h2>, and <img> are:附加到 <div>、<h2> 和 <img> 的 CSS 是:

div.expandcollapse { border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; height: 32px; transition: all 5s ease-in; overflow: hidden; }
div.expandcollapse h2 { cursor: pointer; }
div.expandcollapse img.expandcollapse { transition: all .5s; cursor: pointer; }
div.expandcollapse p.expandcollapse_link { float: right; margin-top: -2px; }

When the user clicks on this, it launches this Javascript function:当用户点击它时,它会启动这个 Javascript function:

function toggleCollapsedItem(clickedItem, forceOpen = false) {
    var collapsedItem = document.getElementById('id' + clickedItem);
    var collapsedLink = document.getElementById('expandcollapselink' + clickedItem);
    var collapsedImage = document.getElementById('expandcollapseimage' + clickedItem);
    if(typeof collapsedItem.originalHeight === 'undefined') {
        Object.defineProperty(collapsedItem, 'originalHeight', { value: collapsedItem.style.height, writable: false });
    }
    if((collapsedItem.style.height === collapsedItem.originalHeight) || (forceOpen === true)) {
        collapsedItem.style.height = 'auto';
        collapsedImage.style.transform = 'rotate(45deg)';
    } else {
        collapsedItem.style.height = collapsedItem.originalHeight;
        collapsedImage.style.transform = '';
    }
}

The image rotates gradually (according to the transition) exactly as it should, but the height doesn't care about the transition.图像完全按照应有的方式逐渐(根据过渡)旋转,但高度不关心过渡。 Can anyone spot my problem here?谁能在这里发现我的问题? I've been up and down Google, Stackoverflow for similar questions, all over the place, and I can't get the height to change gradually.我一直在谷歌和 Stackoverflow 上搜索类似的问题,到处都是,我无法让高度逐渐改变。

Thanks, everyone.感谢大家。

You cannot transition to or from auto values, only between defined values.您不能转换到自动值或从auto值转换,只能在定义的值之间转换。

I'm assuming the element can be of varying height.我假设元素可以具有不同的高度。 If you're already using Javascript, you can transition from 32px to the element's offsetHeight .如果您已经在使用 Javascript,您可以从32px过渡到元素的offsetHeight

So instead所以与其

collapsedItem.style.height = 'auto';

you'd do你会的

collapsedItem.style.height = collapsedItem.offsetHeight+'px';

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

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