javascript - 在 JavaScript 转换期间获取当前 CSS 属性值

标签 javascript css transition

我希望能够在过渡完全执行之前在过渡过程中读取 CSS 属性的值。那可能吗?因此,如果在从 0% 到 100% 的过渡期间,我要在中途检查,是否可以在 50% 时看到它?

最佳答案

Is it possible to get the current css property during a transition in JavaScript?

var timer;

function test(e) {
    var $this;
    $this = $(this);
    timer = setInterval(function () {
        console.log($this.height());
    }, 500);
}
function untest(e) {
    clearInterval(timer);
}

$('div').mouseenter(test).mouseleave(untest);
div
{
    transition: height 10s;
    -moz-transition: height 10s;
    -webkit-transition: height 10s;
    width: 100px;
    height: 100px;
    background-color: #00F;
}

div:hover
{
    height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

到目前为止,我只测试了 firefox 和 chrome,但看起来您可以通过 JS 获取当前的 CSS 高度。

我想不出为什么浏览器不会在 CSS 转换期间向 DOM 报告样式更改的原因。

关于javascript - 在 JavaScript 转换期间获取当前 CSS 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8920934/

相关文章:

javascript - 如何使用 javascript 解析 m3u8 播放列表以获取带宽?

javascript - Coffeescript 中某种 _ref 的用法?

javascript - CSS 指示 "tensed"-div 是过去、现在还是 future ?

html - 调整浏览器窗口大小时内容消失

html - 如何同时在框上悬停和过渡?只使用 css 和 html

javascript - knockout - 限制输入仅包含一位小数点的数字

javascript - 带有不可见层的预加载器覆盖页面

jquery - 自定义选择-溢出隐藏问题

css - 无法设置 CSS3 过渡以允许一秒钟突出显示

android - LayoutTransition 自定义过渡