Trackbacks
- No Trackbacks
- create light-box with jQuery & CSS | blogfreakz.com
As you already know that I’ve been working on improving the performance of my website and I needed a simple solution to create light-box effect for Live-Chat on this website. I had many options to choose from available jQuery plug-ins however, the idea was to optimize the code with minimal use of heavy third-party scripts and CSS. Moreover, Only thing I needed was a light-box effect without any other functionality.
So I created the light-box effect with a few lines of code using CSS and jQuery.
Let’s start coding
Place this code within <body></body> tags where ever you like.
<a id="show-panel" href="#">Show Panel</a> <div id="lightbox-panel"> <h2>Lightbox Panel</h2> <p>You can add any valid content here.</p> <p align="center"> <a id="close-panel" href="#">Close this window</a> </p> </div><!-- /lightbox-panel --> <div id="lightbox"> </div><!-- /lightbox -->
First line of the above code is a link with id “show-panel” which will display the “light-box” and the “lightbox-panel” DIVs, similarly on line 7 we have a link with ID “close-panel” to hide these DIVs. This will be handled by jQuery of course.
#lightbox-panel will hold the content to be dispalyed and #lightbox will add a transparent background behind our content panel. Let’s write some CSS code to stylize these two DIVs before we add the functionality to our links with jQuery.
You can add this code within the document’s <head></head> tag or in any linked style sheet.
* /Lightbox background */
#lightbox {
display:none;
background:#000000;
opacity:0.9;
filter:alpha(opacity=90);
position:absolute;
top:0px;
left:0px;
min-width:100%;
min-height:100%;
z-index:1000;
}
/* Lightbox panel with some content */
#lightbox-panel {
display:none;
position:fixed;
top:100px;
left:50%;
margin-left:-200px;
width:400px;
background:#FFFFFF;
padding:10px 15px 10px 15px;
border:2px solid #CCCCCC;
z-index:1001;
}
Note: The z-index value for #lightbox-panel should be greater than z-index value of #lightbox to display it above the transparent background and both should have the property display as none so they should not show up by default or if the users have Javascript disabled in their browsers.
Let’s put some life to our code with jQuery.
You can add this code within the document’s <head></head> tag and we are done.
<script type="text/javascript" src="PATH_TO/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a#show-panel").click(function(){
$("#lightbox, #lightbox-panel").fadeIn(300);
})
$("a#close-panel").click(function(){
$("#lightbox, #lightbox-panel").fadeOut(300);
})
})
</script>
Could it get easier than this?
Once you click the link with ID “show-panel” it will display both the DIVs with some nice fade effect and if you click the link with ID “close-panel” it will hide these DIVs.
I hope you enjoyed this little trick to create simple light-box effect with CSS and jQuery and You are most welcome to share your inputs and code in the comments below.
please i wanna ask about PATH_TO/jquery.min.js javascript file . how can i downloaded to test the lightwidow in my system
Hey,
Great little script. What happens with the “lightbox” div if the page height is greater than the clientHeight.
It would mean you could effectively scroll down and pass the faded lightbox div.
Does anyone know a fix for this? I have tried to change the containers height to match the clientHeight when the lightbox is activated but this doesn’t work great in IE browers
I changed the “lightbox” div css position from “absolute” to “fixed” and the height problem went away in Firefox, haven’t tested in other browsers yet.
Hey buddy ! It looks cool ! and very handy too … keep it up …
Great work!!! thank you!!
I have creat a php form to sign up to a newsletter, but I dont kno how I can make test validation on required fields??
thanks
Sorry for my bad english
Perfect! I like the effect where it darkens everything in the background.
By the way would you mind implementing this as a WordPress plug-in? It’d be a lot easier for those who doesn’t wanna touch a single coding.
Thanks.
How can I close the box when I click on the empty space of the webpage?
Hi it doesn’t seem to work for me. When I clicked the link, nothing happens. I have added all the codes exactly you have given, but no changes.
great work!!!, bt i can with problem, when i run this on Internet Explore 6 it is not working, can u fix this, thanks…..
Hi there,
Wonder if you can put up a script to display another page (like iframe) but via ajax?
Thanks.
That is awesome, you barley written any code with any of the languages. This is easily the smallest coding script to get a lightbox.
Very nice tut man i just used it
Hello. you can open the window when the page loads rather than clicking?
Sorry for my bad english!!
Oh thats great, thanks for the tutorial
Pretty easy and cool, but can we include any pages that embed flash in the content of the light box panel?!
04 Mar, 2010
Brilliant! Just what I was looking for
Now, how do you get the lightbox to appear in the middle of the page? I have a link off the bottom of a page and when clicked it takes you back to the top before showing the lightbox, i need the page to stay still! Mike.