Here’s the recipy, follow the first 4 steps if you don’t know JavaScript. If you do, you should read on to the finish, it may save you some grief later.
1st. download these two files and save them in the same folder as your index.html:
* http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js
* http://design-forge.ro/projects/opensource/transBG.jquery.plugin.js
2nd. write these lines into the <head> of your website
* <script type=”text/JavaScript” src=”jquery-1.2.6.min.js”></script>
* <script type=”text/JavaScript” src=”transBG.jquery.plugin.js”></script>
3rd. decide what CSS selectors to use. jQuery uses CSS selectors to ”grab” the object(s) you need.
* I may want to apply the background to a speciffic object reffered to by ID and the CSS selector necesary to find that object will be ”#id”
* I may want to apply the background to a speciffic type of objects reffered to by TAGNAME and the CSS selector necesary to find that array of objects will be ”div”
* I may want to apply the background to a speciffic class of objects reffered to by CLASS and the CSS selector necesary to find that array of objects will be ”.classname”
* jQuery accepts even complex CSS selectors, and also has extra pseudo-clases that CSS does not have, in case you know your CSS well.
4th. write the JavaScript commands in the <head> section, bellow the other <script> tags
<script type="text/JavaScript">
$(document).ready(function()
{
$(”#id”).transBGdraw();
$(”#menu”).transBGdraw();
$(”#container”).transBGdraw();
// !!!IMPORTANT!!!
// if you use tagnames
// or classes, make sure
// every object affected
// has a unique id.
$(”div”).each(function()
{ $(this).transBGdraw(); });
$(”h6”).each(function()
{ $(this).transBGdraw(); });
$(”.class1”).each(function()
{ $(this).transBGdraw(); });
$(”.text”).each(function()
{ $(this).transBGdraw(); });
$(”.information”).each(function()
{ $(this).transBGdraw(); });
});
</script>
5th. All the elements that get a transparent background are included in the class ”.transBG” so if you insert data into your page via AJAX you will need to call $(”.transBG”).each(function() {$(this).transBGredraw();}); to redraw every one of the objects that have a transparent backround, or you could call the redraw function only for the elements that the AJAX affects.
6th. You can modiffy the CSS of the transparent background by calling the method transBGdraw() with an argument like this $(”#myobject”).transBGdraw({’z-index’:’5’, ’background-color’:’#f00’, ’opacity’:’0.20’}); or by adding settings to your CSS for the element that has the id=”myobjectTransBG”.
7th. The transparent background effect is created by adding an object behind the data-object that needs the background. The data-object needs to have an ID so that the ID of the background object cand be set as ”initialID”+”TransBG”.
8th. Known issues: The transparent BG object will be correctly placed behind the desired object only if the object has natural or flowing positioning. If you position your object with absolute positioning and z-index settings you will need to match the z-index setting for the background object.
For those of you looking for another solution to this problem: Another solutions make use of a fully transparent gif file, a one pixel transparent gid picture that is used to filter the transparent areas for the item in need of transparency. It works on all normal browsers except on IE6 which unfortunatly still represents some 20% of browser users world wide. To repair this dillema, the ones who created this solution used an IE6 proprietary (IE6 speciffic) css rule. Which “fixes” the fact that IE6 does not render png files as it shoud by adding an external filter that can render pngs corectly without the filter IE6 makes the png images look non transparent as if a white object was directly behind them). There are only two downsides to using this solution: having to create the semitransparent png files every time you need to discreetly alter the transparency of the object (even by 1%); and not being able to use any background-repeat properties for the object with the transparent background (a flaw of the aforementioned external filter that canot render the png images recursively inside a repeating background object… the filter is called only once). If you find this solution more usefull here’s a link torwards the “png fix” site http://homepage.ntlworld.com/bobosola/ all you must do is set your objects background image to a semi-transparent png file and then follow the instructions given on the png fix site.

28 comments
Comments feed for this article
September 3, 2008 at 9:26 am
barracus
I have an issue in safari. The opacity isn’t applied correctly and the replacing div isn’t as high as it should be. I’ve tried applying the clear hack and even inserting a clearing div, without succes.
September 3, 2008 at 10:23 pm
mihaistancu
It may be a bug in the plugin or a layout problem you could avoid by changing some of the default behaviors the plugin has.
To determine which of them it is, i need to know
1. Which version of Safari? Mac or Windows?
2. If you could please post the code, or send it to me via e-mail?
3. What kind of skills and experience you have? so i can answer your questions as thoroughly as you need me to.
March 23, 2009 at 9:52 am
8 Javascript solutions to common CSS problems
[...] Source: Cross Browser Transparent Background with jQuery, no css hacks, no *.png files [...]
March 23, 2009 at 10:49 pm
separati.st » Bi-Weekly Interesting Shizzle for March 4th - March 7th
[...] Cross Browser Transparent Background with jQuery, no css hacks, no *.png files « Mihaistancu&r… [...]
March 25, 2009 at 1:43 pm
Keith
This looks to be just the thing I need for a design I’m working on, however, I have run into the following issue. No matter how I try to use the plugin, it doesn’t work. Firebug shows the following error in it’s console:
$(this).position() is undefined (transBG.jquery.plugin.js,4)
Any pointers on where to start looking to fix this?
March 25, 2009 at 3:47 pm
mihaistancu
Howdy,
Off the top of my head (since i assume you did not alter the item’s positioning at all) try adding a “position: relative” to your object, it most probably won’t alter your layout at all and it may make the plugin work.
When i wrote this plugin i tried to make it as versatile as i could, still it needs work, it’s only a few lines of code (and i’ve seen plugins that do less work with 10 times the code just to be sure all posibilities are covered).
Explanation to my recommendation:
$(this).position() is an object that holds the “recalculated” position of the target object. That means that when you use this method you are requesting information about an object that has been somehow repositioned, moved from it’s natural place in the layout. If you have not altered the objects positioning in any way, no relative positioning, no absolute positioning, no float rules, then the object may not have the position() method at all. It may be initiated only if the object is moved via CSS or JS.
If this hunch of mine doesn’t hold true please send me as much data as you can so i can diagnose the issue better.
March 25, 2009 at 4:13 pm
Keith
You’re correct in assuming I didn’t alter the positioning. However, adding position: relative didn’t solve the problem. Here’s the code I’ve been testing with to see if I could get it to work at all…
http://pastie.textmate.org/private/yznnljgmtbhbkdknapj5ia
It doesn’t matter if I used position rules or float rules, I get the same error.
If you need to see the site where I’m testing, that can be arranged too…
March 25, 2009 at 8:16 pm
mihaistancu
I solved your problem!
Use either:
$(document).ready(function()
{
$(“#trans”).transBGdraw({‘background-color’:'#00f’, ‘opacity’:'0.20′});
});
Or:
window.onload = function()
{
$(“#trans”).transBGdraw({‘background-color’:'#00f’, ‘opacity’:'0.20′});
}
I will assume that you do not know what those statements represent so i’ll explain them to you.
It stands to reason to not be able to access the positioning coordinates of a yet inexisting object.
The html document must be fully processed and it’s associated css files must also be fully processed in order for an object within that document to become accessible to any third party (such as JavaScript).
When the html/css documents are fully processed, the “document object model” is said to be ready to be manipulated. This fact is signaled by an event being triggered.
After the DOM is ready, image files, audio files, video files, and other elements get painted into the layout in the order in which theyr downloads are finished.
When all adjacent files are downloaded and all of their respective sizes are evaluated and they are placed in the layout, so when the page is entirely ready to pe viewd as intended by the designer, another event is triggered, declaring the “Loaded” state of the current window.
March 26, 2009 at 10:48 am
mihaistancu
I added the $(document).ready() statement to my post so it wouldn’t be misleading.
It can be assumed that if i didn’t mention it in the post, the pluggin handles it but it doesn’t, my bad.
March 26, 2009 at 11:59 am
Keith
Thanks for figuring it out…I’m a relative noob when it comes to jQuery, so I didn’t even think to try it (obviously
).
One thing I did notice in playing around with it though, putting an opacity option in the function call doesn’t seem to change the actual opacity of the created div…if I change the opacity in the script, it works fine.
March 26, 2009 at 1:21 pm
mihaistancu
What i noticed is that jQuery’s css() method does not recognise, IE’s “filter:” property, the one you need to use to set “alpha(opacity=40)”, hence, to make sure it works on IE6 as expected you should create a CSS class for trasBG and set the opacities there.
About changing the opacity by parameter i remember it worked fine, you should try setting it as {“opacity”: “.27″} not as {“opacity”:”0.27″} because as far as i can tell the css method doesn’t modiffy the arguments you give it.
If you say css(“width”,”3px”) it commits the value correctly, if you say css(“width”,”3″) it doesn’t so, you have to use the exact (string) value you would have used in CSS, not an evaluated version.
March 29, 2009 at 10:45 pm
Liam Dilley
You can not use this more then once in a valid Xhtml Strict environment.
It injects a div with an ID and so you can not use it more then once.
To solve it you need to search for the instance Id transBG and reference of # and change it to represent a class.
The thing to note is you need to rename it so it does not conflict with the existing class of the same name as the id.
March 29, 2009 at 10:58 pm
mihaistancu
I’ve been meaning to make that change, but frankly as i made it in a moment when i needed it, and now i don’t it’ll have to wait till i finish up what i’m working on right now.
Hopefully vs.2 will be much better.
April 19, 2009 at 4:34 am
Javascript, jQuery, Tutorials… « PostIT
[...] TRANSPARENT BACKGROUND http://mihaistancu.wordpress.com/2008/08/02/cross-browser-transparent-background-with-jquery-no-css... [...]
April 29, 2009 at 8:32 am
Manuel
Hello!
I’m trying to use this script on WP 2.7 for fixing issues on IE6. I keep getting an error that says something like: “The object doesn’t accept the property or method”.
It actually breaks up other JS if I place it outside the
Any help would be greatly appreciated! Cheers…
April 29, 2009 at 1:06 pm
mihaistancu
did you use $(document).ready(function(){ /* insert the code here */ }); ??
April 30, 2009 at 1:10 am
Manuel
Yes, I copy-pasted the code, corrected the “s and just changed my #id to #mybox …
Any import considerations? Or something else?
April 30, 2009 at 1:28 pm
T. Windsor
When calling:
$(“#whatever”).transBGdraw();
then if #($”whatever”) returns no elements, it leads to
“$(this).position() is undefined” error
I think the script should be robust enough to handle this eventuality.
April 30, 2009 at 4:36 pm
mihaistancu
Good point, i’ll be handling this, thanks
May 6, 2009 at 5:40 pm
Matt Stark
Guys,
I’ve written a plugin to do the same thing but it uses a completely different methodology.
Feel free to give it a shot and let me know what works and what doesn’t
http://mattstark.blogspot.com/2009/05/javascript-jquery-plugin-to-fade.html
I also gave props to this article -> thanks for posting your code.
Over And Out
June 7, 2009 at 6:38 am
Background trasparenti con jQuery « TagTagWeb
[...] Tutorial completo [...]
July 15, 2009 at 11:34 am
lillereven
Hi there!
Got a small change set for you. This applies to redraw:
original:
defaults = {
‘background’:'#000000′,
‘opacity’:'0.37′,
‘-moz-opacity’:'0.37′,
‘filter’:'alpha(opacity=37)’,
‘position’:'absolute’,
‘z-index’:'-1′,
‘height’:$(this).height(),
change:
defaults = {
‘background’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘background’),
‘opacity’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘opacity’),
‘-moz-opacity’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘opacity’),
‘filter’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘filter’),
‘position’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘position’),
‘z-index’:$(‘#’+$(this).attr(‘id’)+’TransBG’).css(‘z-index’),
‘height’:$(this).height(),
This way redraw picks up the values used during initialisation, so your function call changes from transBGredraw({options}) to transBGredraw() if you don’t want to change anything.
July 18, 2009 at 12:09 am
mihaistancu
In added the redraw() in kind of a hurry hence the copy-paste of default values.
Come to think of it the project for which i developed it had those exact defaults and i was using redraw() as is, not redraw({options}).
If you visit Roumania make sure i buy you a beer mate, thanks!
August 4, 2009 at 7:31 pm
Sebastian Poreba
I used this plugin with blueprint css framework. To avoid losing your classess from div’s affected by transBG change:
$(this).attr(‘class’,'transBG’);
to
$(this).addClass(‘transBG’);
November 4, 2009 at 6:11 am
James
Hi,
Great job on the script although I have hit a little snag.
I am trying to use this script on my site that already has a jquery accorion menu. If i apply it it exactly as instructed it works but the menu doesn’t. If I place the script with the accordion script neither works.
Would you know why they are conflicting. Thank you for your help in advance
November 5, 2009 at 4:47 am
James
Hi
I figured out my first problem but know I have a stupid question. I dont want th transparency so “see through” and even when I adjust the opacity through the css on the transBGdraw its still too light. Any suggestions how I can make it more solid??
November 5, 2009 at 7:29 pm
mihaistancu
Hi, sorry i didn’t get to answer the first question, it’s been kinda hectic at work.
Glad you figgured it out in the end.
About the second problem, are you saying that the opacity doesn’t get modiffied or that it’s not enough?
What values did you use?
Can you show me an example?
November 6, 2009 at 4:58 am
James
Hi mate, no probs. Its OK I have gone with another solution but thanks anyway.
Although I did set the value to “opacity:1″ dont know if that should of been 1.0, either way I thought that would make it near solid but it didnt. It was still too opaque. Is this normal?