简体   繁体   English

使用 setTimeout() function 关闭溢出-y

[英]turning overflow-y off with a setTimeout() function

i'm developing a project where the scrollbar needs to be hidden and unable to function for the first 4 seconds on the first render, when the site open (there's a gsap animation).我正在开发一个项目,其中需要隐藏滚动条并且在第一次渲染的前 4 秒内无法 function,当网站打开时(有一个 gsap 动画)。 so i set the * {overflow-y: hidden} on css and would like to make it auto again after those seconds with a setTimeout() function, how could i proceed?所以我在 css 上设置了 * {overflow-y: hidden} 并想在几秒钟后用 setTimeout() function 让它再次自动,我该如何继续?

document.querySelector("html").style.overflowY = "hidden";

after 4 secs: 4 秒后:

document.querySelector("html").style.overflowY = "auto";

If you don't want to do it this way you could also do this ( Codepen ):如果你不想这样做,你也可以这样做( Codepen ):

.class1 {
   overflow-y: hidden;
}

.class2 {
  overflow-y: auto;
}

after 4 seconds just replace class1 with class2 on the node 4 秒后,只需在节点上将 class1 替换为 class2

Third option I give you would be declaring a CSS variable and changing it with js:我给你的第三个选项是声明一个 CSS 变量并用 js 更改它:

:root {
  --scrollbar-var: hidden;
}

html {
 overflow-y: var(--scrollbar-var);
}


let root = document.documentElement;
root.style.setProperty('--scrollbar-var', "auto");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM