如何实现这种视差

时间:2016-04-04 09:43:57

标签: javascript html css parallax

尝试实现此问题时出现2个问题:

  • 将它放入身体会破坏粘性导航。
  • div上的滚动条。

如果我尝试在div中执行http://keithclark.co.uk/articles/pure-css-parallax-websites/。它会在该div中生成一个滚动条。

尝试通过将它放入体内来对抗它会破坏粘性导航(使用引导程序制作)。

这是我试图做的笔http://codepen.io/dam0/pen/zviHr。类似的概念。

我只需要div上的视差。不同的元素速度类型。有什么帮助吗?

这是我的html structure。请看一下。我已经有人在那里发表评论。

我正在尽可能地让它成为纯CSS。但如果不能做到,请用js帮助我。

编辑:添加笔

http://codepen.io/anon/pen/qZVEgM

将.parallax添加到想要具有视差效果的div中会导致它具有滚动条。将视差等级放到身体上会破坏位置:固定粘性导航。

编辑2:在此处移动了笔码

HTML:

<div class="parallax holder">
    <div id="carousel" class="parallax__layer parallax__back carousel slide">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#carousel" data-slide-to="0" class="active"></li>
            <li data-target="#carousel" data-slide-to="1"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner">
            <div class="item active">
                <img src="http://loremflickr.com/1920/1080/vegetable,healthy,plate" class="img-responsive center-block">
            </div>

            <div class="item">
                <img src="http://loremflickr.com/1920/1080/vegetable,healthy,plate" class="img-responsive center-block">
            </div>
        </div>
    </div>

    <div id="headerCaption" class="parallax__layer parallax__layer--base">
        <img src="http://healthyaxcess.ph/img/CaptionBg.png" id="curve" class="img-responsive center-block">
    </div>
</div>

<div id="nav-wrapper">
    <nav class="navbar navbar-inverse navbar-static-top" id="navbar-main">
        <div class="container">

            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#"><img src="http://healthyaxcess.ph/img/logo.png"></a>
            </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-shopping-cart"></i> Cart</a></li>
                    <li><a href="#"><i class="fa fa-user"></i> Sign Up/Login</a></li>
                </ul>

            <!-- Nav links -->
            <div class="collapse navbar-collapse navbar-right" id="navbar-collapse">
                <ul class="nav navbar-nav" id="nav-section">
                    <li class="current active">
                        <a href="#carousel">First Section</a>
                    </li>

                    <li>
                        <a href="#section-2">Second Section</a>
                    </li>

                    <li>
                        <a href="#section-3">Third Section</a>
                    </li>

                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Dropdown <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Dropdown link 1</a></li>
                            <li><a href="#">Dropdown link 2</a></li>
                            <li><a href="#">Dropdown link 3</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- End of navbar collapse -->
        </div><!-- End of Container Fluid -->
    </nav>
</div>

<div class="container main">
    <div class="text-center" id="section-2">
        <h1>Sample Title</h1>
        <p>The sample title's description here.</p>
    </div>

CSS:     .navbar-inverse {         背景:#303030;     }

#navbar-main {
    margin: 0;
}
/* To override the static-top */
#navbar-main.affix {
    position: fixed;
    top: 0;
    width: 100%;
}

@media (min-width: 992px) {
    #navbar-main .navbar-nav {
        position: relative;
        margin-top: 16px;
    }
}

.main {
/* for example only */
    min-height: 200px;
}

.navbar-brand {
    padding: 0;
/* firefox bug fix */
    height: 80px;
}

.navbar-brand>img {
    height: 100%;
    padding: 8px;
/* firefox bug fix */
    width: auto;
    position: relative;
}

#footerBox {
    background-color: #303030;
    color: #FAFAFA;
}

.holder {
    display: none;
}

#headerCaption {
    position: relative;
}

#headerCaption img {
    z-index: 3;
    position: absolute;
    width: 100%;
}

.carousel {
    position: relative;
}

.parallax {
    perspective: 1px;
    height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
}

.parallax__layer {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

.parallax__layer--base {
    transform: translateZ(0);
}

.parallax__layer--back {
    transform: translateZ(-1px);
}

JS:

$('#carousel img').on("load", function(){
    $('body').scrollspy({ target: '#navbar-collapse' });
    $('.holder').css('display', 'block');
    $('#nav-wrapper').height($("#navbar-main").height());
    $('#navbar-main').affix( { offset:{ top:$('#navbar-main').offset().top } } );
    $('.carousel').carousel({pause: false});
})

0 个答案:

没有答案