

图片点击放大,你的网页也能做到!
source link: https://walterlv.github.io/post/create-click-to-zoom-image-for-web-pages.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我经常在博客中插入大图,然而总需要借助浏览器的滚轮缩放功能放大观看实在是不方便。于是我希望做一个点击即放大的功能。
下面就是一张可点击放大的图片,你可以点击试试!当然,我期望的效果是自动对所有博客中的图片生效。
▲ Fluent Design App Header
创建一个用于放图片的 HTML 节点
<div id="image-cover-modal" class="image-cover-modal"> <span class="image-cover-close">×</span> <img id="image-cover-image" class="image-cover-modal-content"> <div id="image-cover-caption"></div> </div>
最外层是容器,里面包含一个关闭按钮,一张图片和一个图片标题。
为图片的 HTML 节点添加 CSS 样式
.image-cover-modal { display: none; position: fixed; z-index: 30; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); } .image-cover-modal-content { margin: auto; display: block; width: 80%; } #image-cover-caption { margin: auto; display: block; text-align: center; color: #ccc; padding: 10px 0; height: 150px; } .image-cover-modal-content, #image-cover-caption { animation-name: zoom; animation-duration: 0.3s; } @keyframes zoom { from {transform:scale(0)} to {transform:scale(1)} } .image-cover-close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 2rem; font-weight: bold; transition: 0.3s; } .image-cover-close:hover, .image-cover-close:focus { color: #bbb; text-decoration: none; cursor: pointer; } @media only screen and (max-width: 45rem){ .image-cover-modal-content { width: 100%; } }
添加放大图片的 JS 脚本
// 获取所需的 DOM 节点。 var modal = document.getElementById('image-cover-modal'); var modalImg = document.getElementById("image-cover-image"); var captionText = document.getElementById("image-cover-caption"); var span = document.getElementsByClassName("image-cover-close")[0]; // 为关闭按钮添加功能(事实上是点击任何地方都关闭)。 modal.onclick = function() { modal.style.display = "none"; } // 遍历页面中的每一张图片,为其添加点击事件,点击放大。 var i; for (i = 0; i < document.images.length; i++) { // Get the image and insert it inside the modal - use its "alt" text as a caption var img = document.images[i]; img.onclick = function(){ modal.style.display = "block"; modalImg.src = this.src; captionText.innerHTML = this.alt; } }
参考资料
本文会经常更新,请阅读原文: https://walterlv.github.io/post/create-click-to-zoom-image-for-web-pages.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 。
Recommend
-
90
一:简介在项目中免不了会遇到,实名认证上传身份证、绑定银行卡等功能。在实际操作中呢,会涉及到上传图片,在页面布局时,可能图片不是一张,考虑到布局的美观等因素,显示图片的位置变得很小,如果想查看上传的图片是否清晰,内容是否完整,可能就需要放大才能...
-
5
业余爱好也能做到顶级的人最可怕池建强青峰之巅,山外之山我的读者里估计很多人读过《黑...
-
12
网页中的图片点击放大效果:Highslide 和 Lightbox 谢益辉 / 2007-09-08 这两个网站是我所看到比较经典的 JS 图片放大效果网站: 前者是在页面内缩放,后者是采用的 “全屏” 效果,我看后者的代码稍微简单一些,就采用了...
-
12
在日常开发过程中,有时候会遇到需要在app中嵌入网页,此时使用WebView实现效果,但在默认情况下是无法点击图片查看大图的,更无法保存图片。本文将就这一系列问题的实现进行说明。 项目的知识点: 加载网页后如何捕捉网页中的图...
-
13
单击随机更换图片 点击按钮就会更换一张图片
-
6
V2EX › 程序员 微信读书网页版如何 模拟 点击 下一页 按钮 ? 975779964 · 9 小时 55...
-
17
如何实现在网页中,移动鼠标点击元素就能获取到相应的代码或元素选择器?如在浏览器中检查相应元素代码的效果,在网页中获取元素的对应标签,像八爪鱼采集器在网页中选取元素的效果 [视频]
-
20
纯CSS图片层叠点击展开
-
6
使Simple主题支持图片点击放大 在使用 hexo 主题模版 hexo-theme-simple99 的时候,发现该模版并不支持数学公式的渲染,随即动手开始改造改模版使其支持图片点击放大。 为了...
-
7
1|0需求提供一张2d编辑完成的户型图片,图片中有多个房间、尺寸比例不固定、还有2d户型图数据(墙体顶点坐标、漫游点坐标等),需要做到将2d编辑时提供的漫游点坐标映射到图片上,以做到用户点击图片某个位置时...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK