简体   繁体   中英

Javascript minimize and maximize

I want to scale the div onclick to 25% and back onclick to 50%. Why isn't it scaling back to 50%?

var minimize = document.getElementById('minimize');
var container = document.getElementById('container');

minimize.onclick = function(){

    if(container.style.width = "50%") {
        container.style.width = "25%";
        container.style.height = "25%";
    }else{
        container.style.width = "50%";
        container.style.height = "50%";
    }

    console.log(container.style.width);
}

}

There's an assignment rather than a comparison in your if statement:

if (container.style.width = "50%") {

Should be:

if (container.style.width == "50%") {

The assignment will return the assigned value - "50%". That value is truthy, so the code inside the if statement will be executed.

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