以下是 jQuery纵向全屏滚动背景代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="JS代码,其他代码,JS广告代码,JS特效代码" />
<meta name="description" content="此代码内容为jQuery纵向全屏滚动背景代码,属于站长常用代码" />
<title>jQuery纵向全屏滚动背景代码</title>
<link href="css/lrtk.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/nbw-parallax.js"></script>
<script type="text/javascript" src="js/jquery.localscroll-1.2.7-min.js"></script>
<script type="text/javascript" src="js/jquery.scrollTo-1.4.2-min.js"></script>
<script type="text/javascript" src="js/jquery.inview.js"></script>
</head>
<body>
<!-- 代码 开始 -->
<ul id="nav">
<li><a href="#intro" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#second" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#third" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#fourth" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
<li><a href="#fifth" title="Next Section"><img src="images/dot.png" alt="Link" /></a></li>
</ul>
<div id="header">
<div class="story">
<h1>内容</h1>
<p>此处为头部导航区域</p>
</div> <!--.story-->
</div> <!--#header-->
<div id="intro">
<div class="story">
<div class="float-left">
<h2>(Almost) Static Background</h2>
<p>This section has a background that moves slightly slower than the user scrolls. This is achieved by changing the top position of the background for every pixel the page is scrolled.</p>
</div>
</div> <!--.story-->
</div> <!--#intro-->
<div id="second">
<div class="story"><div class="bg"></div>
<div class="float-right">
<h2>Multiple Backgrounds</h2>
<p>The multiple backgrounds applied to this section are moved in a similar way to the first section -- every time the user scrolls down the page by a pixel, the positions of the backgrounds are changed.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nibh erat, sagittis sit amet congue at, aliquam eu libero. Integer molestie, turpis vel ultrices facilisis, nisi mauris sollicitudin mauris, volutpat elementum enim urna eget odio. Donec egestas aliquet facilisis. Nunc eu nunc eget neque ornare fringilla. Nam vel sodales lectus. Nulla in pellentesque eros. Donec ultricies, enim vitae varius cursus, risus mauris iaculis neque, euismod sollicitudin metus erat vitae sapien. Sed pulvinar.</p>
</div>
</div> <!--.story-->
</div> <!--#second-->
<div id="third">
<div class="story">
<div class="float-left">
<h2>What Happens When JavaScript is Disabled?</h2>
<p>The user gets a slap! Actually, all that jQuery does is moves the backgrounds relative to the position of the scrollbar. Without it, the backgrounds simply stay put and the user would never know they are missing out on the awesome! CSS2 does a good enough job to still make the effect look cool.</p>
</div>
</div> <!--.story-->
</div> <!--#third-->
<div id="fourth">
<div class="story">
<div class="float-left">
<h2>Empty Containers vs CSS3 Multiple Backgrounds</h2>
<p><em>This section only works in modern browsers that support multiple backgrounds.</em></p>
<p>The Nikebetterworld.com demo uses only CSS2 to create it's parallax effect. It uses empty containers in each section to give the impression of multiple backgrounds, which is the method I used for the image of the trainers. It's actually possible to achieve the same effect using CSS3s multiple backgrounds -- as I've done with the bubbles in this section, although, it will only work in modern browsers that support CSS3 multiple backgrounds.</p>
<p>The use of CSS3 means less markup and jQuery, making the script slightly quicker.</p>
</div>
</div> <!--.story-->
</div> <!--#fourth-->
<div id="fifth">
<div class="story">
<h2>I Wanna See More Amazing Things!</h2>
<p>You might like to check out my other demos too:</p>
<ul>
<li><a href="#" title="Original jQuery Vertical Parallax Demo">Original jQuery Vertical Parallax Demo</a></li>
<li><a href="#" title="BBC News jQuery Map">BBC News jQuery Map</a></li>
<li><a href="#" title="jQuery Media Rich Dropdown Menu">jQuery Media Rich Dropdown Menu</a></li>
<li><a href="#" title="Referrer Specific jQuery Greeting">"Referrer Specific jQuery Greeting</a></li>
</ul>
<p>Or go visit the official <a href="#" title="Nike Better World">Nikebetterworld.com</a> website that this demo is based on. It utilises a bunch of other cool technologies too!</p>
<h2>Credits</h2>
<p>This demo makes use of some scripts and images made by others:</p>
<ul>
<li><a href="#" title="jQuery Element 'in view' Event Plugin">jQuery Element 'in view' Event Plugin</a></li>
<li><a href="#" title="jQuery ScrollTo">jQuery ScrollTo</a></li>
<li><a href="#" title="Licensed fonts from FontDeck">Licensed fonts from FontDeck</a></li>
<li><a href="#" title="Background Textures">Wooden and Pyschedlic Background Textures</a></li>
<li><a href="#" title="Trainers Image">Trainers Image</a></li>
<li><a href="#" title="Basketball Image">Basketball Image</a></li>
<li><a href="#" title="Sea Background">Sea Background</a></li>
<li><a href="#" title="Bottles Image">Bottles Image</a></li>
<li>Bubbles by <a href="#" title="Ian Lunn Design">myself</a>, feel free to use them under <a href="#" title="Lincense">the same license</a> as this demo</li>
</ul>
<p class="center">P.S - You've scrolled <span id="pixels">0</span> Pixels. Oh Yeah!</p>
</div> <!--.story-->
</div> <!--#fifth-->
<script type="text/javascript">
$(document).ready(function(){
$('#nav').localScroll();
})
</script>
<!-- 代码 结束 -->
</body>
</html>
JS代码(jquery.inview.js):
/** * author Remy Sharp * url http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
(function ($){
function getViewportHeight(){
var height = window.innerHeight;
// Safari,Opera var mode = document.compatMode;
if ( (mode || !$.support.boxModel) ){
// IE,Gecko height = (mode == 'CSS1Compat') ? document.documentElement.clientHeight:// Standards document.body.clientHeight;
// Quirks}
return height;
}
$(window).scroll(function (){
var vpH = getViewportHeight(),scrolltop = (document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body.scrollTop),elems = [];
// naughty,but this is how it knows which elements to check for $.each($.cache,function (){
if (this.events && this.events.inview){
elems.push(this.handle.elem);
}
}
);
if (elems.length){
$(elems).each(function (){
var $el = $(this),top = $el.offset().top,height = $el.height(),inview = $el.data('inview') || false;
if (scrolltop > (top + height) || scrolltop + vpH < top){
if (inview){
$el.data('inview',false);
$el.trigger('inview',[ false ]);
}
}
else if (scrolltop < (top + height)){
if (!inview){
$el.data('inview',true);
$el.trigger('inview',[ true ]);
}
}
}
);
}
}
);
// kick the event to pick up any elements already in view. // note however,this only works if the plugin is included after the elements are bound to 'inview' $(function (){
$(window).scroll();
}
);
}
)(jQuery);
JS代码(nbw-parallax.js):
/*JavaScript for the demo:Recreating the Nikebetterworld.com Parallax DemoDemo:Recreating the Nikebetterworld.com Parallax DemoAuthor:Ian LunnAuthor URL:http://www.ianlunn.co.uk/Demo URL:http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/Tutorial URL:http://www.ianlunn.co.uk/blog/code-tutorials/recreate-nikebetterworld-parallax/License:http://creativecommons.org/licenses/by-sa/3.0/ (Attribution Share Alike). Please attribute work to Ian Lunn simply by leaving these comments in the source code or if you'd prefer,place a link on your website to http://www.ianlunn.co.uk/.Dual licensed under the MIT and GPL licenses:http://www.opensource.org/licenses/mit-license.phphttp://www.gnu.org/licenses/gpl.html*/
$(document).ready(function(){
//when the document is ready...//save selectors as variables to increase performancevar $window = $(window);
var $firstBG = $('#intro');
var $secondBG = $('#second');
var $thirdBG = $('#third');
var $fourthBG = $('#fourth');
var trainers = $("#second .bg");
var windowHeight = $window.height();
//get the height of the window//apply the class "inview" to a section that is in the viewport$('#intro,#second,#third,#fourth').bind('inview',function (event,visible){
if (visible == true){
$(this).addClass("inview");
}
else{
$(this).removeClass("inview");
}
}
);
//function that places the navigation in the center of the windowfunction RepositionNav(){
var windowHeight = $window.height();
//get the height of the windowvar navHeight = $('#nav').height() / 2;
var windowCenter = (windowHeight / 2);
var newtop = windowCenter - navHeight;
$('#nav').css({
"top":newtop}
);
//set the new top position of the navigation list}
//function that is called for every pixel the user scrolls. Determines the position of the background/*arguments:x = horizontal position of backgroundwindowHeight = height of the viewportpos = position of the scrollbaradjuster = adjust the position of the backgroundinertia = how fast the background moves in relation to scrolling*/
function newPos(x,windowHeight,pos,adjuster,inertia){
return x + "% " + (-((windowHeight + pos) - adjuster) * inertia) + "px";
}
//function to be called whenever the window is scrolled or resizedfunction Move(){
var pos = $window.scrollTop();
//position of the scrollbar//if the first section is in view...if($firstBG.hasClass("inview")){
//call the newPos function and change the background position$firstBG.css({
'backgroundPosition':newPos(50,windowHeight,pos,900,0.3)}
);
}
//if the second section is in view...if($secondBG.hasClass("inview")){
//call the newPos function and change the background position$secondBG.css({
'backgroundPosition':newPos(50,windowHeight,pos,1250,0.3)}
);
//call the newPos function and change the secnond background positiontrainers.css({
'backgroundPosition':newPos(50,windowHeight,pos,1900,0.6)}
);
}
//if the third section is in view...if($thirdBG.hasClass("inview")){
//call the newPos function and change the background position$thirdBG.css({
'backgroundPosition':newPos(50,windowHeight,pos,2850,0.3)}
);
}
//if the fourth section is in view...if($fourthBG.hasClass("inview")){
//call the newPos function and change the background position for CSS3 multiple backgrounds$fourthBG.css({
'backgroundPosition':newPos(0,windowHeight,pos,200,0.9) + "," + newPos(50,windowHeight,pos,0,0.7) + "," + newPos(50,windowHeight,pos,0,0.5) + "," + newPos(50,windowHeight,pos,700,0.3)}
);
}
$('#pixels').html(pos);
//display the number of pixels scrolled at the bottom of the page}
RepositionNav();
//Reposition the Navigation to center it in the window when the script loads$window.resize(function(){
//if the user resizes the window...Move();
//move the background images in relation to the movement of the scrollbarRepositionNav();
//reposition the navigation list so it remains vertically central}
);
$window.bind('scroll',function(){
//when the user is scrolling...Move();
//move the background images in relation to the movement of the scrollbar}
);
}
);
CSS代码(lrtk.css):
@charset "utf-8";body{margin:0;min-width:980px;padding:0;}
p{margin:0 0 20px 0;}
p,ul{font-family:"Proxima Nova Light",'Helvetica Neue',Arial,Helvetica,sans-serif;font-size-adjust:0.488;font-weight:200;font-style:normal;}
img{border:0;}
h1,#pixels{font-family:"Museo 900",Verdana,serif;font-size-adjust:0.40;font-weight:700;font-style:normal;}
h2{font-family:"Beau Sans Pro Book",Verdana,sans-serif;font-size-adjust:0.531;font-weight:500;font-style:normal;}
#header h1,#pixels{color:#48941A;}
.float-left{float:left;margin:0 0 0 20px;}
.float-right{float:right;margin:0 20px 0 0;}
.center{font-size:2.5em;padding:80px 0;text-align:center;}
#nav{list-style:none;position:fixed;right:20px;}
#nav li{margin:0 0 15px 0;}
#header,#intro,#second{width:100%;}
#header{background:white;height:130px;}
#intro{background:url(../images/firstBG.jpg) 50% 0 no-repeat fixed;color:white;height:600px;margin:0;padding:160px 0 0 0;}
#second{background:url(../images/secondBG.jpg) 50% 0 no-repeat fixed;color:white;height:1300px;margin:0 auto;overflow:hidden;padding:0;}
#second .bg{background:url(../images/trainers.png) 50% 0 no-repeat fixed;height:1000px;margin:0 auto;padding:0;position:absolute;width:900px;z-index:200;}
#third{background:url(../images/thirdBG.jpg) 50% 0 no-repeat fixed;color:white;height:650px;padding:100px 0 0 0;}
#fourth{background:url(../images/bubbles2.png),url(../images/bubbles2.png),url(../images/bubbles1.png),url(../images/fourthBG.jpg);background-position:50% 0,50% 0,50% 0,50% 0;background-color:#036;background-attachment:fixed;background-repeat:repeat,repeat,repeat,no-repeat;color:white;height:1300px;padding:100px 0 0 0;}
#fifth{background:#ccc;height:1000px;margin:0 auto;padding:40px 0 0 0;}
.story{margin:0 auto;min-width:980px;width:980px;}
.story .float-left,.story .float-right{padding:100px 0 0 0;position:relative;width:350px;}