简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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