我试图创建一个包含文本和颜色的基本视差滚动示例;没有图像。然而,这种效果似乎不起作用。以下是代码:
<!doctype html>
<html>
<head>
<title>Parallax Scrolling</title>
</head>
<style type="text/css">
body{
margin:0;
padding:0;
}
#img1 {
background: rgb(44,89,238) 50% 0 repeat fixed;
height: 1000px;
margin: 0 auto;
width: 100%;
max-width: 1920px;
position: relative;
box-shadow: 0 0 50px rgba(0,0,0,0.8);
}
#img2 {
background: rgb(252,151,20) 50% 0 repeat fixed;
height: 1000px;
margin: 0 auto;
width: 100%;
max-width: 1920px;
position: relative;
box-shadow: 0 0 50px rgba(0,0,0,0.8);
}
p {
height: 458px;
position: absolute;
top: 291px;
width: 100%;
font-size: 100px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$(document).ready(function(){
var $window = $(window);
$('section[data-type="background"]').each(function(){
var $bgobj = $(this); // assigning the object
$(window).scroll(function() {
var yPos = -($window.scrollTop() / $bgobj.data('speed'));
var coords = '50% '+ yPos + 'px';
$bgobj.css({ backgroundPosition: coords });
});
});
});
</script>
<body>
<section id="img1"><p>Hello</p></section>
<section id="img2"><p>Hello</p></section>
</body>
</html>我试图编辑css,但似乎没有任何工作。请帮帮忙。
发布于 2014-01-01 20:54:40
有一些实用的视差滚动JS库,它们可能会对我们帮助很大,请看一下:
滚动效果:
http://markdalgleish.com/projects/stellar.js/
星型是有用的和容易的,它可以简单的添加data-stellar-ratio=""到一个元素,但是它可以用jQuery来调整,它是用jQuery写的,所以需要jQuery才能使用它。
陀螺/鼠标运动效应:
http://wagerfield.github.io/parallax/
Parallax.js也很方便,也很酷,虽然这不是你提到的视差滚动,但我想你还是会喜欢的。视差是用jQuery和JavaScript编写的,因此不需要jQuery。
https://stackoverflow.com/questions/20873066
复制相似问题