纯Javascript实现图片点击弹出_html/css_WEB-ITnose
一直想给安装一个缩略图点击弹出的插件,但是找了找几乎都是用的php来做的,插件的使用和安装极其繁琐,于是上网查了些demo,自己实现了一个纯js的图片弹出插件。
实现的思路是通过编写hook图片的onclick事件的函数,在函数中对body追加div元素,再将传入的图片对象放入元素中,同时再监听div的onclilck事件,当捕捉到点击,再关闭(其实是隐藏)弹出的div。
通过在函数初始化的时候收集页面所有的img元素,再为每个img元素增加onclick="picHook(this)"这条属性,这样当图片在被点击时,这个函数就能自动创建div蒙板背景,并获取被点击图片的宽度和高度,同时生成一个新的和图片一样大小的div来显示图片。当蒙板再次被点击时,hook事件再次响应,并将蒙板和图片div的style置为none,弹出的图片就被关闭了。
实现效果见本博客,任意点击一个图片即可查看效果。
说起来很简单,做起来就更简单了,简单到只需要一个函数即可实现。
talking is cheap,show you my code:
<script>function picHook(pic){ /*图片对象*/ var imgs = document.getElementsByTagName("img"); /*前景div*/ var light = document.getElementById('light') || document.createElement("div"); /*背景div*/ var bg = document.getElementById('bg') || document.createElement("div"); /*图片放大*/ var s_pic = document.getElementById('s_pic') || document.createElement("img"); /*css对象*/ var css = document.createElement("style"); /*css样式*/ var csstext = '\.pic_bg{\ position: absolute;\ margin:0 auto; \ top: 0%;\ left: 0%;\ width: 100%;\ padding-bottom: 1000%;\ background-color: black;\ z-index:1001;\ opacity:.80;\ filter: alpha(opacity=80);\ overflow:scroll;\}\.pic_div {\ margin-bottom: auto;\ position: fixed;\ left:50%;\ top:50%;\ margin-left:-250px;\ margin-top:-100px;\ z-index:1002;\}'; /*收集页面所有图片对象*/ for(i=0; i<img s.length;i++){ imgs[i].setAttribute("onclick", "picHook(this)" ); } css.type = "text/css"; /*关闭图像*/ if( !pic ){ bg.style.display = light.style.display = "none"; } /*ie兼容*/ if(css.styleSheet){ css.styleSheet.cssText = csstext; }else{ css.appendChild(document.createTextNode(csstext)); } s_pic.setAttribute("id", "s_pic"); s_pic.setAttribute("src", pic.src); light.setAttribute("id", "light"); light.setAttribute("class", "pic_div"); light.style.display = 'block'; light.appendChild(s_pic); bg.setAttribute("id", "bg"); bg.setAttribute("class", "pic_bg"); bg.setAttribute("onclick", "picHook()"); bg.style.display = light.style.display; document.getElementsByTagName("head")[0].appendChild(css); document.body.appendChild(bg); document.body.appendChild(light);}</script alt="纯Javascript实现图片点击弹出_html/css_WEB-ITnose" >
将这段代码保存在页面的head中,再将body的onload事件绑定到picHook()函数,你的页面中就也可以实现图片点击弹出大图啦。
还存在一点小bug,主要是因为我不太熟悉css,导致div的样式做的有点难看。
css的样式我是直接声明在js里的,这样就不用再另外创建css文件了。
强迫症,没办法。
等有时间再琢磨琢磨css,优化下样式。
喜欢就拿去吧。
你可以点击下面的图片测试一下看看效果:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex
