视差滚动(使用JavaScript)问题

Parallax scrolling(using JavaScript) issue

本文关键字:问题 JavaScript 使用 滚动 视差      更新时间:2024-02-07

我正在尝试使用JavaScript和Bootstrap来使用视差滚动。它正在工作,但页脚也随着可滚动的内容移动,我希望页脚应该固定在底部。

<?php
require 'header.php';
?>
<style type="text/css">
    *{
        margin: 0px;
        padding: 0px;
    }
    #image {
        position: relative;
        z-index: -1
    }
    #content {
        position: relative;
        z-index: 1;
        height: 750px;
        width: 100%;
        background-color:  #4dbbac;
        margin-bottom: 30px
    }
</style>

<script type="text/javascript">  
       var yPos, image;
       function parallax (){
           yPos = window.pageYOffset;
           image = document.getElementById('image');
           image.style.top = yPos * 1 + 'px';
       }
       window.addEventListener('scroll', parallax);  
</script>

<img id="image"  src="../images/company_img1.jpg" height="700px" width="100%" alt="companyProfile_image" class="img-responsive"/>
<div id="content"></div>

<?php
require 'footer.php';
?>

您应该在图像周围添加一个包装div,该div具有类似的css属性

HTML

<div class="container">
  <img id="image" src="../images/company_img1.jpg" height="700px" width="100%" alt="companyProfile_image" class="img-responsive" />
</div>

CSS

.container {
    position: relative;
    z-index: 1;
    overflow: hidden;
}

这样,图像在移动时不会影响DOM的其余部分。