效果展示
源码
<style>
#box {
background-color: yellow;
border: 1px solid red;
width: 300px;
height: 300px;
transition: 2s;
}
#box.active {
width: 0;
height: 0;
}
</style>
<body>
<button onclick="toggleWidthAndHeight()">编辑</button>
<div id="box"></div>
<script>
const box = document.getElementById('box');
let toggled = false;
function toggleWidthAndHeight() {
toggled = !toggled;
box.setAttribute('class', toggled ? 'active' : '');
}
</script>
</body>