所以我是JavaScript的新手,尤其是视差滚动。但我尝试按照基本教程学习,但有两件事情无效:
(1)视差滚动要实现的第二个背景图像不会落后于文本.` (2)视差滚动甚至不起作用,这可能与图像以某种方式与文本在同一层上的事实有关,即使它在完全不同的div标签中。
我是初学者所以请帮助。
以下是整个代码的链接:
(http://codepen.io/allon-z/pen/JXqBNL?editors=1100)
答案 0 :(得分:0)
您需要包含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);
&#13;
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;
}
&#13;
<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>
&#13;