4

问题的小提琴:http: //jsfiddle.net/Vy365/3/

我正在尝试在具有视差滚动效果的页面上创建部分。

我用来实现这一点的主要 CSS 是background-attachment: fixed用于背景图像和图像position: fixed顶部的文本。

我在页面上有多个具有这种效果的 div,我希望每个部分都覆盖之前的部分。

HTML:

<section>
    <div id="parallax-1" class="parallax">
        <div class="title">
            <h1>Fixed Text 1</h1>
        </div>
    </div>
</section>

<section class="scrolling-content">Scrolling Content</section>

<section>
    <div id="parallax-2" class="parallax">
        <div class="title">
            <h1>Second Fixed Text</h1>
        </div>
    </div>
</section>

<section class="scrolling-content">Scrolling Content</section>

CSS:

.parallax {
    margin: 0 auto;
    padding: 0;
    position: relative;
    width: 100%;
    max-width: 1920px;
    height: 200px;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: 50% 0;
    z-index: 1;
}

.parallax .title {
    position: fixed;
    top: 80px;
}

#parallax-1 {
    background-image: url(http://placekitten.com/500/200);
}

#parallax-2 {
    background-image: url(http://placekitten.com/500/202);
}

.scrolling-content {
    position: relative;
    width: 100%;
    height: 200px;
    background: #ffffff;
    z-index: 2;
}

背景图像适当地相互覆盖,但是固定的文本仍然固定在页面上(再次参见小提琴)。

有没有办法用 CSS 解决这个问题?还是我必须做一些令人讨厌的 jquery 窗口滚动监控?

4

3 回答 3

1

认为你想使用 position:absolute 而不是 position:fixed 在你的 '.parallax .title' 类

于 2013-10-30T23:18:13.783 回答
0

既然您无论如何都在使用 jQuery,为什么不尝试像http://stephband.info/jparallax/这样的插件呢?

编辑:对于移动应用程序,您可能需要查看Skrollr。它是纯 Javascript,在“In the wild”部分中有一些非常好的示例。

它可以帮助您重新发明轮子。

于 2013-07-23T19:14:26.630 回答
0

这里有两个教程(都使用 Skrollr.js),可以帮助其他人尝试创建类似的视差滚动效果。

于 2014-01-30T11:50:57.917 回答