简体   繁体   English

在整个页面上垂直滑动

[英]Vertically slideown over whole page

i'm working on a project where i need a semi-transparent div to slideover the entire page at a certain point. 我正在做一个需要半透明div在某个点上滑过整个页面的项目。 i have a version working currently, but it's not as smooth as i'd like it to be. 我有一个目前正在运行的版本,但它不如我希望的那样平滑。

http://jsfiddle.net/27e310e8/ http://jsfiddle.net/27e310e8/

jQuery from above example: 上面例子中的jQuery:

var windowWidth = $(window).innerWidth();
var windowHeight = $(window).innerHeight();

function blackOut() {
    $('#fail-screen').css({
        "width": windowWidth + "px",
            "height": windowHeight + "px"
    });

    $('#fail-screen').delay(1000).animate({
        top: '0px'
    }, 1000, 'linear');
}



$(document).ready(function () {
    blackOut(windowWidth, windowHeight);
});

as you can see i'm getting the innerWidth and height to set the "fail-screen" div, as setting it to 100% wasn't working well. 如您所见,我正在将innerWidth和height设置为“故障屏幕” div,因为将其设置为100%效果不佳。 i'm using jQuery to animate the top position of the "fail-screen" div. 我正在使用jQuery设置“故障屏幕” div的顶部位置的动画。

again, i'm just looking to refactor this code and improve overall presentation and performance. 再次,我只是希望重构此代码并改善总体表示和性能。 i'm open to using animation/physic libraries if anyone knows of any that would be good to use here. 如果有人知道在这里可以使用任何动画/物理库,我愿意使用它。

appreciate any suggestions. 感谢任何建议。

As @Jason has mentioned, I strongly recommend using CSS transforms instead of fiddling with the offsets. 正如@Jason所提到的,我强烈建议您使用CSS转换,而不是摆弄偏移量。 That is being not only are CSS transforms offloaded to the GPU (intelligently determined by the browser as of when needed, but you can also force it), but it allows for subpixel rendering. 这不仅是将CSS转换卸载到GPU(由浏览器在需要时智能地确定,但您也可以强制使用),但它允许子像素渲染。 Paul Irish published a rather good write-up on this topic back in 2012. 保罗·爱尔兰(Paul Irish)早在2012年就发表了有关该主题的出色文章

Also, your code is slightly problematic in the sense that it fails to handle viewport resize events. 同样,您的代码在处理视口调整大小事件方面也存在一些问题。 In fact, a more straightforward solution would simply be using position: fixed , and then playing around with CSS transform to bring the element into view after a delay. 实际上,一个更直接的解决方案将只是使用position: fixed ,然后使用CSS转换在延迟后将元素显示出来。

See updated fiddle here: http://jsfiddle.net/teddyrised/27e310e8/2/ 在这里查看更新的小提琴: http : //jsfiddle.net/teddyrised/27e310e8/2/

For the JS, we simply use the .css() method. 对于JS,我们仅使用.css()方法。 The delay, animation duration and even the timing function can be easily done via CSS. 延迟,动画持续时间甚至计时功能都可以通过CSS轻松完成。

The new JS is rather straightforward: we set the transform of #fail-screen so that we move it back to its original vertical position. 新的JS非常简单:我们设置#fail-screen的转换,以便将其移回到原始垂直位置。 jQuery automagically prefixes the transform property ;) jQuery 自动transform属性添加前缀;)

$(document).ready(function () {
    $('#fail-screen').css({
        'transform': 'translateY(0)'
    });
});

For the CSS, we initially set a vertical translation ( translateY ) of -100%, which means we push the element upwards by its own height. 对于CSS,我们最初将垂直平移( translateY )设置为-100%,这意味着我们将元素向上推其自身的高度。 Using fixed positioning and declaring all four offsets as 0 , we can force the element to fill the viewport without any advanced hack of listening to window resize events via JS. 使用固定的位置并将所有四个偏移量声明为0 ,我们可以强制元素填充视口,而无需通过JS来监听窗口调整大小事件的高级技巧。 Remember that you will have to add vendor prefixes to the transform property to maximize cross-browser compatibility. 请记住,您必须将供应商前缀添加到transform属性,以最大程度地实现跨浏览器的兼容性。

CSS can also handle transition delay, duration and even the timing function, ie transition: [property] [duration] [timing-function] [delay]; CSS还可以处理过渡延迟,持续时间甚至计时功能,即transition: [property] [duration] [timing-function] [delay]; Since in your jQuery code you have set both duration and delay to be 500ms, it should be transition: all 0.5s linear 0.5s . 由于在jQuery代码中将持续时间和延迟都设置为500ms,因此应该transition: all 0.5s linear 0.5s However, the linear timing function doesn't look good — perhaps using ease-in-out would be better, or even a custom cubic-bezier curve , perhaps? 但是,线性时序函数看起来并不好-也许使用ease-in-out会更好,或者甚至可以使用自定义三次贝塞尔曲线

Also, I recommend moving the opacity to the background-color value, simply because if you set an opacity on the element itself, all child nodes will be rendered at 0.6 opacity, too... it might be something that you do not want to achieve. 另外,我建议将不透明度移到background-color值,这仅仅是因为如果您在元素本身上设置了不透明度,那么所有子节点也将被渲染为0.6不透明度...这可能是您不想要的实现。

#fail-screen{
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0,0,0,.6);  /* Moved opacity to background-color */
    position: fixed;                   /* Use fixed positioning */
    z-index: 303;

    /* Use CSS transform to move the element up by its own height */
    -webkit-transform: translateY(-100%);
    -ms-transform: translateY(-100%);
    -o-transform: translateY(-100%);
    transform: translateY(-100%);

    /* CSS transition */
    transition: all .5s ease-in-out .5s;
}

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

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