Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fading fullscreen background-image rotator

Status
Not open for further replies.

ralphonzo

Programmer
Apr 9, 2003
228
0
0
GB
Struggling here. I've got a lovely fullscreen image, and a lovely image rotator, but can I get the two to marry? No! No matter what I try with the rotator I can't get the images to stretch. I'm trying to get the screen height from Javascript inline, but even if that works it probably won't fix width issues.

I'm including all of the current code, but without the rotator elements, as none of the methods I've tried work properly anyway!!

Code:
<!DOCTYPE html>

<head>
<title>NABDH - Web Concept 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<script src="[URL unfurl="true"]http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"[/URL] type="text/javascript"></script>
<script src="[URL unfurl="true"]http://tab-slide-out.googlecode.com/files/jquery.tabSlideOut.v1.3.js"></script>[/URL]
<script type="text/javascript">
$(function(){
	$('.slide-out-div').tabSlideOut({
		tabHandle: '.handle',                     //class of the element that will become your tab
		pathToTabImage: 'contab.jpg', 		  //path to the image for the tab //Optionally can be set using css
		imageHeight: '181px',                     //height of tab image           //Optionally can be set using css
		imageWidth: '40px',                       //width of tab image            //Optionally can be set using css
		tabLocation: 'right',                     //side of screen where tab lives, top, right, bottom, or left
		speed: 300,                               //speed of animation
		action: 'hover',                          //options: 'click' or 'hover', action to trigger animation
		topPos: '100px',                          //position from the top/ use if tabLocation is left or right
		leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
		fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
	});

});

</script>

<STYLE>
<!--
body, html {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background: #00121E url("nabdh_bg.jpg") no-repeat left center fixed;
	-webkit-background-size: cover; /*for webKit*/
	-moz-background-size: cover; /*Mozilla*/
	-o-background-size: cover; /*opera*/
	background-size: cover; /*generic*/
	overflow: hidden;
}
#bg_menu_bar {
	z-index: 3;
	height: 160px;
	margin:0 auto 0 auto;
	width: 100%;
	background-color: #FFF;
	opacity: 1;
}
#left_menu_bar {
	z-index: 4;
	height: 160px;
	margin:0 auto 0 auto;
	width: 1200px;
	background-color: #FFF;
	opacity: 1;
}
.logo{
	width: 329px;
	position: relative;
	margin: 0 0 0 106px;
	z-index:10;
	opacity: 1;
	float:left;
}
.left_menu {
	width: 173px;
	margin: 56px 50px 0 130px;
	text-align: left;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #333;
	float:left;
	position: relative;
}
.middle_menu {
	width: 160px;
	margin: 56px 50px 0 0;
	text-align: left;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #333;
	float:left;
	position: relative;
}
.right_menu {
	width: 160px;
	margin: 56px 0 0 0;
	text-align: left;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color: #333;
	float:left;
	position: relative;
}
a:link {text-decoration:none; color:#FFF;}
a:visited {text-decoration:none; color:#FFF;}
a:hover {text-decoration:none; color:#5283CA;}
a:active {text-decoration:none; color:#FFF;}
#wrapper{
	z-index: 1;
	width: 1051px;
	margin:0 auto 0 auto;
}
#wraphead {
	z-index: 19;
	position: absolute;
	top: 370px;
}
#wrapcontent {
	z-index: 28;
	width: 520px;
	margin: 0 auto 0 auto;
	padding: 15px;
	font-family: Arial, Helvetica, sans-serif;
	min-height: 320px;
	text-align: left;
	color: #FFF;
	position: relative;
	background-color:#333;
	opacity: 0.8;
}
.slide-out-div {
	padding: 20px;
	width: 250px;
	height: 181px;
	font-family: Arial, Helvetica, sans-serif;
	text-align: left;
	color: #FFF;
	position: relative;
	background-color:#000;
	z-index:100;
} 
</style>
</head>

<body>
<div id="bg_menu_bar">
    <div id="left_menu_bar">
	<div class="logo"><img src="nabdh_logo.jpg" alt="NABDH Logo" border="0" /></div>
        <div class="left_menu"><strong>Address</strong><br/>Clocktower Roundabout<br/>Dubai</div>
        <div class="middle_menu"><strong>Telephone</strong><br/>+97155 66812 7346</div>
        <div class="right_menu"><strong>Email</strong><br/>sales@nadbh.ae</div>
    </div>
</div>
<div id="wrapper">	
    <div id="wraphead"><img src="nabdh_solar.png" alt="NABDH Solar Specialists" border="0" /></div>
</div>
</body>
</html>

Development is taking place @
 
my first observation is that I cannot see any element with the class of slide-out-div. so i guess the sliding tab is not yet implemented?

why are you using a (very) old version of jquery?

when you say 'image rotator' can you clarify?
1. do you mean that you want the image to be replaced by another, in rotation?
2. that the images should carousel in a nice animated fashion (not a simple replace)?
3. that the image should physically rotate around an axis (without the image itself changing). e.g. spinning?

if 1 or 2, where are the image urls in the code?
 
The sliding tab has been deactivated, but I just know the client will want it back, so I've left it there for the moment.

Rotating in the fashion that each image needs to fade out as the next fades in. I've been playing with timers with a script that you helped me with some time ago but can't get the damn thing to play. Here's the code that sets the background image to change onClick:

Code:
<!DOCTYPE html >
<head>
    <title>Background Image Changer</title>
    <script type="text/javascript" src="[URL unfurl="true"]http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>[/URL] 
    <style>
    body, html {
        margin: 0px;
        background: #fff url("images/a.jpg") no-repeat center center fixed;
        -webkit-background-size: cover; 
        -moz-background-size: cover; 
        -o-background-size: cover; 
        background-size: cover; 
    }
    </style>
    <script type="text/javascript">
	$(document).ready(function () {
		$('.imgbg').on('click', function(e){
		 e.preventDefault();
		 var i = '';
		 switch ($(this).text()){
		   case "Image 1":
			 i = 'images/a.jpg' ;//url to image file
			 break;
		   case "Image 2":
			 i = 'images/b.jpg' ;//url to image file
			 break;
		   case "Image 3":
			 i = 'images/c.jpg' ;//url to image file
			 break;
		   case "Image 4":
			 i = 'images/d.jpg' ;//url to image file
			 break;
		 }
		 $('body,html').css('background-image', 'url("' + i + '")');
		}); 
	});
    </script>
</head>
<body>
            <h3>Change the images</h3>
            <p><a href="#" class="imgbg">Image 1</a></p>
            <p><a href="#" class="imgbg">Image 2</a></p>
            <p><a href="#" class="imgbg">Image 3</a></p>
            <p><a href="#" class="imgbg">Image 4</a></p>
</body>
</html>

Am I wrong in thinking this may be set to timed update?
 
it should be fine to make that change on a timer.
i'd do something like this

Code:
$[COLOR=#990000]([/color]document[COLOR=#990000]).[/color][b][COLOR=#000000]ready[/color][/b][COLOR=#990000]([/color] [b][COLOR=#0000FF]function[/color][/b] [COLOR=#990000]()[/color][COLOR=#FF0000]{[/color]
  curImage [COLOR=#990000]=[/color] [COLOR=#993399]1[/color][COLOR=#990000];[/color]
  imageRotator [COLOR=#990000]=[/color] [b][COLOR=#000000]setInterval[/color][/b][COLOR=#990000]([/color] [b][COLOR=#000000]changer[/color][/b][COLOR=#990000](),[/color] [COLOR=#993399]2000[/color] [COLOR=#990000]);[/color]
[COLOR=#FF0000]}[/color][COLOR=#990000]);[/color]

[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]changer[/color][/b][COLOR=#990000]()[/color][COLOR=#FF0000]{[/color]
  [b][COLOR=#0000FF]var[/color][/b] images [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]Array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'images/a.jpg'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'images/b.jpg'[/color] [COLOR=#990000],[/color] [COLOR=#FF0000]'images/c.jpg'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'images/d.jpg'[/color] [COLOR=#990000]);[/color]
  curImage [COLOR=#990000]=[/color] curImage [COLOR=#990000]==[/color] [COLOR=#993399]4[/color] [COLOR=#990000]?[/color] [COLOR=#993399]1[/color] [COLOR=#990000]:[/color] curImage [COLOR=#990000]+[/color] [COLOR=#993399]1[/color][COLOR=#990000];[/color]
  $[COLOR=#990000]([/color][COLOR=#FF0000]'body,html'[/color][COLOR=#990000]).[/color][b][COLOR=#000000]css[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'background-image'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'url("'[/color] [COLOR=#990000]+[/color] images[COLOR=#990000][[/color]curImage[COLOR=#990000]][/color] [COLOR=#990000]+[/color] [COLOR=#FF0000]'")'[/color][COLOR=#990000]);[/color]
[COLOR=#FF0000]}[/color]

if you want to fade them in/out then probably you need to use the jQuery UI animate class. But off the top of my head you might be able to use vanilla jquery as follows

Code:
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]changer[/color][/b][COLOR=#990000]()[/color][COLOR=#FF0000]{[/color]
  [b][COLOR=#0000FF]var[/color][/b] images [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]Array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'images/a.jpg'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'images/b.jpg'[/color] [COLOR=#990000],[/color] [COLOR=#FF0000]'images/c.jpg'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'images/d.jpg'[/color] [COLOR=#990000]);[/color]
  curImage [COLOR=#990000]=[/color] curImage [COLOR=#990000]==[/color] [COLOR=#993399]4[/color] [COLOR=#990000]?[/color] [COLOR=#993399]1[/color] [COLOR=#990000]:[/color] curImage [COLOR=#990000]+[/color] [COLOR=#993399]1[/color][COLOR=#990000];[/color]
  $[COLOR=#990000]([/color][COLOR=#FF0000]'body,html'[/color][COLOR=#990000]).[/color][b][COLOR=#000000]animate[/color][/b][COLOR=#990000]([/color]
[tab][tab][COLOR=#FF0000]{[/color]opacity[COLOR=#990000]:[/color] [COLOR=#993399]0[/color][COLOR=#FF0000]}[/color]
[tab][tab][COLOR=#FF0000]{[/color]
[tab][tab]  duration[COLOR=#990000]:[/color] [COLOR=#FF0000]'fast'[/color][COLOR=#990000],[/color]
[tab][tab]  complete[COLOR=#990000]:[/color] [b][COLOR=#0000FF]function[/color][/b] [COLOR=#990000]()[/color][COLOR=#FF0000]{[/color]
[tab][tab][tab]$[COLOR=#990000]([/color][COLOR=#FF0000]'body'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'html'[/color][COLOR=#990000]).[/color][b][COLOR=#000000]css[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'background-image'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'url("'[/color] [COLOR=#990000]+[/color] images[COLOR=#990000][[/color]curImage[COLOR=#990000]][/color] [COLOR=#990000]+[/color] [COLOR=#FF0000]'")'[/color][COLOR=#990000]);[/color]
[tab][tab][tab]$[COLOR=#990000]([/color][COLOR=#FF0000]'body'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'html'[/color][COLOR=#990000]).[/color][b][COLOR=#000000]animate[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]{[/color]opacity[COLOR=#990000]:[/color] [COLOR=#993399]1[/color][COLOR=#FF0000]}[/color][COLOR=#990000],[/color][COLOR=#FF0000]'fast'[/color][COLOR=#990000]);[/color]
[tab][tab]  [COLOR=#FF0000]}[/color]
[tab][tab][COLOR=#FF0000]}[/color][COLOR=#990000]);[/color]
[COLOR=#FF0000]}[/color]

 
Am I missing something? It all seems to make sense (which is probably a first - ha!), but nothing's rendering at all.

Code:
<!DOCTYPE html >
<head>
    <title>Background Image Changer</title>
    <script type="text/javascript" src="[URL unfurl="true"]http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>[/URL] 
    <style>
    body, html {
        margin: 0px;
        background: #fff no-repeat center center fixed;*/
        -webkit-background-size: cover; 
        -moz-background-size: cover; 
        -o-background-size: cover; 
        background-size: cover; 
    }
    </style>
    <script type="text/javascript">
	$(document).ready( function (){
	  curImage = 1;
	  imageRotator = setInterval( changer(), 2000 );
	});
	function changer(){
	  var images = new Array('images/a.jpg', 'images/b.jpg' , 'images/c.jpg', 'images/d.jpg' );
	  curImage = curImage == 4 ? 1 : curImage + 1;
	  $('body,html').animate(
			{opacity: 0}
			{
			  duration: 'fast',
			  complete: function (){
				$('body','html').css('background-image', 'url("' + images[curImage] + '")');
				$('body','html').animate({opacity: 1},'fast');
			  }
			});
	}	
 
    </script>
</head>
<body>
</body>
</html>

Nothing happened with the first example either. Am I being thick(er than usual)?
 
enclose the reference to changer() in quotes. my bad.

Code:
imageRotator = setInterval( [red]"[/red]changer()[red]"[/red], 2000 );
 
Still no go. I've ploughed through the rest of it for syntax, but I have to be honest, I'd have missed that one anyway. It seems to have all the requisite semi-colons and braces. I just wish it was a little more...logical.
 
and there is a comma missing after {opacity: 0}

the hazards of typing directly into the edit box on the site...

also do make sure that the url references are correct!
 
Odd things are a hapnin' now. Text on the page is disappearing, but still no images either showing or therefore changing. URLs are correct though.
 
oops. my code animates the whole body to opacity 0 and then back up. should just animate the background!
 
Is it possible to distinguish that? Isn't the background-image part of the body element? This does my head in!
 
Found another option, but the images need to be the same size as each other, and it messes up when resized. It uses the 100% width element to scale the thing, so it adds a vertical scroller on most screen resolutions and doesn't look too pretty. A lot of people are saying that animating the background can't be done???
 
the background image is part of the body element, yes. however the problem with using body/html is that every other DOM node is also part of those nodes. so fading out the parent node will also fade out all child nodes.

I'm not convinced this is a dead dodo though. if you can supply links to the images you are using I will have a play in a coffee break later on
 
yup, but you are then not animating the background of the body element, but a separate positioned div. no problem there, it's just a different (and much easier) exercised.

instead of the hide show method, consider the opacity animation that I proposed. that should smoothen things out a bit. Also I'm not sure why you'd use a timeout instead of an interval, but I don't suppose it matters a great deal.
 
I'm trying to adapt a script. I'm not very good at it!! This isn't my bag...but there again, neither is it yours really, is it!!? I'd much rather it use the opacity method - much less jumpy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top