Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle div size on click

I'm trying to make div bigger on click. After clickig again, I'd like to make the div small. Is there a way to do this? And could I add animation to it?

document.addEventListener('click', function(event) {
  if (!event.target.matches('.hud-map')) return;
  event.preventDefault();
  var map = document.getElementById("hud-map");
  map.style.height = "200px";
  map.style.width = "200px";
}, false);
<div id="hud-map" class="hud-map" style="width: 100px; height: 100px;
 border-radius: 1em; color: rgb(211, 84, 0); border: 2px solid rgb(0, 0, 0);">

</div>
like image 649
Numnut Avatar asked Dec 31 '25 21:12

Numnut


1 Answers

You can use CSS to handle the animation with something like transition: all .5s;, and to handle the toggling of the size, a ternary condition like (map.style.height == '200px') ? "100px" : "200px";

document.addEventListener('click', function(event) {
  if (!event.target.matches('.hud-map')) return;
  event.preventDefault();
  var map = document.getElementById("hud-map");
  map.style.height = (map.style.height == '200px') ? "100px" : "200px";
  map.style.width = (map.style.width == '200px') ? "100px" : "200px";
}, false);
#hud-map {
  width: 100px;
  height: 100px;
  border-radius: 1em;
  color: rgb(211, 84, 0);
  border: 2px solid rgb(0, 0, 0);
  transition: all .5s;
}
<div id="hud-map" class="hud-map">
</div>
like image 118
j08691 Avatar answered Jan 03 '26 12:01

j08691



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!