您好,我正在开发一个项目,我找到了一个在codepen.io上使用的免费小提琴。在网站上它完美地工作,当你去为我做的js小提琴有一个问题。当您向下滚动到主要内容时,主标题和框将显示在文本上。有没有办法消除这种情况,就像在原始的代码笔上显示一样?
小提琴HERE
function EasyPeasyParallax() {
scrollPos = $(this).scrollTop();
$('#banner').css({
'background-position' : '50% ' + (-scrollPos/4)+"px"
});
$('#bannertext').css({
'margin-top': (scrollPos/4)+"px",
'opacity': 1-(scrollPos/250)
});
}
$(document).ready(function(){
$(window).scroll(function() {
EasyPeasyParallax();
});
});

答案 0 :(得分:1)
编织: http://kodeweave.sourceforge.net/editor/#dcbf9c586b0e798fce89be31c50e3950
注意: zIndex仅适用于positioned元素。
我所做的就是告诉#content
为position relative
并且效果很好。
html, body {
height: 100%;
}
body {
background: #FFF;
font: 400 1.5em/1.5 "Droid Serif", serif;
color: #111;
text-align: center;
}
h1 {
font: 700 2.8em/1.2 "Droid Sans", sans-serif;
}
h2 {
font: 700 1.5em/1.5 "Droid Sans", sans-serif;
margin: 1em 0;
}
#banner {
background: url(http://f.cl.ly/items/1b2V08451N440W0H420K/bg.jpg) no-repeat fixed 50% 0;
background-size: cover;
color: #fff;
height: 100%;
margin-left: -7px;
margin-top: -8px;
width: 100%;
z-index: 1;
}
#bannertext {
width: 24em;
position: fixed;
top: 20%;
left: 50%;
border: .5em solid #fff;
margin-left: -12em;
padding: 2em 0;
z-index: 1;
}
#content {
position: relative;
max-width: 28em;
text-align: justify;
margin: 0 auto;
padding: 2em;
z-index: 10;
}
#content p {
margin: 0 0 2em;
z-index: 10;
}

<div id="banner">
<div id="bannertext">
<h1>| EX |</h1>
<p>Get famous on twitter.</p>
</div>
</div>
<div id="content">
<h2>EX</h2>
<br>
<br>
<br>
<h2>EX.</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin pellentesque, est ut venenatis aliquam, lorem quam porttitor ligula, eget ultrices velit dui sed quam. Praesent vehicula placerat lectus. Nulla pede. Quisque a nulla quis massa pulvinar sagittis. Pellentesque neque massa, mattis vulputate, pellentesque nec, vehicula volutpat, purus. Proin pretium dui et nulla cursus eleifend. Aenean aliquam urna eget urna. Vestibulum euismod elit. Donec eget augue sit amet neque elementum pretium. Proin posuere lacus id lacus. Duis vel justo suscipit neque ornare iaculis.</p>
<p>Ut urna urna, rhoncus eget, vestibulum tempus, venenatis non, nunc. Nunc consequat quam in nulla. Praesent feugiat posuere orci. Sed ac ante. Mauris pellentesque massa vitae ante mattis bibendum. Quisque dapibus lectus eu eros. Nulla facilisi. Praesent hendrerit egestas erat. Suspendisse at velit. Quisque mollis feugiat est. Curabitur ut leo. Cras auctor semper augue. Pellentesque leo pede, tempus sed, ornare in, venenatis sed, nisl. Quisque est velit, eleifend vitae, mollis ac, adipiscing at, eros. Mauris velit. Etiam nec lorem. Vestibulum pellentesque ligula a velit. Maecenas felis metus, suscipit et, eleifend vel, accumsan vitae, magna. Phasellus ut justo vel magna congue laoreet.</p>
</div>
&#13;
答案 1 :(得分:0)
答案 2 :(得分:0)
如果我没记错的话,z-index只适用于定位元素。
我添加了“position:relative;” #content,#content p和#banner似乎有效。
html {
height: 100%;
}
body {
background: #FFF;
font: 400 1.5em/1.5 "Droid Serif", serif;
color: #111;
text-align: center;
height: 100%;
}
h1 {
font: 700 2.8em/1.2 "Droid Sans", sans-serif;
}
h2 {
font: 700 1.5em/1.5 "Droid Sans", sans-serif;
margin: 1em 0;
}
#banner {
background: url(http://f.cl.ly/items/1b2V08451N440W0H420K/bg.jpg) no-repeat fixed 50% 0;
background-size: cover;
color: #fff;
height: 100%;
margin-left: -7px;
margin-top: -8px;
width: 100%;
z-index: 1;
position:relative;
}
#bannertext {
width: 24em;
position: fixed;
top: 20%;
left: 50%;
border: .5em solid #fff;
margin-left: -12em;
padding: 2em 0;
z-index: 1;
}
#content {
max-width: 28em;
text-align: justify;
margin: 0 auto;
padding: 2em;
z-index: 10;
position:relative;
}
#content p {
margin: 0 0 2em;
z-index: 10;
position:relative;
}
答案 3 :(得分:0)
只需使用最新版本的jQuery即可正常使用
小提琴:https://jsfiddle.net/gu7dt5oz/2/
<script src="https://code.jquery.com/jquery-2.2.3.min.js"
integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="
crossorigin="anonymous"></script>