Home >Web Front-end >JS Tutorial >How to use JavaScript to achieve image lightbox effect?
How to use JavaScript to achieve the image lightbox effect?
With the development of social media and web design, the picture lightbox effect has become one of the common interactive special effects in many websites. Image lightbox is an effect that displays an enlarged image in the center of the screen by clicking on it. It not only improves the user experience, but also better displays the details of the image. In this article, we will learn how to use JavaScript to implement a simple image lightbox effect.
First, we need an HTML page to display images. Below is a basic HTML structure.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图片灯箱</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <img src="image1.jpg" alt="Image 1" class="lightbox"> <img src="image2.jpg" alt="Image 2" class="lightbox"> <img src="image3.jpg" alt="Image 3" class="lightbox"> </div> <div id="lightbox" class="hidden"> <img src="" alt="Lightbox Image" id="lightbox-image"> <span id="close-button">X</span> </div> <script src="script.js"></script> </body> </html>
In the above code, we created a container containing three pictures and added a class of "lightbox" to each picture. We also create a hidden div to display the enlarged image, with the id "lightbox". In this div, we added an empty image and a close button.
Next, we need to create some CSS styles to achieve the effect of the image lightbox. Create a file named style.css and add the following styles:
.container { display: flex; } .lightbox { cursor: pointer; width: 200px; height: 200px; } #lightbox { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); display: flex; justify-content: center; align-items: center; flex-direction: column; text-align: center; z-index: 9999; } #lightbox img { width: 80%; max-height: 80%; margin-bottom: 20px; } #close-button { color: #fff; font-size: 30px; cursor: pointer; }
In the above CSS style, we set the style of the container and image so that they can be displayed in the form of a grid on the page . We also define a style called "lightbox" that is used to adjust the layout and background color when the image is enlarged. Finally, we set the style for the enlarged image and the style for the close button.
Now, we can start writing JavaScript code. Create a file named script.js in the same directory as the HTML file and add the following code:
// 获取所有的图片元素 var lightboxImages = document.getElementsByClassName("lightbox"); // 获取放大图片和关闭按钮元素 var lightbox = document.getElementById("lightbox"); var lightboxImage = document.getElementById("lightbox-image"); var closeButton = document.getElementById("close-button"); // 绑定点击事件,显示放大图片 for (var i = 0; i < lightboxImages.length; i++) { lightboxImages[i].addEventListener("click", function() { var imageSource = this.getAttribute("src"); lightboxImage.setAttribute("src", imageSource); lightbox.classList.remove("hidden"); }); } // 绑定点击事件,隐藏放大图片 closeButton.addEventListener("click", function() { lightbox.classList.add("hidden"); });
In the above JavaScript code, we first get all the files through the getElementsByClassName
method Image element with "lightbox" class. Next, we get the elements for the zoom image and the close button. Then, we use a loop to bind a click event to each image. When the user clicks on the image, the source address of the image is set to the address of the enlarged image, and the hidden style of the enlarged image is removed. Finally, we bind the click event of the close button and add a hidden style to enlarge the image when the user clicks the close button.
Now, we can open the HTML file in the browser, click on the image, and see the effect of the image being enlarged and displayed in the center of the page. When the close button is clicked, the enlarged image will be hidden.
The above is the sample code for using JavaScript to achieve the image lightbox effect. You can modify and expand it according to your needs to create more complex and diverse picture lightbox effects. I wish you success in achieving this!
The above is the detailed content of How to use JavaScript to achieve image lightbox effect?. For more information, please follow other related articles on the PHP Chinese website!