原理:点击图片蒙版层出现,显示图片大图,再次点击蒙版层隐藏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>原生js点击图片放大效果简单实现</title> <meta name="renderer" content="webkit"> <style> .imgPreview { display: none; top: 0; width: 100%; height: 100%; position: fixed; background: rgba(0, 0, 0, 0.5); } .imgPreview img { z-index: 100; width: 90%; height: auto; position: fixed; top: 50%; transform: translate(-50%, -50%); left: 50%; } </style> </head> <body> <div> <img src="https://cdn.jsdelivr.net/gh/wuzhiguang1/bolgfile@2.0/photo/4ed4255c5172b148a5780260e48ee01a--59873749.jpg" class="img" style="width: 300px;">
</div> <div class="imgPreview"> <img src="#" alt="" id="imgPreview"> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> <script> $('.img').on('click', function () { var src = $(this).attr('src'); $('.imgPreview img').attr('src', src); $('.imgPreview').show() }); $('.imgPreview').on('click', function () { $('.imgPreview').hide() }); </script> </body> </html>
|
效果图如下: