开发者

JavaScript Resize w/ CSS3 Issue

I'm writing a Firefox extension, and one of the features is to be able to add an extra "row' of icons in the interface. I'm doing this using a combination of CSS and JavaScript, but I'm getting a weird issue if I click too quickly on the "expand" or "contract" button, where it's adding a random amount of pixels to the height. I think it's because it's getting the height from the css, and while it's transitioning, it has a "growing" height, so it does the JS equation with the wrong height.

CSS:

#wrapper {
    background: rgba(0,0,0,.85);
    box-shadow: 0px 0px 5px #000;
    color: #fff;
    height: 100px;
    width: 250px;
            -moz-transition: height 1s;
}

JavaScript:

function expand() {
            var orig = document.getElementById("wrapper").clientHeight;
            if (orig < 300) {
                var changed = orig + 100;
                document.getElementById("wrapper").style.height=changed;
                // debug
                document.getElementById("print").innerHTML="height: " + changed + "px";
                // end debug
            } else {
                // do nothing
            }
        }
        function contract() {
            开发者_如何转开发var orig = document.getElementById("wrapper").clientHeight;
            if (orig > 100) {
                var changed = orig - 100;
                document.getElementById("wrapper").style.height=changed;
                // debug
                document.getElementById("print").innerHTML="height: " + changed + "px";
                // end debug
            } else {
                // do nothing
            }
        }

HTML:

<div id="wrapper">
    <a onclick="expand()">Exand Div</a> - <a onclick="contract()">Contract Div</a><br />
    <span id="print">height: 100px</span>
</div>


The transitionend event could be the solution here.

Just disable your onclick handlers once clicked, then re-enable when the transitionend fires.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜