GSAP/CSS 导航栏动画大小调整

如何解决GSAP/CSS 导航栏动画大小调整

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

有没有办法在getVpdr函数中只动态计算#wrapper2的大小,让Prelude> replicate 0 ' ' "" Prelude> replicate 1 ' ' " " Prelude> replicate 2 ' ' " " Prelude> replicate 3 ' ' " " 值只填充这个div的尺寸?

scale: getVpdr()

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); // Width
  const vpd = Math.sqrt(vph + vpw); // Diagonal
  return (vpd * 2) / circleWidth; // Circle radius
};

const openNavbar = () => {
  const openTimeline = new TimelineMax();
  openTimeline.to(".navbar",{ 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",opacity: 1,delay: 0.5 },-0.1);
  closeTimeline.to("#bg-circle",1,{scale: 0,ease: Expo.easeInOut,delay: -0.5});
  closeTimeline.to(".navbar",{ 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",{ 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;
}

解决方法

我认为这可能是您要找的:

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",{
    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",opacity: 1,delay: 0.5
  },-0.1);
  closeTimeline.to("#bg-circle",1,{
    scale: 0,ease: Expo.easeInOut,delay: -0.5
  });
  closeTimeline.to(".navbar",{
    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",{
      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,然后我将 position:fixedposition:absolute 上的 navbar 替换为 bg-circle {1}}。
最后在 JS 中我改变了 vpw 所以它只需要 wrapper2 的宽度。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-