One of the reasons why you might want to develop your own lightbox is to cut down on the usage of third party libraries. This might be because you want to cut down on the page request or because you want more control on customization.
What is a LightBox
A lightbox is a JavaScript technique to show modal dialogs which can host images and other content. These dialogs usually take center screen and positions themselves above all other content. Usually the background content is made to appear less focused. Modal dialogs help to bring a particular content to the users attention. In this article I will explain how to develop a lightbox that can enlarge a thumbnail image and display it in a modal dialog.Before we get started with code, lets take a moment to understand the approach. We start with the page load event "window.onload". When this event is triggered, we scan the HTML document and look for all image elements. When an image element is found, we attach an onclick event to the element, which when clicked will display a modal dialog. Using this approach means you don't need to assign an onclick event for each thumbnail image you want to enlarge in a lightbox.
The next step involves dynamically inserting a new element in the "Body" of the document when the thumbnail image is clicked. This element will host the lightbox modal dialog. The appearance of this element will be that it is of fixed size and will consume the entire page. Applying a semi transparent background will give it the appearance of a modal dialog. Clicking anywhere on the modal dialog should close the dialog and remove the newly created element from the document.
It's time we looked at some code, let's start with the window.onload event. As mentioned when the window is loaded, we need to attach an onclick event to every image element. However, there maybe some images on the page such as icons, that don't need to appear in a lightbox. To solve this problem, we can use the role attribute and give it a value. When we scan the document and look for image elements, we only assign an onclick event to those image elements that have a role attribute with a certain value.
- window.onload = function(){
- var elements = document.getElementsByTagName("img");
- var count = elements.length;
- for(var i=0; i < count; i++){
- var role = elements[i].getAttribute("role");
- if(role === "lightbox"){
- // Attche event here
- }
- }
- }
- elements[i].addEventListener("click", function(){
- var body = document.getElementsByTagName("body")[0];
- var modal = document.createElement("div");
- modal.style.position = "fixed";
- modal.style.top = "0px";
- modal.style.right = "0px";
- modal.style.bottom = "0px";
- modal.style.left = "0px";
- modal.style.backgroundColor = "rgba(0, 0, 0, 0.6)";
- modal.addEventListener("click", function(){
- this.remove();
- });
- modal.innerHTML = '<table width="100%" height="100%"><tr><td valign="middle" align="center"><img src="' + this.src + '" class="lightbox_popup" /><td></tr></table>';
- document.body.insertBefore(modal, document.body.firstChild);
- });
Download
No comments:
Post a Comment