我目前正在做一个javascript视差页面。我设法设置了背景图片和另外2张图片(#content,#content2)。
当我一直滚动到我的内容下方,然后再滚动到我的content2时,我希望网页在此结束。但是我可以无限向下滚动。
任何人都可以看看我的代码并告诉我需要添加或更改的内容,以便我的网页在content2之后结束并停止滚动。
请注意,我的#image是我的主要背景,而content和content2是位于背景之上的单独图像,但是我希望页面和滚动在content2处停止。
代码:
<style type="text/css">
* {
margin: 0px;
padding: 0px;
}
#image {
position: relative;
z-index: -1
}
#content {
height:690px;
width: 100%;
margin-top:-10px;
background:url(http:/chicago_bulls_wallpaper_backgrounds.jpg);
position: relative;
z-index: 1;
background-repeat: no-repeat;
background-position: center center;
}
#content2 {
top:710px;
height:570px;
width: 100%;
margin-top:-10px;
background:url(All.jpg);
position: relative;
z-index: 1;
background-repeat: no-repeat;
background-position: center center
}
</style>
<script type="text/javascript">
var ypos, image;
function parallex() {
ypos = window.pageYOffset;
image = document.getElementById('image');
image.style.top = ypos * 1 + 'px';
}
window.addEventListener('scroll', parallex);
</script>
<img id="image" src="black-glass.png" height="710px" width="100%" />
<div id="content"></div>
<div id="content2"></div>
这是因为您的视差因子为1
,这意味着背景正随着屏幕精确移动。因此,浏览器认为它总是有空间并且总是有能力向下滚动,这实际上是一个非常有趣的错误。
如果要进行真正的视差滚动,则将因子设置为小于1,如下所示:
image.style.top = ypos * 0.95 + 'px';
如果您根本不希望背景随页面的其余部分一起移动,请将主体的背景设置为该图像(就像您对div所做的那样),然后将该background-attachment
属性设置为fixed
-不需要JavaScript。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句