html - GSAP/CSS 导航栏动画大小
问题描述
我正在尝试更改此原始 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>
通过将navbar
&bg-circle
放入wrapper2
.
在 CSS 中,我确保wrapper2
有 a position:relative
,然后在&上overflow:hidden
替换position:fixed
为。
最后在 JS 中我改变了,所以它只需要.position:absolute
navbar
bg-circle
vpw
wrapper2
推荐阅读
- google-analytics - 如何跟踪潜在客户产生的产品
- php - JSON 数据未从 Ajax 调用正确返回
- json - 来自后端的无效 JSON?角 7
- c# - C# ASP.Net Core 应用程序出现奇怪的 TLS 客户端证书握手问题
- java - 如何将 OnClickListener 添加到 PagerAdapter
- python - 使用 Flask 时 Python 中持久数据库连接的最佳实践
- django - 如何使用 django-ldapdb 从 LDAP 组中添加/删除成员?
- c# - 解析整数时出现溢出异常
- sorting - 如何使用双向链表创建排序列表?
- react-native - 下面的问题应该怎么解决?