Search code examples
csscss-transforms

Border-radius not working during CSS transform


Trying to make a rounded menu background. But the border-radius is not working while closing

var menuButton = document.querySelector('.btn-menu');
    menuButton.addEventListener('click', function(e){
        e.preventDefault();
        document.body.classList.toggle('menu-open');
    });
.btn-menu{
  width: 30px;
  height: 30px;
  border: 2px solid red;
  position: relative;
  z-index: 5;
  float: right;
}
.menu-bg {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: 100%;
z-index: 40;
pointer-events: none;
z-index: 1;
}
.menu-bg:before {
content: '';
height: 1px;
width: 1px;
background: #000;
position: fixed;
right: 30px;
top: 30px;
transition: all ease .8s;
border-radius: 50%;
transform: scale(1);
overflow:hidden;
}
.menu-open .menu-bg:before {
transform: scale(500);
}
<div class="btn-menu"><a href=""><span>Menu</span></a></div>
<div class="menu-bg"></div>

JSFiddle - https://jsfiddle.net/afelixj/ew7b065h/


Solution

  • 1px as width/height is not a good idea, I would do it differently and start at scale(0):

    var menuButton = document.querySelector('.btn-menu');
    menuButton.addEventListener('click', function(e) {
      e.preventDefault();
      document.body.classList.toggle('menu-open');
    });
    .btn-menu {
      width: 30px;
      height: 30px;
      border: 2px solid red;
      position: relative;
      z-index: 5;
      float: right;
    }
    
    .menu-bg {
      position: fixed;
      left: 0;
      top: 0;
      bottom: 0;
      width: 100%;
      z-index: 40;
      pointer-events: none;
      z-index: 1;
    }
    
    .menu-bg:before {
      content: '';
      height: 100px;
      width: 100px;
      background: #000;
      position: fixed;
      right: -20px;
      top: -20px;
      transition: all ease .8s;
      border-radius: 50%;
      transform: scale(0);
      overflow: hidden;
    }
    
    .menu-open .menu-bg:before {
      transform: scale(5);
    }
    <div class="btn-menu"><a href=""><span>Menu</span></a></div>
    <div class="menu-bg"></div>