"; 3. Use the "function showBgImg(e) {...}" method to achieve the effect of clicking on the image to enlarge it."/> "; 3. Use the "function showBgImg(e) {...}" method to achieve the effect of clicking on the image to enlarge it.">

Home  >  Article  >  Web Front-end  >  How to achieve click-to-enlarge image using pure css

How to achieve click-to-enlarge image using pure css

藏色散人
藏色散人Original
2023-01-28 14:40:042326browse

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 .

How to achieve click-to-enlarge image using pure css

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

I. Background

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

1. Idea

First split the structure of the page:

  • There is a pop-up window that displays a large image in the pop-up window; and The pop-up window is hidden by default
  • You can place many pictures on the main page and add click events
  • After clicking, the pop-up window displays and displays the big picture
  • Click on the big picture After the following, close the pop-up window

II. Implementation

According to the above description, we first implement a basic version, first write HTML

<body>

<!-- 先来实现弹窗 -->
<div class=&#39;modal&#39; id=&#39;modal&#39;>
    <img  id=&#39;bgImg&#39; / alt="How to achieve click-to-enlarge image using pure css" >
</div>


<!-- 下面则是主页内容,先只给几个图片 -->

<div>
    <img class=&#39;thum-img&#39; 
    src=&#39;http://f.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266cdb2ba16a718972bd4073612.jpg&#39; />
</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(&#39;modal&#39;);
    var bgImg = document.getElementById(&#39;bgImg&#39;);
    var thumImg = document.getElementById(&#39;thumImg&#39;);
    thumImg.onclick = function() {
        modal.style.display = &#39;block&#39;;
        bgImg.src = this.src;
    }

    bgImg.onclick = function() {
        modal.style.display = &#39;none&#39;;
    }
</script>

After assembling the above implementation into an html, test it directly and view it. The demonstration effect is as follows

How to achieve click-to-enlarge image using pure css

Although the above has achieved the results we expected, there are a few points that we are not satisfied with.

  • is not the pop-up effect we expected. The original The picture is squeezed down
  • It would be better if there is an enlarged animation effect in the pop-up window (just can use the animation learned before)
  • When there are many pictures in the picture, How to click to enlarge

III. Advanced

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=&#39;position:fixed&#39;>
    <div class=&#39;modal&#39; id=&#39;modal&#39;>
        <img  id=&#39;bgImg&#39; / 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,

  • Change to fill the background of all images
  • Remove the background color, add a shadow, and add a white border

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

How to achieve click-to-enlarge image using pure css

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

How to achieve click-to-enlarge image using pure css

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=&#39;position:fixed;width:100%;height:100%;background-color:rgb(0,0,0,0.65)&#39; id=&#39;modal&#39;>
<div class=&#39;modal&#39; id=&#39;modalw&#39;>
    <img  id=&#39;bgImg&#39; / alt="How to achieve click-to-enlarge image using pure css" >
</div>
</div>


<!-- 下面则是主页内容,先只给几个图片 -->

<div>
    <img onclick=&#39;showBgImg(this)&#39; class=&#39;thum-img&#39; 
    src=&#39;http://f.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266cdb2ba16a718972bd4073612.jpg&#39; />
 	<img  class=&#39;thum-img&#39; src=&#39;http://a.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef5e886d03f1faaf51f3de666d.jpg&#39; onclick=&#39;showBgImg(this)&#39;/ alt="How to achieve click-to-enlarge image using pure css" > 
    <img  class=&#39;thum-img&#39; src=&#39;http://g.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcb747b4a5cb2096b63f624a845.jpg&#39; onclick=&#39;showBgImg(this)&#39;/ alt="How to achieve click-to-enlarge image using pure css" >
    <img  class=&#39;thum-img&#39; src=&#39;http://c.hiphotos.baidu.com/image/pic/item/b21c8701a18b87d6657856e70c0828381f30fd14.jpg&#39; onclick=&#39;showBgImg(this)&#39;/ alt="How to achieve click-to-enlarge image using pure css" >
    <img  class=&#39;thum-img&#39; src=&#39;https://raw.githubusercontent.com/liuyueyi/Source/master/img/info/blogInfoV2.png&#39; onclick=&#39;showBgImg(this)&#39;/ alt="How to achieve click-to-enlarge image using pure css" >
</div>

<script>
    var modal = document.getElementById(&#39;modal&#39;);
    var bgImg = document.getElementById(&#39;bgImg&#39;);


    function showBgImg(e) {
		modal.style.display = &#39;block&#39;;
		bgImg.src = e.src;
	}

	bgImg.onclick = function() {
		modal.style.display = &#39;none&#39;;
	}
</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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn