以下是 9款不同jquery背景导航切换特效代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<title>www.baidu.com</title>
<meta charset="UTF-8" />
<meta name="description" content="Sliding Background Image Menu with jQuery" />
<meta name="keywords" content="jquery, background image, image, menu, navigation, panels" />
<meta name="author" content="Codrops" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/sbimenu.css" />
<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=News+Cycle&v1' rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
<div class="header">
<h1>Sliding Background Image Menu<span>with jQuery</span></h1>
<h2>Default - Show/hide</h2>
</div>
<div class="content">
<div id="sbi_container" class="sbi_container">
<div class="sbi_panel" data-bg="images/1.jpg">
<a href="#" class="sbi_label">About</a>
<div class="sbi_content">
<ul>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
</ul>
</div>
</div>
<div class="sbi_panel" data-bg="images/2.jpg">
<a href="#" class="sbi_label">Menu</a>
<div class="sbi_content">
<ul>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
</ul>
</div>
</div>
<div class="sbi_panel" data-bg="images/3.jpg">
<a href="#" class="sbi_label">Location</a>
<div class="sbi_content">
<ul>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
<li><a href="#">Subitem</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="more">
<ul>
<li>More examples:</li>
<li class="selected"><a href="example1.html">Show/hide</a></li>
<li><a href="example2.html">Fade</a></li>
<li><a href="example3.html">Sequential fade</a></li>
<li><a href="example4.html">Side slide</a></li>
<li><a href="example4_1.html">Side slide (bounce)</a></li>
<li><a href="example5.html">Sequential slide</a></li>
<li><a href="example6.html">Up/down</a></li>
<li><a href="example7.html">Sequential up/down</a></li>
<li><a href="example8.html">Alternating up/down</a></li>
<li><a href="example8_1.html">Alternating up/down (2)</a></li>
<li><a href="example9.html">Seq. alt. up/down</a></li>
</ul>
</div>
</div>
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="js/jquery.bgImageMenu.js"></script>
<script type="text/javascript">
$(function() {
$('#sbi_container').bgImageMenu();
});
</script>
</body>
</html>
JS代码(jquery.bgImageMenu.js):
(function($){
jQuery.fn.reverse = Array.prototype.reverse;
var temp={
}
,aux={
// sets the panels bg image & positionsetPanelBackground:function( bgimage,$panels,panelWidth ){
$panels.css({
'background-image':'url(' + bgimage + ')'}
).each(function(i){
$(this).css('background-position',( - i * panelWidth ) + 'px 0px');
}
);
}
,preloadImages:function( $panels,nmbPanels ){
var cnt= 0;
$panels.each(function(i){
var $panel= $(this),bgimage= $panel.data('bg');
$('<img/>').attr( 'src',bgimage );
}
);
}
,setup:function( $panels,nmbPanels,panelWidth,$panelBg,$el,$content,settings ){
$content.each(function(i){
var $el= $(this);
// save each content's height (where the menu items are)// and hide them by setting the height to 0px$el.data( 'height',$el.outerHeight(true) ).css( 'height','0px' ).show();
}
);
// set the width for the panels;
$panels.css( 'width',panelWidth + 'px' );
// set the width of the panelBgs$panelBg.css( 'width',panelWidth + 'px' );
// set the width,height and background image of the main container$el.css({
'width':panelWidth * nmbPanels + 'px','height':settings.height + 'px'}
);
// if defaultBg is passed then defaultBg is set as background,// otherwise we set the image of the default opened panelif( settings.defaultBg )aux.setPanelBackground( settings.defaultBg,$panels,panelWidth );
elseaux.setPanelBackground( temp.currentBgImage,$panels,panelWidth );
// apply a margin right of settings.border pixels for all the panels except the last one$panels.not( $panels.eq( nmbPanels - 1 ) ).css( 'margin-right',settings.border + 'px' );
var spaces= (nmbPanels - 1) * settings.border;
// adjust the sbi_container's width given the margins$el.css( 'width',$el.width() + spaces + 'px' );
}
}
,// animation typesanim={
def:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var $panel= $label.closest('div.sbi_panel'),panelIdx= $panel.index(),bgimage= $panel.data('bg'),dir;
if( temp.current === panelIdx ){
$el.data( 'anim',false );
return false;
}
temp.current= $panel.index();
anim[settings.type.mode].slideAux( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0;
// set the correct left to the $panelBg// and also the bg image$panelBg.css({
'left':'0px','background-image':'url(' + bgimage + ')'}
).each(function(i){
$(this).css('background-position',( - i * panelWidth ) + 'px 0px');
}
);
$el.data( 'anim',false );
}
}
,fade:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like def modeanim['def'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0;
// set the correct left to the $panelBg// and also the bg image$panelBg.css({
'left':'0px','background-image':'url(' + bgimage + ')'}
).each(function(i){
$(this).hide() .css('background-position',( - i * panelWidth ) + 'px 0px') .fadeIn(settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bgaux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
);
}
}
,seqFade:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var $panel= $label.closest('div.sbi_panel'),panelIdx= $panel.index(),bgimage= $panel.data('bg'),dir;
if( temp.current < panelIdx )dir= 1;
else if( temp.current > panelIdx )dir= -1;
else{
$el.data( 'anim',false );
return false;
}
temp.current= $panel.index();
anim[settings.type.mode].slideAux( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0,seq_t= settings.type.seqfactor,$elems= $el.find('div.sbi_panel_img');
if( dir === -1 )$elems = $elems.reverse();
// set the correct left to the $panelBg// and also the bg image$elems.css({
'left':'0px','background-image':'url(' + bgimage + ')'}
).each(function(i){
var $thePanelBg= $(this).hide();
setTimeout(function(){
var factor= - i * panelWidth;
if( dir === -1 )factor= - (nmbPanels - 1 - i) * panelWidth;
$thePanelBg.css('background-position',factor + 'px 0px') .fadeIn(settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bgaux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
,i * seq_t);
}
);
}
}
,horizontalSlide:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0;
// set the correct left to the $panelBg depending on dir// and also the bg image$panelBg.css({
'left':dir * panelWidth + 'px','background-image':'url(' + bgimage + ')'}
).each(function(i){
$(this).css('background-position',( - i * panelWidth ) + 'px 0px') .stop() .animate({
left:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
);
}
}
,seqHorizontalSlide:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0,seq_t= settings.type.seqfactor,$elems= $el.find('div.sbi_panel_img');
if( dir === 1 )$elems = $elems.reverse();
// set the correct left to the $panelBg depending on dir// and also the bg image$elems.css({
'left':dir * panelWidth + 'px','background-image':'url(' + bgimage + ')'}
).each(function(i){
var $thePanelBg= $(this);
setTimeout(function(){
var factor= - i * panelWidth;
if( dir === 1 )factor= - (nmbPanels - 1 - i) * panelWidth;
$thePanelBg.css('background-position',factor + 'px 0px') .stop() .animate({
left:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
,i * seq_t);
}
);
}
}
,verticalSlide:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0;
// set the correct top to the $panelBg depending on dir// and also the bg image$panelBg.css({
'top':dir * settings.height + 'px','background-image':'url(' + bgimage + ')'}
).each(function(i){
$(this).css('background-position',( - i * panelWidth ) + 'px 0px') .stop() .animate({
top:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
);
}
}
,seqVerticalSlide:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0,seq_t= settings.type.seqfactor,$elems= $el.find('div.sbi_panel_img');
if( dir === 1 )$elems = $elems.reverse();
// set the correct top to the $panelBg depending on dir// and also the bg image$elems.css({
'top':dir * settings.height + 'px','background-image':'url(' + bgimage + ')'}
).each(function(i){
var $thePanelBg= $(this);
setTimeout(function(){
var factor= - i * panelWidth;
if( dir === 1 )factor= - (nmbPanels - 1 - i) * panelWidth;
$thePanelBg.css('background-position',factor + 'px 0px') .stop() .animate({
top:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
,i * seq_t);
}
);
}
}
,verticalSlideAlt:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0,j;
// set the correct top to the $panelBg// and also the bg image$panelBg.css({
'background-image':'url(' + bgimage + ')'}
).each(function(i){
if( i % 2 === 0 )j = 1;
elsej = -1;
$(this).css('top',j * settings.height + 'px') .css('background-position',( - i * panelWidth ) + 'px 0px') .stop() .animate({
top:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
);
}
}
,seqVerticalSlideAlt:{
slide:function( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
// same like seqFade modeanim['seqFade'].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
,slideAux:function( bgimage,dir,nmbPanels,panelWidth,$panelBg,$el,$panels,settings ){
var cnt= 0,seq_t= settings.type.seqfactor,$elems= $el.find('div.sbi_panel_img'),j;
if( dir === 1 )$elems = $elems.reverse();
// set the correct top to the $panelBg depending on dir// and also the bg image$elems.css({
'top':dir * settings.height + 'px','background-image':'url(' + bgimage + ')'}
).each(function(i){
var $thePanelBg= $(this);
setTimeout(function(){
var factor= - i * panelWidth;
if( dir === 1 )factor= - (nmbPanels - 1 - i) * panelWidth;
if( i % 2 === 0 )j = 1;
elsej = -1;
$thePanelBg.css('top',j * settings.height + 'px') .css('background-position',factor + 'px 0px') .stop() .animate({
top:'0px'}
,settings.type.speed,settings.type.easing,function(){
++cnt;
if( cnt === nmbPanels ){
$el.data( 'anim',false );
// set default bg aux.setPanelBackground( bgimage,$panels,panelWidth );
}
}
);
}
,i * seq_t);
}
);
}
}
}
,methods ={
init:function( options ){
if( this.length ){
var settings ={
/*by default the first item is openedif defaultBg is passed,then pos will be ignored*/
pos:0,// width of the sbi_container (image width)width:'900',// height of the sbi_container (image height)height:'510',// border / margin size (distance between panels)border:0,// time that the menu takes to expand / collapsemenuSpeed:450,// animation typetype:{
// name:use def | fade | seqFade | horizontalSlide |// seqHorizontalSlide | verticalSlide | seqVerticalSlide |// verticalSlideAlt | seqVerticalSlideAltmode:'def',// speed of the panel animationspeed:250,// easing type for the animationeasing:'jswing',// this is the interval between each panel's animation// used for seqFade & seqHorizontalSlide & seqVerticalSlide & seqVerticalSlideAltseqfactor:100}
}
;
return this.each(function(){
// if options exist,lets merge them with our default settingsif ( options ){
$.extend( settings,options );
}
var $el= $(this),$panels= $el.children('div.sbi_panel'),nmbPanels= $panels.length,$labels= $el.find('a.sbi_label'),$content= $el.find('div.sbi_content'),animTime,// width for each panelpanelWidth= Math.floor( settings.width / nmbPanels );
// preload imagesaux.preloadImages( $panels,nmbPanels );
// current paneltemp.current= settings.pos;
if( settings.defaultBg )temp.current= -1;
else{
var $defaultPanel= $panels.eq( settings.pos );
temp.currentBgImage= $defaultPanel.data('bg');
}
// prepend a bg image container for each one of the panels// this will have the right image as background$panels.prepend('<div class="sbi_panel_img"></div>');
// have a reference to those containers - $panelBgvar $panelBg= $el.find('div.sbi_panel_img');
// set this and that...aux.setup( $panels,nmbPanels,panelWidth,$panelBg,$el,$content,settings );
// if defaultBg is not passed we show the menu of the default panelif( !settings.defaultBg ){
var $defContent= $defaultPanel.children('div.sbi_content');
$defContent.css( 'height',$content.data('height') + 'px' );
}
// mouseenter event on the labels:$labels.bind( 'mouseenter',function(e){
var $label= $(this),$content= $label.next();
clearTimeout(animTime);
animTime= setTimeout(function(){
if( $el.data( 'anim' ) ) return false;
$el.data( 'anim',true );
if( temp.current != -1 ){
$panels.eq( temp.current ) .find('div.sbi_content') .stop() .animate({
height:'0px'}
,settings.menuSpeed);
}
anim[settings.type.mode].slide( $label,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
$content.stop().animate({
height:$content.data('height') + 'px'}
,settings.menuSpeed);
}
,100);
}
)// mouseleave event on the main container (just if we have set a default bg image for the menu)// this will reset the menu to the original / default image$el.bind('mouseleave',function(e){
if( temp.current != -1 ){
$panels.eq( temp.current ) .find('div.sbi_content') .stop() .animate({
height:'0px'}
,settings.menuSpeed);
temp.current= -1;
if( settings.defaultBg ){
clearTimeout(animTime);
if( $el.data( 'anim' ) ) return false;
$el.data( 'anim',true );
anim[settings.type.mode].slideAux( settings.defaultBg,-1,nmbPanels,panelWidth,$panelBg,$el,$panels,settings );
}
}
return false;
}
);
}
);
}
}
}
;
$.fn.bgImageMenu = function(method){
if ( methods[method] ){
return methods[method].apply( this,Array.prototype.slice.call( arguments,1 ));
}
else if ( typeof method === 'object' || ! method ){
return methods.init.apply( this,arguments );
}
else{
$.error( 'Method ' + method + ' does not exist on jQuery.bgImageMenu' );
}
}
;
}
)(jQuery);
CSS代码(reset.css):
/* CSS reset */
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0;}
html,body{margin:0;padding:0;}
table{border-collapse:collapse;border-spacing:0;}
fieldset,img{border:0;}
input{border:1px solid #b0b0b0;padding:3px 5px 4px;color:#979797;width:190px;}
address,caption,cite,code,dfn,th,var{font-style:normal;font-weight:normal;}
ol,ul{list-style:none;}
caption,th{text-align:left;}
h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}
q:before,q:after{content:'';}
abbr,acronym{border:0;}
CSS代码(sbimenu.css):
.sbi_container{position:relative;margin:0 auto;overflow:hidden;padding:10px;background:#000;-moz-box-shadow:1px 1px 5px #000;-webkit-box-shadow:1px 1px 5px #000;box-shadow:1px 1px 5px #000;-moz-border-radius:4px;-webkit-border-radius:4px;border-radius:4px 4px 4px 4px;}
.sbi_panel{float:left;position:relative;height:100%;overflow:hidden;}
.sbi_panel_img{position:absolute;height:100%;top:0px;background-repeat:no-repeat;}
.sbi_label{display:block;width:100%;background:#000;text-align:center;font-size:20px;height:40px;line-height:40px;position:absolute;bottom:50px;left:0px;opacity:0.7;cursor:pointer;text-shadow:0px 0px 1px #000;text-decoration:none;color:#fff;outline:none;}
.sbi_label:hover{opacity:0.9;}
.sbi_content{position:absolute;border-top:2px solid #000;bottom:90px;left:0px;width:100%;background:transparent url(../images/pattern.png) repeat top left;display:none;overflow:hidden;}
.sbi_content ul{padding:10px;}
.sbi_content ul a{display:block;color:#f0f0f0;font-size:16px;padding:4px 6px 4px 14px;background:transparent url(../images/triangle.png) no-repeat 3px 50%;opacity:0.9;}
.sbi_content ul a:hover{background-color:#000;color:#fff;-moz-box-shadow:1px 1px 5px #000;-webkit-box-shadow:1px 1px 5px #000;box-shadow:1px 1px 5px #000;}
CSS代码(style.css):
@import url('reset.css');body{background:#555 url(../images/random_grey_variations.png) repeat top left;color:#000;font-family:'PT Sans Narrow',Arial,sans-serif;font-size:12px;overflow-y:scroll;}
a{color:#f0f0f0;text-decoration:none;}
.clr{clear:both;}
.container{width:100%;margin:0 auto;}
.header{padding-top:10px;}
.more ul{text-transform:uppercase;font-size:13px;font-weight:bold;padding:20px 0px 30px 20px;height:30px;}
.more ul li{float:left;margin:0px 1px;}
.more ul li.selected a,.more ul li.selected a:hover{background:#000;color:#fff;text-shadow:none;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px 5px 5px 5px;}
.more ul li a{text-shadow:1px 1px 1px #000;color:#e3cbfe;padding:3px 6px;}
.more ul li a:hover{color:#bda2dc;}
h1{margin:0 0 13px 0;float:left;clear:both;font-size:34px;padding:12px;text-shadow:0px 0px 1px #000;color:#f8f8f8;font-family:'News Cycle',Georgia,serif;}
h1 span{color:#e3cbfe;text-align:right;font-size:25px;padding:0 10px;}
h2{float:right;color:#bda2dc;padding:30px 20px 0 0;}
.content{width:100%;position:relative;margin:20px 0 0 0;clear:both;}
/* Footer Style */
.topbar{position:relative;width:100%;height:25px;line-height:24px;bottom:0;font-size:13px;background:#000;opacity:0.7;text-transform:uppercase;font-family:'PT Sans Narrow',Arial,sans-serif;z-index:4;}
.topbar a{padding:5px 10px;letter-spacing:1px;text-shadow:1px 1px 1px #000;color:#ddd;text-align:right;}
.topbar a:hover{color:#fff;}
.topbar a span{font-weight:bold;}
.topbar span.right_ab{position:absolute;right:4px;}