Heim  >  Fragen und Antworten  >  Hauptteil

Warum ArrowBackground sofort konvertiert und nicht 0,5 Sekunden dauert

//* Define variables and html elements

const sidebar = document.querySelector('.sidebar');
const arrow = document.querySelector('.arrow');

//* Add event listeners

sidebar.addEventListener('mouseout', () => {
  arrow.style.transitionDelay = '0s';
  sidebar.style.transitionDelay = '0.05s';
})

sidebar.addEventListener('mouseover', () => {
  arrow.style.transitionDelay = '0.05s';
  sidebar.style.transitionDelay = '0s';
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  width: 100vw;
  display: flex;
}

.sidebar {
  position: fixed;
  height: 100vh;
  background-color: black;
  width: 80px;
  transition: all 0.5s ease;
  z-index: 0;
}

.arrow {
  position: relative;
  left: 7.5px;
  right: 7.5px;
  top: 7.5px;
  width: 65px;
  height: 65px;
  fill: rgb(115, 151, 162);
  transition: all 0.5s ease;
  z-index: 2;
}

.arrowBackground {
  position: absolute;
  top: 0;
  width: 82px;
  height: 80px;
  background-color: rgb(30, 30, 30);
  z-index: 1;
  border: 2px solid rgb(50, 50, 50);
  transition: all 0.5s ease;
}

.sidebar:hover {
  width: 240px;
}

body:has(.sidebar:hover) .arrow {
  transform: rotate(180deg);
  left: 167.5px;
}

body:has(.sidebar:hover) .arrowBackground {
  left: 160px;
}
<section class="sidebar">
  <div class="icons">
    <div>
      <svg class="icon arrow" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M9.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.2 288 416 288c17.7 0 32-14.3 32-32s-14.3-32-32-32l-306.7 0L214.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg>
      <div class="arrowBackground"></div>
    </div>
  </div>
</section>

Wenn ich mit der Maus über die Seitenleiste fahre, dauert die Konvertierung des Pfeils und der Seitenleiste 0,5 Sekunden, aber die Pfeil-Hintergrundklasse macht das sofort. Ich weiß nicht warum. Ich habe erwartet, dass die Konvertierung 0,5 Sekunden dauert, aber ich weiß nicht, wie das geht Repariere es

Ich habe versucht, Code neu anzuordnen, aber nichts hat funktioniert. Ich habe versucht, eine Übergangsverzögerung in JavaScript festzulegen, aber es funktioniert immer noch nicht

P粉473363527P粉473363527168 Tage vor3502

Antworte allen(1)Ich werde antworten

  • P粉226642568

    P粉2266425682024-04-06 00:16:09

    left: 0; 添加到 arrowBackground,否则它不知道从什么位置转换

    //* Define variables and html elements
    
    const sidebar = document.querySelector('.sidebar');
    const arrow = document.querySelector('.arrow');
    
    //* Add event listeners
    
    sidebar.addEventListener('mouseout', () => {
      arrow.style.transitionDelay = '0s';
      sidebar.style.transitionDelay = '0.05s';
    })
    
    sidebar.addEventListener('mouseover', () => {
      arrow.style.transitionDelay = '0.05s';
      sidebar.style.transitionDelay = '0s';
    })
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      height: 100vh;
      width: 100vw;
      display: flex;
    }
    
    .sidebar {
      position: fixed;
      height: 100vh;
      background-color: black;
      width: 80px;
      transition: all 0.5s ease;
      z-index: 0;
    }
    
    .arrow {
      position: relative;
      left: 7.5px;
      right: 7.5px;
      top: 7.5px;
      width: 65px;
      height: 65px;
      fill: rgb(115, 151, 162);
      transition: all 0.5s ease;
      z-index: 2;
    }
    
    .arrowBackground {
      position: absolute;
      left: 0;
      top: 0;
      width: 82px;
      height: 80px;
      background-color: rgb(30, 30, 30);
      z-index: 1;
      border: 2px solid rgb(50, 50, 50);
      transition: all 0.5s ease;
    }
    
    .sidebar:hover {
      width: 240px;
    }
    
    body:has(.sidebar:hover) .arrow {
      transform: rotate(180deg);
      left: 167.5px;
    }
    
    body:has(.sidebar:hover) .arrowBackground {
      left: 160px;
    }

    Antwort
    0
  • StornierenAntwort