【问题标题】:html/css parallax with fixed navigation带有固定导航的 html/css 视差
【发布时间】:2015-09-04 19:46:52
【问题描述】:

我有下面的代码,需要一些关于固定位置的帮助。特别是涉及透视和变换。当我将视差效果与 css (transform:translateZ) 一起使用时,导航位置 (position:fixed) 不适用。我希望有人可以帮助我指出如何在滚动时将导航固定在顶部并保持视差效果。

html:

  <div class="parallax-layer parallax-slider">
     <div id="slider">
        <img src="img/image/background.png">
     </div>
  </div>

  <div id="nav-top"></div>

  <div class="parallax-layer parallax-body">
     <div id="nav">
        <ul>
            <li><a href="#">first item</a></li>
            <li><a href="#">second item</a></li>
            <li><a href="#">third item</a></li>
            <li><a href="#">forth item</a></li>
        </ul>
     </div>
     <div id="main-wrapper">
        <div id="main">
           <div id="content">
           content goes here
           </div>
        </div>
     </div>
  </div>

  <footer></footer>

css:

body {
    padding: 0;
    margin: 0;
    resize: both;
    width: 100%;
}
#wrapper {
}
.parallax {
    perspective: 1700px;
    height: 100vh;
    overflow-x: hidden;
    overflow-y; auto;
}
.parallax-layer {
    top:0;
    right: 0;
    bottom: 0;
    left: 0;
}
.parallax-slider {
    transform: translateZ(-300px) scale(1.2,1.287);
}
.parallax-body {
    transform: translateZ(0);
    z-index: 1;
    background-color: white;
}
#nav-top {
    top:0;
}
#nav.fix {
    position: fixed !important;
    top:0;
}
#nav {
    display: table;
    width: 100%;
    min-width: 1080px;
    background-color: rgba(39,39,39,1);
    height: 50px;
    overflow: hidden;
}
#content {
    margin-top=0;
}
#slider {
    width: 100%;
    height: 600px;
    margin: 0 auto;
    overflow: hidden;
    background-color: transparent;
}
#slider img {
    width: 100%;
    margin-top: -100px;
    right: 0;
}
#main-wrapper {
    width:100%;
    margin: 0 auto;
    margin-bottom: 0; 
}
#main {
    width: 60%;
    margin: 0 auto;
    margin-bottom: 50px;
    margin-top: 10px;
}

jquery:

$(function () {
    $(window).scroll(function(){
    var window_top = $(window).scrollTop();
    var div_top = $("#nav-top").offset().top;
    console.log(div_top);
    if (window_top > div_top) {
        $('#nav').addClass('fix');
    } else {
        $('#nav').removeClass('fix');
    }
    });
});

【问题讨论】:

标签: jquery html css


【解决方案1】:

对于视差效果,您还可以使用 background-attachment: fixed;并将您的图片放入 background-image:url("your img path");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多