有没有办法忽略基于css属性的某些transitionend事件?

时间:2015-01-08 02:40:39

标签: javascript css

有没有办法忽略某些基于css属性的transitionend事件?例如,在以下代码中,高度转换比左转换更快结束。有没有办法忽略高度转换以获得最后一个transitionend事件?

HTML:

<div id="testButton"></div>

<div id="wrapper" class="wrapper">
    <div id="menu" class="menu">
        <div id="menu-sets" class="menu-sets">

        </div>
    </div>
</div>

的CSS:

#testButton {
    position: absolute;
    top: 0;
    left: 200px;
    width: 100px;
    height: 100px;

    background-color: #00ff00;
}
.menu-sets {
    position: absolute;
    visibility: hidden;
    overflow: hidden;
    top: 0;
    left: -50px;
    z-index: 100;
    width: 100px;
    height: 100px;

    background-color: #ff0000;

    transition: left 3.3s ease-out, height 0.2s ease-out;
}
.show-menu-1 .menu-sets {
    visibility: visible;
    left: 0px;
    height: 200px;
}

JS:

$(document).ready(function () {
    $("#testButton").click(
        function () {
            $('#wrapper').addClass('show-menu-1'); // show menu
        }
    );

    $('#menu-sets').one('transitionend', function() { 
        alert("event done");
    });
});

jsfiddle例子:

http://jsfiddle.net/rotaercz/zysxsc41/2/

1 个答案:

答案 0 :(得分:1)

可以肯定的是,原始事件包含有关正在转换的属性的信息

$('#menu-sets').on('transitionend', function(event) { 

    if ( event.originalEvent.propertyName === 'left' ) {
        alert('done');
        $('#menu-sets').off('transitionend');
    }

});

由于one()仅触发一次,您必须使用on()并取消绑定

FIDDLE