简体   繁体   English

防止窗口向上滚动而在自定义模式打开时不会闪烁

[英]Prevent window from scroll up without flicker on custom modal open

So I had an issue where I had a custom modal window open up on click and move to the top of the viewport, problem was, when you scrolled up you could see the page beneath, so I needed to limit the scrolling so you couldn't scroll up once the modal was open. 因此,我遇到一个问题,即单击并打开一个自定义模态窗口并移至视口顶部,问题是,当您向上滚动时可以看到下面的页面,因此我需要限制滚动以便您不能模态打开后,向上滚动。 Here's the jquery I used. 这是我使用的jQuery。

var top = window.pageYOffset;

$(document).on("scroll", function(e){
    var windowScrollTop = $(window).scrollTop();
    if(windowScrollTop < top){
        console.log("not allowed");
        $(document).scrollTop(top);
    }
    else{
        console.log("allowed");
    }
});

This works fine for preventing scrolling, but it creates a flicker at the top of the page when you try to scroll up (only been tested on mac). 这可以很好地防止滚动,但是当您尝试向上滚动时,它会在页面顶部产生闪烁(仅在Mac上经过测试)。 It's not so bad on firefox for mac, just a little strip of white, but on chrome for mac, you can see a good bit of the page underneath flicker when you try to scroll up. 对于Mac来说,在firefox上还算不错,只是一点点白色,但是对于Mac,在chrome上,当您向上滚动时,您会在闪烁下看到很多页面。 Is there any way to get around this and make the scroll prevention cleaner? 有什么方法可以解决这个问题并使防涡卷器更清洁?

EDIT: for an example, I'm trying to make the scrolling behave similarly to this page. 编辑:例如,我试图使滚动行为类似于此页面。 Click on one of the case studies to open it and you will notice it opens to the top of viewport, not the document, and scrolling above the top of the modal is not possible. 单击一个案例研究将其打开,您会发现它打开到视口的顶部,而不是文档,并且无法在模式顶部上方滚动。 https://infinum.co/client-work https://infinum.co/client-work

EDIT EDIT: The only css I have applied on this div is overflow:hidden and position:absolute. 编辑:我在此div上应用的唯一CSS是:溢出:隐藏和位置:绝对。 As for html, it's just a div with content being loaded in dynamically, so it changes, but the overall structure is the same for every modal. 对于html来说,它只是一个div,其内容是动态加载的,因此它会发生变化,但是每个模式的总体结构都是相同的。

EDIT EDIT EDIT: I've added a codepen, but it doesn't display the behavior that I am describing with chrome. 编辑编辑:我已经添加了一个代码笔,但是它不显示我用chrome描述的行为。 On codepen it appears to work fine. 在Codepen上似乎可以正常工作。 http://codepen.io/kathryncrawford/full/ZWYLva/ http://codepen.io/kathryncrawford/full/ZWYLva/

To be super clear, this is only an issue on chrome for mac, for some weird reason. 要非常清楚,出于某些奇怪的原因,这仅是Mac版chrome上的问题。 这是显示行为的gif

One of our devs found a solution (after hours of trying, ugh). 我们的一位开发人员找到了解决方案(经过数小时的尝试,呃)。 We set the modal to position: absolute and overflow: scroll, then when we opened it set the body to overflow: hidden. 我们将模态设置为position:absolute,overflow:scroll,然后在打开它时将主体设置为overflow:hidden。 That prevented scrolling on the body and allows us to scroll the modal. 这样可以防止在主体上滚动,并允许我们滚动模态。 We also had to change the height of the modal to animate to 100% rather than the height of the body to get this to work. 为了使它起作用,我们还必须将模式的高度更改为100%的动画,而不是身体的高度。

Here's an updated codepen in case you're curious 这是更新的codepen,以防您好奇

http://codepen.io/kathryncrawford/full/ZWYLva/ http://codepen.io/kathryncrawford/full/ZWYLva/

CSS changed CSS已更改

.s-case-expand {
opacity: 0;
display: none;
position: absolute;
overflow: scroll;
}

JS changed JS已更改

    expandTl.set(caseExpand, {zIndex: 100, backgroundColor: color})
                .set("body", {overflow: "hidden"})
                .to(caseExpand, 0.5, {opacity: 1})
                .to(caseExpand, 1, {width: width, height: "100%", top: top, left: 0,});

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

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