hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Design and Content : Web Design and Content Tutorials : Changing page background with css & javascript
Reply

Web Design and Content Tutorials Tutorials on web design and content.
Forum Jump

Changing page background with css & javascript

Reply Post New Thread In Web Design and Content Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2005, 10:49 PM
ub3r ub3r is offline
Disabled
 
Join Date: Dec 2002
Location: chica go go
Posts: 11,858

Changing page background with css & javascript


Earlier today, i was working on a project of mine, and i came up with an idea.

This idea would change <body>'s css driven background style, when an element like <a> or <h1> was moused over.

Using the following code, i was able to accomplish the task, and it even turned out to Validate Perfectly, according to xhtml 1.0 strict standards.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title>TheWebsite</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<style type="text/css">
		#body{ background-image:URL(kerry.jpg); background-repeat: no-repeat; background-position: center; background-color:#777; font-family: verdana, arial, helvetica, sans-serif;}
		H1{ margin: 0px; font-size: 50pt; font-weight: bold; bottom: 0; right: 0; position: absolute;}
		#body2{ background-image:URL(marshmallowman.jpg); background-repeat: no-repeat; background-position: center; background-color:#777; font-family: verdana, arial, helvetica, sans-serif;}
	</style>
</head>
<body id="body">
	<h1 onmouseover="body.id='body2'" onmouseout="body.id='body'">TheWebsite</h1>


</body>
</html>
Let's go over the code that were key in accomplishign this task...

Code:
#body{ background-image:URL(kerry.jpg); background-repeat: no-repeat; background-position: center; background-color:#777; font-family: verdana, arial, helvetica, sans-serif;}
this is our first body css element. It uses the background image kerry.jpg, the background image isn't repeated, and the background is aligned right smack dab in the middle of the page. In addition to that, we have assigned the background color #777 which is a darkish gray. Also, we have chose the font family of verdana, arial, helvetica, and sans-serif.

Code:
H1{ margin: 0px; font-size: 50pt; font-weight: bold; bottom: 0; right: 0; position: absolute;}
This is the style element for our text. Margins are removed from the element as to not carry the margins that are typical in the h1 element. Font size is 50 points, font weight is bold, and the text is aligned at the very bottom of the page, in the most right corner of the page. Also, we chose to position this element using absolute positioning. This is done so that the elements acts on it's own, without being influenced by any other elements (eg: body) .

Code:
#body2{ background-image:URL(marshmallowman.jpg); background-repeat: no-repeat; background-position: center; background-color:#777; font-family: verdana, arial, helvetica, sans-serif;}
body2 is our second body element. It holds all the same style elements as #body, with a single change - this change being that the file marshmallowman.jpg is called, instead of the original - kerry.jpg .

Code:
<body id="body">
body element has to use the id body, in order to hold the style of our #body css element.

Code:
<h1 onmouseover="body.id='body2'" onmouseout="body.id='body'">TheWebsite</h1>
Next is our javascript. we use the javascript handlers - onmouseover, and onmouseout to control what happens on mouse over of the element, and on mouse out of the element. With the first javascritp command, we use onmouseover="body.id='body2'" to change the <body> element's id to body2 on mouseover. and we use onmouseout="body.id='body'" to change the elements id back to body, on mouseout.

Take a look at the result, in the attached file.

Reply With Quote


Sponsored Links
  #2  
Old 04-03-2005, 12:49 PM
Krayis Krayis is offline
Newbie
 
Join Date: Mar 2005
Posts: 6
Good tutorial. Is this like the hotmail.com thing they have somewhat?

Reply With Quote
  #3  
Old 04-03-2005, 01:05 PM
Krayis Krayis is offline
Newbie
 
Join Date: Mar 2005
Posts: 6
Where is the attached file?

Reply With Quote
Sponsored Links
Reply

Related posts from TheWhir.com
Title Type Date Posted
CloudFlare Railgun Offers Hosting Customers One-Click Dynamic Caching Web Hosting News 2013-02-27 10:44:29
cPanel Conference 2012: Everything You Need to Know About New TLDs with Chris Sheridan Web Hosting News 2012-10-10 16:17:54
Software Developer Softaculous Launches Softaculous Auto Installer Version 3.8 Web Hosting News 2011-11-25 15:34:49
Microsoft Launches First Beta of Microsoft WebMatrix 2.0 Web Hosting News 2011-09-26 15:43:24
A Game Changer - An M&A Transaction Blog 2011-07-20 14:25:09


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?