【问题标题】:Image with parallax effect on full screen page without scrollbars在没有滚动条的全屏页面上具有视差效果的图像
【发布时间】:2017-06-30 19:02:20
【问题描述】:

我的登录页面需要为 100vh / 100vw 且不滚动。背景是具有视差效果的图像,因此当鼠标移动时,图像也会随之移动。

我正在努力弄清楚如何让它发挥作用。对于视差图像,我目前有:

min-height: 100vh;
min-width: 100vw;

页面是 100vh/100vw,我需要它。问题是当图像移动时(鼠标移动),它不再覆盖页面。我需要使图像更大,以便它始终覆盖整个页面,但我需要保留一个没有滚动条的全屏页面。

这是问题的jsFiddle Demo。

【问题讨论】:

  • @ChavaG 你是对的。我想我误读了,并认为链接的网站是他想要实现的目标的一个例子。将删除我的评论!

标签: jquery html css parallax parallax.js


【解决方案1】:

您可以将background-size: 200% 或300% 设置为任意大小。

【讨论】:

  • 这将缩放背景图像,但元素本身的大小仍然相同。它不会达到@James Bimson 想要的效果。
【解决方案2】:

以下是如何在没有滚动条的情况下保持全页的视差滚动效果:

首先,将主体(或包含视差图像的元素)的高度设置为100vh 和overflow: hidden;。这样,您可以保持 100vw/100vh 的大小,同时仍然拥有更大的元素。

overflow:  hidden;
height: 100vh;

现在设置视差图像(.layer.layer-1) 以占用所需的空间量。此外,将上边距和左边距设置为负数,将内边距设置为正数,以使该元素的起点位于视口之外。

min-height: 200vh;
min-width: 200vw;
margin-top: -100px;
margin-left: -300px;
padding-top: 100px;

$('#scene').parallax();
body {
  height: 100vh;
  overflow: hidden;
  background-attachment: scroll !important;
  background-color: transparent;
  margin: 0;
}
#scene {
    transform: translate3d(0px, 0px, 0px);
    transform-style: preserve-3d;
    backface-visibility: hidden;
    position: relative;
    z-index: -1;
    display: block;
    margin-bottom: 0px;
    padding-left: 0px;
    list-style: none;
    background-attachment: scroll;
}

.layer.layer-1 {
    transform: translate3d(-87.1px, -63.9px, 0px);
    transform-style: preserve-3d;
    backface-visibility: hidden;
    min-height: 200vh;
    min-width: 200vw;
    margin-top: -100px;
    margin-left: -300px;
    padding-top: 100px;
    background-image: url(http://uploads.webflow.com/5900af0a5f7f83692b682e4d/59468089c7312e4d8a190fa3_521477.jpg);
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

.layer.layer-2 {
    position: absolute;
    display: block;
    left: 0px;
    top: 0px;
    transform: translate3d(0px, 0px, 0px);
    transform-style: preserve-3d;
    backface-visibility: hidden;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    background-image: url(http://uploads.webflow.com/5900af0a5f7f83692b682e4d/5946831616c1f5436ec09e17_slice1.png);
    background-position: 50% 100%;
    background-size: cover;
    background-repeat: no-repeat;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://dl.dropboxusercontent.com/s/fjcsy7p5wuw8iij/jquery.parallax.js"></script>
<script src="https://daks2k3a4ib2z.cloudfront.net/5900af0a5f7f83692b682e4d/js/webflow.11254b67e.js"></script>

<body class="body">

<ul class="scene w-list-unstyled" id="scene" style="transform: translate3d(0px, 0px, 0px);transform-style: preserve-3d;backface-visibility: hidden;overflow: hidden;height: 100vh;">
<li class="layer layer-1" data-depth="1.00" style="position: relative; display: block; left: 0px; top: 0px; transform: translate3d(-78.5px, -55.7px, 0px); transform-style: preserve-3d; backface-visibility: hidden;"></li>
<li class="layer layer-2" style="position: absolute; display: block; left: 0px; top: 0px; transform: translate3d(0px, 0px, 0px); transform-style: preserve-3d; backface-visibility: hidden;"></li>
<li class="layer layer-3" data-depth="0.50" style="position: absolute; display: block; left: 0px; top: 0px; transform: translate3d(-39.25px, -27.85px, 0px); transform-style: preserve-3d; backface-visibility: hidden;"></li></ul>
</body>

这里有一个jsFiddle 来演示。

【讨论】:

  • 非常感谢 :) 你是唯一一个知道我想要它的人!我还有一些问题需要解决,主要与移动版本有关。我将为不同的移动版本制作单独的图层,因为我需要使图像缩放得更好,它们也会在 div 中反弹。
  • 很高兴能帮上忙!
【解决方案3】:

您是否尝试过将背景 div 的边距设置为负数,并将填充设置为正数?这将允许背景图像“溢出”其 div。

【讨论】:

    【解决方案4】:

    如果使用img 标签,一种方法是使用scale:

    img {
      transform: scale(2);
    }
    

    JSFiddle 演示:https://jsfiddle.net/9hej5acL/1/

    如果使用background-image,那么您可以使用background-size 来缩放图像:

    body {
      background-image: url('http://example.com/image.png');
      background-repeat: no-repeat;
      background-position: center;
      background-size: 200% auto;
    }
    

    JSFiddle 演示:https://jsfiddle.net/4cw7qx2o/1/

    【讨论】:

    • 这将缩放背景图像,但元素本身的大小仍然相同。它不会实现@James Bimson 正在寻找的视差滚动效果。
    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多