LoginSignup
0
0

画像で横幅が-90度回転した場合に縦に合わせる

Last updated at Posted at 2024-05-18
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Rotate and Center with Proportional Div2</title>
  </head>
  <body>
    <div class="p-body">
      <img class="div1" src="./img/blue-tit-8024809_1280.png" alt="" width="1280" height="640" />
      <img class="div2" src="./img/img2.webp" alt="">
    </div>

    <style>
      body {
        margin: 0;
      }
      .p-body {
        position: relative;
        height: 100vh;
        display: flex;
        justify-content: center;
      }
      .div1 {
        height: auto;
        width: calc(100vh - 72px);
        object-fit: contain;
        transform: translateY(36px) rotate(-90deg);
      }
      .div2 {
        position: absolute;
        top: calc(50% + 36px);
        left: 50%;
        width: 33vw;
        transform: translate(-50%, -50%);
        background-color: rgb(10, 42, 42);
      }
    </style>
    <script>
      document.addEventListener("DOMContentLoaded", function() {
        const div1 = document.querySelector('.div1');
        const div2 = document.querySelector('.div2');

        function updateDiv2Width() {
          const div1Width = div1.naturalWidth;
          const div1Height = div1.naturalHeight;
          const aspectRatio = div1Height / div1Width;

          const div1RotatedHeight = window.innerHeight - 72
          const div1RotatedWidth = div1RotatedHeight * aspectRatio;

          div2.style.width = `${div1RotatedWidth}px`;
        }

        div1.onload = updateDiv2Width;

        // If the image is already loaded (from cache), trigger the onload event manually
        if (div1.complete) {
          updateDiv2Width();
        }

        window.addEventListener('resize', updateDiv2Width);
      });
    </script>

  </body>
</html>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0