Nov 19, 2022

Solution 1 :

I think this might be what you’re looking for:

``````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 {
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;
}

.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;
}

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

.navbar ul li {
opacity: 0;
}

.navbar ul li a {
color: white;
text-decoration: none;
font-size: 25px;
display: block;
text-align: left;
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;
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>``````

Tweaked the HTML Structure a tiny bit by placing the `navbar` & `bg-circle` into `wrapper2`.
In the CSS I made sure that the `wrapper2` has a `position:relative` and `overflow:hidden` I then replaced `position:fixed` with `position:absolute` on both the `navbar` & `bg-circle`.
And finally in the JS I changed `vpw` so it only takes the width of `wrapper2`.

Problem :

I am trying to alter this original GSAP code to have the blue navbar only to fill the green #wrapper2 not the entire screen.

Is there a way to dynamically calculate only the #wrapper2 size in the getVpdr function, so that the `scale: getVpdr()` value will fill the dimensions of only this 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 {
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;
}
.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;
margin: 0;
}
.navbar ul li {
opacity: 0;
}
.navbar ul li a {
color: white;
text-decoration: none;
font-size: 25px;
display: block;
text-align: left;
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;
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>``````

Comment posted by upgrading to GSAP 3 syntax

We at GreenSock highly recommend

Comment posted by Fjott

Thanks I’ll look into that!

Comment posted by Fjott

Perfect! And thank you for this precisely explanation as well.