Home > Article > Web Front-end > How to achieve click-to-enlarge image using pure css
Pure CSS method to achieve click-to-enlarge images: 1. Create an HTML sample file; 2. Set "
"; 3. Use the "function showBgImg(e) {...}" method to achieve the effect of clicking on the image to enlarge it .
The operating environment of this tutorial: Windows 10 system, HTML5&&CSS3 version, DELL G3 computer
How to use pure css to click on the image to enlarge it?
Css actual training picture, click to enlarge
This is a very common function, usually displayed on the website It's a thumbnail. After you click on the thumbnail, the enlarged picture will be displayed in a pop-up box
So how is this function implemented? I just learned the basic knowledge of CSS, and now I can do the actual thing Operation
First split the structure of the page:
According to the above description, we first implement a basic version, first write HTML
<body> <!-- 先来实现弹窗 --> <div class='modal' id='modal'> <img id='bgImg' / alt="How to achieve click-to-enlarge image using pure css" > </div> <!-- 下面则是主页内容,先只给几个图片 --> <div> <img class='thum-img' src='http://f.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266cdb2ba16a718972bd4073612.jpg' /> </div> </body>
Then add the corresponding style, which requires the modal to be hidden by default, so as follows (in order to better distinguish the pop-up window, the background color and border are added)
<style> .modal { display: none; margin: auto; width: 80%; height: 80%; background-color: rgb(0, 0, 0, 0.89); z-index: 1; border: 1px solid rgb(255,255,255,1); } .modal>img { display: block; margin: auto; padding: 10%; max-width: 60%; max-height: 60%; } .thum-img { width: 200px; height: 200px; margin: auto; display: block; padding: 40px; } </style>
The next step is to click on the logic of displaying the large image, which is implemented with the help of js.
<script> var modal = document.getElementById('modal'); var bgImg = document.getElementById('bgImg'); var thumImg = document.getElementById('thumImg'); thumImg.onclick = function() { modal.style.display = 'block'; bgImg.src = this.src; } bgImg.onclick = function() { modal.style.display = 'none'; } </script>
After assembling the above implementation into an html, test it directly and view it. The demonstration effect is as follows
Although the above has achieved the results we expected, there are a few points that we are not satisfied with.
First of all, if you want it to be a real pop-up window without affecting the existing layout, you usually do it by setting the position, as we do You can add another layer outside the modal to become
<div style='position:fixed'> <div class='modal' id='modal'> <img id='bgImg' / alt="How to achieve click-to-enlarge image using pure css" > </div> </div>
The second is that the style of the pop-up window is too ugly. We can use the border shadow we learned before to achieve a beautiful pop-up effect,
The modified css is as follows
.modal { display: none; margin: auto; padding-top: 5%; width: 50%; height: 80%; z-index: 1; background-color: white; } .modal img { display: block; padding: 10px; margin: auto; max-width: 100%; max-height: 100%; box-shadow: 0 2px 6px rgb(0, 0, 0, 0.2), 0 10px 20px rgb(0, 0, 0, 0.2); border-radius: 12px; border: 1px solid white; }
Next consider adding animation and a magnification effect
@keyframes zoom { from {transform: scale(0.1)} to {transform: scale(1)} } .modal img { animation-name: zoom; animation-duration: 0.6s; }
Then let’s look at the demonstration effect as follows
The next step is to turn this into a universal solution to support multiple images. This is mainly a modification of the image click event. Just make some changes to the hard-coded parts above
IV. Source code
Finally give all the source code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小灰灰css学习笔记</title> <style> #modal { display: none; } .modal { margin: auto; padding-top: 5%; width: 50%; height: 80%; z-index: 1; } .modal img { animation-name: zoom; animation-duration: 0.6s; display: block; padding: 10px; margin: auto; max-width: 100%; max-height: 100%; box-shadow: 0 2px 6px rgb(0, 0, 0, 0.2), 0 10px 20px rgb(0, 0, 0, 0.2); border-radius: 12px; border: 1px solid white; } @keyframes zoom { from {transform: scale(0.1)} to {transform: scale(1)} } .thum-img { float: left; width: 200px; height: 200px; margin: auto; display: block; padding: 40px; } </style> </head> <body> <!-- 先来实现弹窗 --> <div style='position:fixed;width:100%;height:100%;background-color:rgb(0,0,0,0.65)' id='modal'> <div class='modal' id='modalw'> <img id='bgImg' / alt="How to achieve click-to-enlarge image using pure css" > </div> </div> <!-- 下面则是主页内容,先只给几个图片 --> <div> <img onclick='showBgImg(this)' class='thum-img' src='http://f.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266cdb2ba16a718972bd4073612.jpg' /> <img class='thum-img' src='http://a.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef5e886d03f1faaf51f3de666d.jpg' onclick='showBgImg(this)'/ alt="How to achieve click-to-enlarge image using pure css" > <img class='thum-img' src='http://g.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcb747b4a5cb2096b63f624a845.jpg' onclick='showBgImg(this)'/ alt="How to achieve click-to-enlarge image using pure css" > <img class='thum-img' src='http://c.hiphotos.baidu.com/image/pic/item/b21c8701a18b87d6657856e70c0828381f30fd14.jpg' onclick='showBgImg(this)'/ alt="How to achieve click-to-enlarge image using pure css" > <img class='thum-img' src='https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png' onclick='showBgImg(this)'/ alt="How to achieve click-to-enlarge image using pure css" > </div> <script> var modal = document.getElementById('modal'); var bgImg = document.getElementById('bgImg'); function showBgImg(e) { modal.style.display = 'block'; bgImg.src = e.src; } bgImg.onclick = function() { modal.style.display = 'none'; } </script> </body> </html>recommends learning: "css video tutorial"
The above is the detailed content of How to achieve click-to-enlarge image using pure css. For more information, please follow other related articles on the PHP Chinese website!