因此,我是JavaScript的新手,尤其是视差滚动。但是我尝试按照基本教程进行学习,但有两件事没有用:

(1)要实现视差滚动的第二个背景图像不在文本后面。
(2)视差滚动甚至无法正常工作,这可能与以下事实有关:即使图像位于完全不同的div标签中,图像仍与文本位于同一层。

我是初学者,请帮忙。

这是整个代码的链接:

(http://codepen.io/allon-z/pen/JXqBNL?editors=1100)

最佳答案

您需要包含javascript。您可以使用头部或正文中的script标记添加它。

<script>
  function parallax(){
    var prlx_lyr_1 = document.getElementById('prlx_lyr_1');
    var prlx_lyr_2 = document.getElementById('prlx_lyr_2');
    prlx_lyr_1.style.top = -(window.pageYOffset / 4)+'px';
    prlx_lyr_2.style.top = -(window.pageYOffset / 8)+'px';
  }
  window.addEventListener("scroll", parallax, false);
</script>


片段:


  function parallax(){
    var prlx_lyr_1 = document.getElementById('prlx_lyr_1');
    var prlx_lyr_2 = document.getElementById('prlx_lyr_2');
    prlx_lyr_1.style.top = -(window.pageYOffset / 4)+'px';
    prlx_lyr_2.style.top = -(window.pageYOffset / 8)+'px';
  }
  window.addEventListener("scroll", parallax, false);

body{
  margin: 0px;
  position:relative;
}

#content_layer{
  position: absolute;
  height: 32000px;
}

#prlx_lyr_1{
  position: fixed;
  background: url(https://wallpaperscraft.com/image/shape_highlights_circles_light_47468_3840x2400.jpg) no-repeat;
  width: 100%;
  height: 800px;
}

#prlx_lyr_2{
  position: fixed;
  background: url(http://space-facts.com/wp-content/uploads/venus-transparent.png) no-repeat 0px 100px;
  width: 100%;
  height: 800px;
}

<body>
  <div id="prlx_lyr_1"></div>
  <div id="prlx_lyr_2"></div>
  <div id="content_layer">
    <h2>About Me</h2>
    <p>Hi! I am a web developper that focuses on fast, responsive web development. I am proficient in JavaScript, PHP, Databases, SQL, HTML, CSS, Bootstrap, and much more. Check out some of my websites!</p>

    <h2>Porfolio</h2>
    <p>Hi! I am a web developper that focuses on fast, responsive web development. I am proficient in JavaScript, PHP, Databases, SQL, HTML, CSS, Bootstrap, and much more. Check out some of my websites!</p>

    <h2>Contact</h2>
    <p>Hi! I am a web developper that focuses on fast, responsive web development. I am proficient in JavaScript, PHP, Databases, SQL, HTML, CSS, Bootstrap, and much more. Check out some of my websites!</p>

    <h2>Social Media</h2>
    <p>Hi! I am a web developper that focuses on fast, responsive web development. I am proficient in JavaScript, PHP, Databases, SQL, HTML, CSS, Bootstrap, and much more. Check out some of my websites!</p>

    <h2>My Webiste Links</h2>
    <p>Hi! I am a web developper that focuses on fast, responsive web development. I am proficient in JavaScript, PHP, Databases, SQL, HTML, CSS, Bootstrap, and much more. Check out some of my websites!</p>

    <h2>More Gibberish</h2>
    <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sollicitudin tempus odio, sed vulputate quam molestie ornare. Sed et risus felis. Donec aliquet suscipit urna quis facilisis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras auctor orci eros, non fringilla enim tempus sit amet. Aliquam et cursus massa. Donec urna magna, blandit id eros quis, pretium eleifend augue. Ut blandit neque arcu, vitae placerat lacus volutpat eget. Praesent suscipit nulla leo, ut eleifend est auctor ac. Vivamus ut arcu pulvinar, vulputate ipsum ac, sodales elit.

    Cras interdum ligula magna, eu mollis   leo sagittis tristique. Ut semper arcu non tristique posuere. Nullam efficitur purus ac nibh congue, sed sollicitudin nisi sodales. Nullam consectetur at lorem vitae congue. Phasellus condimentum gravida leo, sed maximus neque eleifend eget. Duis at leo purus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam porta non ante eget pulvinar. Donec in urna non velit accumsan auctor eu vel ipsum. Phasellus interdum id dolor a venenatis. Aliquam facilisis lacinia justo, vitae porta mi ornare eget. Donec et sollicitudin nisl. Mauris dignissim consequat magna at ornare. Vestibulum at dolor turpis.

    Mauris tristique tellus leo, at porttitor urna tincidunt id. Pellentesque nec iaculis tortor. Aenean maximus justo ac enim pellentesque gravida. Cras ullamcorper aliquam nunc quis auctor. Fusce semper dui nec accumsan suscipit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed tempor neque. Curabitur porttitor velit vitae nisi pulvinar, in rhoncus erat tempor.

    Nam at sodales arcu. Sed vehicula tortor ut enim molestie consectetur. Sed eu porta nulla. Integer ultricies lorem quis sagittis mattis. Cras vel dui dolor. In feugiat sem quis pellentesque dictum. Nam quis metus justo. Sed non leo arcu. Donec commodo vel purus non semper. Sed viverra nisl at leo finibus, ac mollis ligula vehicula.

    Curabitur dignissim ligula quis velit finibus fringilla. Donec varius consectetur purus, ac consectetur neque pulvinar ac. Donec sit amet leo eget sem maximus tempus quis ac ex. Praesent sapien erat, finibus vel nisi eget, molestie gravida tellus. Donec sapien augue, aliquam id convallis et, vulputate hendrerit est. Curabitur nibh ipsum, pharetra vitae sodales eget, scelerisque eget erat. Nulla nec cursus eros, sed rhoncus sapien. Cras diam quam, pulvinar gravida massa sed, vehicula imperdiet mi. Praesent et ipsum sed sem sodales dapibus. Sed pulvinar dui a sollicitudin fermentum. Nulla faucibus dolor nisl, a pretium nunc laoreet sed. Cras magna odio, fringilla id sagittis ullamcorper, aliquam a orci. Integer convallis id turpis ut sagittis.</p>

  </div>

</body>

关于javascript - 视差滚动-图像不落后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37291042/

10-11 02:19