简体   繁体   English

打开菜单时“溢出-y:隐藏”未应用于正文

[英]"overflow-y:hidden" not being applied to body when menu opened

I have a fullscreen menu, when the user clicks the menu button to open the menu "#menu-button", I need to disable scrolling on the body.我有一个全屏菜单,当用户单击菜单按钮打开菜单“#menu-button”时,我需要禁用主体上的滚动。 I'm pretty certain the below code should be working by adding the class ".no-scroll" to the body when the menu button is clicked, but it's not.我很确定下面的代码应该通过在单击菜单按钮时将类“.no-scroll”添加到正文来工作,但事实并非如此。 Can anyone indicate what the issue might be?谁能指出可能是什么问题?

var menuAnimation = gsap.timeline({paused:true});
var menuAnimationBack = gsap.timeline({paused:true, reversed: true});
var navMain = document.getElementById("nav-main");
var menuButton = document.getElementById("menu-button");
var toggle = true;

gsap.set('.link',{y:30});

menuAnimation
        .to(navMain, {duration:.8, width: '100%', clipPath: "polygon(0 0, 100% 0, 100% 100%, 0 100%)", ease: "power2.inOut", x:0, y:0, z:0})
.to('.link',{duration:.5,autoAlpha:1,y:0,stagger:.2,ease: "power4.out"});


menuAnimationBack
.to('.link',{duration:.5,autoAlpha:0,y:30,stagger:.2,ease: "power4.out"})
.to(navMain, {duration:0.55,width: 0, clipPath: "polygon(0 0, -100% 0, 100% 100%, 0 100%)", ease: "power4.in", x:0, y:0, z:0});

menuButton.onclick = function() {
  toggle = !toggle;
  toggle === false ? menuAnimation.play(0) : menuAnimationBack.play(0);
};

menuButton.addEventListener('click', () => {   document.body.classList.toggle('no-scroll') });
.no-scroll{
overflow-y:hidden
}

add your no-scroll class on html tag, hope it will work for you在 html 标签上添加你的 no-scroll 类,希望它对你有用

menuButton.addEventListener('click', () => {
    document.querySelector("html").classList.toggle("no-scroll")
});

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

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