GSAP/CSS 导航栏动画尺寸

GSAP/CSS navbar animation sizing

我正在尝试更改 this 原始 GSAP 代码,使蓝色导航栏仅填充绿色 #wrapper2 而不是整个屏幕。

有没有办法在getVpdr函数中只动态计算#wrapper2大小,让scale: getVpdr()值只填充这个div?

const getVpdr = () => {
  const vph = Math.pow(html.offsetHeight, 2); // Height
  const vpw = Math.pow(html.offsetWidth, 2); // Width
  const vpd = Math.sqrt(vph + vpw); // Diagonal
  return (vpd * 2) / circleWidth; // Circle radius
};

console.clear();

const html = document.documentElement;
const toggle = document.getElementById("toggle");
const circle = document.getElementById("bg-circle");
const circleWidth = circle.clientWidth;

// Math calcul to get Height, Width, Diagonal and Circle Radius

const getVpdr = () => {
  const vph = Math.pow(html.offsetHeight, 2); // Height
  const vpw = Math.pow(html.offsetWidth, 2); // Width
  const vpd = Math.sqrt(vph + vpw); // Diagonal
  return (vpd * 2) / circleWidth; // Circle radius
};

const openNavbar = () => {
  const openTimeline = new TimelineMax();
  openTimeline.to(".navbar", 0, { display: "flex" });
  openTimeline.to("#bg-circle", 1.5, {scale: getVpdr(),ease: Expo.easeInOut});
  openTimeline.staggerFromTo(".navbar ul li",0.5,{ y: 25, opacity: 0 },{ y: 0, opacity: 1 },0.1,1);
};

const closeNavbar = () => {
  const closeTimeline = new TimelineMax();
  closeTimeline.staggerFromTo(".navbar ul li",0.5,{ y: 0, opacity: 1, delay: 0.5 },{ y: 25, opacity: 0 },-0.1);
  closeTimeline.to("#bg-circle", 1, {scale: 0,ease: Expo.easeInOut,delay: -0.5});
  closeTimeline.to(".navbar", 0, { display: "none" });
};

let isOpen = false;

toggle.onclick = function () {
  if (isOpen) {
    this.classList.remove("active");
    closeNavbar();
  } else {
    this.classList.add("active");
    openNavbar();
  }
  isOpen = !isOpen;
};

// On windows resize, recalcule circle radius and update

window.onresize = () => {
  if (isOpen) {
    gsap.to("#bg-circle", 1, { scale: getVpdr(), ease: Expo.easeInOut });
  }
};
body {
  align-items: center;
  display: flex;
  height: 100%;
  justify-content: center;
  margin: 0;
  overflow: hidden;
  width: 100%;
  font-family: sans-serif;
  background: white;
}
body #wrapper1, body #wrapper2 {
  width: 50vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
body #wrapper1 {
  background-color: red;
}
body #wrapper2 {
  background-color: green;
}

.navbar-toggle {
  -webkit-tap-highlight-color: transparent;
  transition: transform 400ms;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: pointer;
  position: fixed;
  z-index: 20;
  top: 1rem;
  right: 1rem;
  background: transparent;
  border: none;
  outline: none;
  padding: 0;
}
.navbar-toggle .line {
  fill: none;
  transition-delay: 400ms, 0;
  transition-property: stroke, stroke-dasharray, stroke-dashoffset;
  transition-timing-function: ease;
  transition-duration: 400ms;
  stroke: #000;
  stroke-width: 5.5;
  stroke-linecap: round;
}
.navbar-toggle .line.top {
  stroke-dasharray: 40 139;
}
.navbar-toggle .line.bottom {
  stroke-dasharray: 20 180;
  stroke-dashoffset: -20px;
}
.navbar-toggle.active {
  transform: rotate(45deg);
}
.navbar-toggle.active .line {
  stroke: #FFFFFF;
}
.navbar-toggle.active .line.top {
  stroke-dashoffset: -98px;
}
.navbar-toggle.active .line.bottom {
  stroke-dashoffset: -138px;
}
.navbar-toggle:not(.active):hover .line.bottom {
  stroke-dasharray: 40 180;
  stroke-dashoffset: 0px;
}

.navbar {
  position: fixed;
  width: 100%;
  height: 100%;
  z-index: 2;
  display: none;
  align-items: center;
  justify-content: center;
}
.navbar ul {
  width: 100%;
  max-width: 400px;
  list-style: none;
  padding: 0;
  margin: 0;
}
.navbar ul li {
  opacity: 0;
}
.navbar ul li a {
  color: white;
  text-decoration: none;
  font-size: 25px;
  display: block;
  text-align: left;
  padding: 20px 0;
  font-weight: bold;
  letter-spacing: 2px;
  text-transform: uppercase;
  cursor: pointer;
  transition: all ease 500ms;
  position: relative;
}
.navbar ul li a:before {
  content: attr(data-text);
  position: absolute;
  left: 0;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 70px;
  opacity: 0;
  transition: opacity ease 500ms;
}
.navbar ul li a:hover {
  letter-spacing: 3px;
}
.navbar ul li a:hover:before {
  opacity: 0.2;
}

#bg-circle {
  transform: scale(0);
  width: 80px;
  height: 80px;
  background: #4E6EE2;
  position: fixed;
  top: 1rem;
  right: 1rem;
  border-radius: 50%;
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.3/gsap.min.js"></script>
<div id="wrapper1">
  
  <h1>Menu NOT cover the this red div</h1>
  
  <button class="navbar-toggle" id="toggle" type="button">
<svg viewBox="0 0 100 100" width="80">
  <path class="line top" d="m 30,33 h 40 c 0,0 9.044436,-0.654587 9.044436,-8.508902 0,-7.854315 -8.024349,-11.958003 -14.89975,-10.85914 -6.875401,1.098863 -13.637059,4.171617 -13.637059,16.368042 v 40" />
  <path class="line middle"d="m 30,50 h 40" />
  <path class="line bottom" d="m 30,67 h 40 c 12.796276,0 15.357889,-11.717785 15.357889,-26.851538 0,-15.133752 -4.786586,-27.274118 -16.667516,-27.274118 -11.88093,0 -18.499247,6.994427 -18.435284,17.125656 l 0.252538,40" />
</svg>
</button>

<div class="navbar">
    <ul>
      <li><a data-text="1" href="#">Home</a></li>
      <li><a data-text="2" href="#">Our Team</a></li>
      <li><a data-text="3"href="#">Projects</a></li>
      <li><a data-text="4" href="#">Contact</a></li>
    </ul>
</div>
  <div id="bg-circle"></div>
</div>

<div id="wrapper2">
  <h1>Menu only cover the this green div</h1>
</div>

我想这可能是您要找的:

console.clear();

const html = document.documentElement;
const toggle = document.getElementById("toggle");
const circle = document.getElementById("bg-circle");
const circleWidth = circle.clientWidth;

// Math calcul to get Height, Width, Diagonal and Circle Radius

const getVpdr = () => {
  const vph = Math.pow(html.offsetHeight, 2); // Height
  const vpw = Math.pow(document.getElementById("wrapper2").offsetWidth, 2); // Width
  const vpd = Math.sqrt(vph + vpw); // Diagonal
  return (vpd * 2) / circleWidth; // Circle radius
};

const openNavbar = () => {
  const openTimeline = new TimelineMax();
  openTimeline.to(".navbar", 0, {
    display: "flex"
  });
  openTimeline.to("#bg-circle", 1.5, {
    scale: getVpdr(),
    ease: Expo.easeInOut
  });
  openTimeline.staggerFromTo(".navbar ul li", 0.5, {
    y: 25,
    opacity: 0
  }, {
    y: 0,
    opacity: 1
  }, 0.1, 1);
};

const closeNavbar = () => {
  const closeTimeline = new TimelineMax();
  closeTimeline.staggerFromTo(".navbar ul li", 0.5, {
    y: 0,
    opacity: 1,
    delay: 0.5
  }, {
    y: 25,
    opacity: 0
  }, -0.1);
  closeTimeline.to("#bg-circle", 1, {
    scale: 0,
    ease: Expo.easeInOut,
    delay: -0.5
  });
  closeTimeline.to(".navbar", 0, {
    display: "none"
  });
};

let isOpen = false;

toggle.onclick = function() {
  if (isOpen) {
    this.classList.remove("active");
    closeNavbar();
  } else {
    this.classList.add("active");
    openNavbar();
  }
  isOpen = !isOpen;
};

// On windows resize, recalcule circle radius and update

window.onresize = () => {
  if (isOpen) {
    gsap.to("#bg-circle", 1, {
      scale: getVpdr(),
      ease: Expo.easeInOut
    });
  }
};
body {
  align-items: center;
  display: flex;
  height: 100%;
  justify-content: center;
  margin: 0;
  overflow: hidden;
  width: 100%;
  font-family: sans-serif;
  background: white;
}

body #wrapper1,
body #wrapper2 {
  width: 50vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

body #wrapper1 {
  background-color: red;
}

body #wrapper2 {
  position: relative;
  background-color: green;
  overflow: hidden;
}

.navbar-toggle {
  -webkit-tap-highlight-color: transparent;
  transition: transform 400ms;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: pointer;
  position: fixed;
  z-index: 20;
  top: 1rem;
  right: 1rem;
  background: transparent;
  border: none;
  outline: none;
  padding: 0;
}

.navbar-toggle .line {
  fill: none;
  transition-delay: 400ms, 0;
  transition-property: stroke, stroke-dasharray, stroke-dashoffset;
  transition-timing-function: ease;
  transition-duration: 400ms;
  stroke: #000;
  stroke-width: 5.5;
  stroke-linecap: round;
}

.navbar-toggle .line.top {
  stroke-dasharray: 40 139;
}

.navbar-toggle .line.bottom {
  stroke-dasharray: 20 180;
  stroke-dashoffset: -20px;
}

.navbar-toggle.active {
  transform: rotate(45deg);
}

.navbar-toggle.active .line {
  stroke: #FFFFFF;
}

.navbar-toggle.active .line.top {
  stroke-dashoffset: -98px;
}

.navbar-toggle.active .line.bottom {
  stroke-dashoffset: -138px;
}

.navbar-toggle:not(.active):hover .line.bottom {
  stroke-dasharray: 40 180;
  stroke-dashoffset: 0px;
}

.navbar {
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.navbar ul {
  width: 100%;
  max-width: 400px;
  list-style: none;
  padding: 0;
  margin: 0;
}

.navbar ul li {
  opacity: 0;
}

.navbar ul li a {
  color: white;
  text-decoration: none;
  font-size: 25px;
  display: block;
  text-align: left;
  padding: 20px 0;
  font-weight: bold;
  letter-spacing: 2px;
  text-transform: uppercase;
  cursor: pointer;
  transition: all ease 500ms;
  position: relative;
}

.navbar ul li a:before {
  content: attr(data-text);
  position: absolute;
  left: 0;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 70px;
  opacity: 0;
  transition: opacity ease 500ms;
}

.navbar ul li a:hover {
  letter-spacing: 3px;
}

.navbar ul li a:hover:before {
  opacity: 0.2;
}

#bg-circle {
  transform: scale(0);
  width: 80px;
  height: 80px;
  background: #4E6EE2;
  position: absolute;
  top: 1rem;
  right: 1rem;
  border-radius: 50%;
  z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.3.3/gsap.min.js"></script>
<div id="wrapper1">

  <h1>Menu NOT cover the this red div</h1>

  <button class="navbar-toggle" id="toggle" type="button">
<svg viewBox="0 0 100 100" width="80">
  <path class="line top" d="m 30,33 h 40 c 0,0 9.044436,-0.654587 9.044436,-8.508902 0,-7.854315 -8.024349,-11.958003 -14.89975,-10.85914 -6.875401,1.098863 -13.637059,4.171617 -13.637059,16.368042 v 40" />
  <path class="line middle"d="m 30,50 h 40" />
  <path class="line bottom" d="m 30,67 h 40 c 12.796276,0 15.357889,-11.717785 15.357889,-26.851538 0,-15.133752 -4.786586,-27.274118 -16.667516,-27.274118 -11.88093,0 -18.499247,6.994427 -18.435284,17.125656 l 0.252538,40" />
</svg>
</button>
</div>

<div id="wrapper2">
  <h1>Menu only cover the this green div</h1>
  <div class="navbar">
    <ul>
      <li><a data-text="1" href="#">Home</a></li>
      <li><a data-text="2" href="#">Our Team</a></li>
      <li><a data-text="3" href="#">Projects</a></li>
      <li><a data-text="4" href="#">Contact</a></li>
    </ul>
  </div>
  <div id="bg-circle"></div>
</div>

通过将 navbarbg-circle 放入 wrapper2 来稍微调整 HTML 结构。
在 CSS 中,我确保 wrapper2 有一个 position:relativeoverflow:hidden 然后我在两个 [=13] 上用 position:absolute 替换了 position:fixed =] & bg-circle
最后在 JS 中我更改了 vpw 所以它只需要 wrapper2.

的宽度