Get current CSS property value during a transition in JavaScript

前端 未结 2 1827
长情又很酷
长情又很酷 2020-12-29 09:14

I would like to be able to read the value of a CSS property in the middle of a transition before it is fully executed. Is that possible? So if during a transition from 0% to

2条回答
  •  不思量自难忘°
    2020-12-29 09:41

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

    Yes

    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;
    }
    
    

    So far I've only tested firefox & chrome, but it appears that you can get the current CSS height via JS.

    I can't think of a reason why the browser wouldn't report the change in styles to the DOM during a CSS transition.

提交回复
热议问题