简体   繁体   English

CSS / JS内容向上滚动而不是向下滚动

[英]CSS/JS content scroll UP instead of down

I'm building a site with the navigation bar stretching across the entire site and it's fixed. 我正在建立一个网站,导航栏横跨整个网站,并且已修复。 Under the navigation bar there is an image, a background image, which is set as a cover. 在导航栏下面有一个图像,一个背景图像,它被设置为封面。 And under the image is the main content. 而在图像下是主要内容。

When you scroll down, the navigation bar covers the image from top to bottom and the main content is now visible, effectively scrolling in a downwards fashion. 向下滚动时,导航栏从上到下覆盖图像,主要内容现在可见,有效地向下滚动。 I would like to "reverse" it. 我想“逆转”它。 So the navigation is still fixed with the cover image under it but this time, when you scroll down the main content comes up and covers the image from bottom to top. 所以导航仍然固定在它下面的封面图像,但这一次,当你向下滚动时,主要内容出现并从下到上覆盖图像。 So when you scroll down, the main content scrolls up. 因此,当您向下滚动时,主要内容会向上滚动。

Let's say my image has a 1 at the top and a 2 at the bottom. 假设我的图像顶部为1,底部为2。 So, normally when you scroll down the navigation bar covers the image from top to bottom the 1 will disappear and the 2 will be visible until that is also covered. 因此,通常当您向下滚动导航栏从上到下覆盖图像时,1将消失,2将被显示,直到它也被覆盖。 The effect I'm looking for would make the 2 disappear and the 1 would remain in the same place until it is covered by the main content. 我正在寻找的效果将使2消失,1将保持在同一个地方,直到它被主要内容覆盖。

I looked into parallax but I'm not sure if that's the right thing to go with. 我调查了视差,但我不确定这是否合适。 And I have no idea how to do achieve this effect. 我不知道如何实现这种效果。

Hopefully you'll understand what I'm trying to do here. 希望你能理解我在这里要做的事情。 If you need any more info then just let me know. 如果您需要更多信息,请告诉我。

Thanks in advance. 提前致谢。

EDIT 编辑

The effect can be seen on the abduzeedo frontpage 这个效果可以在abduzeedo首页上看到

You need the image to be "attached" to the background ? 您需要将图像“附加”到背景中吗? If so, cannot you just fix it to the background ? 如果是这样,你不能把它修复到后台吗?

body {
    background-attachment:fixed;
}

Source: http://www.w3schools.com/cssref/pr_background-attachment.asp https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment 资料来源: http //www.w3schools.com/cssref/pr_background-attachment.asp https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment

Note: Be careful using W3Schools, their information is often incorrect, see here . 注意:小心使用W3Schools,他们的信息通常不正确,请看这里

Here's an example with an img element. 这是一个img元素的例子。

Demo 演示

code view 代码视图

The basic HTML layout: 基本的HTML布局:

<nav></nav>
<img src="/image.ext" class="scrollup" />
<div class="main"></div>

Your nav will be positioned fixed, as you said. 正如你所说,你的导航将定位固定。 The image also needs fixed positioning. 图像也需要固定定位。 We set its z-index to -1 to make sure it's covered up. 我们将其z-index设置为-1以确保它已被掩盖。

img {
  position: fixed;
  width: 100%;
  z-index: -1;
  top: 20px; left: 0;
}

The main element is positioned relatively. 主要元素相对定位。 Because our nav and image both have fixed positioning, the top value is relative to the top of the viewport. 因为我们的导航和图像都具有固定的定位,所以top值相对于视口的顶部。 100% means that .main starts as soon as we start scrolling. 100%表示.main开始滚动就会启动。

.main {
  background: white;
  position: relative;
  top: 100%;
}

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

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