一个元素设置了transition属性,宽高分别从0变为100%,默认是从左上角往左下角变的,如何从元素中心开始变?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .block{
      background: lightblue;
      transition: all 300ms linear;
      width: 0;
      height: 0;
    }
  </style>
</head>
<body>
  <div class="block"></div>
  <button onclick="toBig()">大</button>
  <button onclick="toSmall()">小</button>
  <script>
    function toBig(){
      console.log('tobig')
      const block = document.querySelector('.block');
      block.style.width='200px'
      block.style.height='200px'
    }
    function toSmall(){
      console.log('tosmall')
      const block = document.querySelector('.block');
      block.style.width='0'
      block.style.height='0'
    }
  </script>
</body>
</html>
阅读 2.1k
1 个回答

transform-origin: center

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题