基于javascript代码实现通过点击图片显示原图片
2015-11-29 00:00
821 查看
废话不多说了,直接给大家贴js实现点击图片显示原图片的代码,具体代码如下所示:
下面分享一段关于js实现上传图片及时预览
JS限制上传图片大小不使用控件在本地实现
JSP上传图片产生 java.io.IOException: Stream closed异常解决方法
jsp中实现上传图片即时显示效果功能
js实现上传图片之上传前预览图片
关于jsp版ueditor1.2.5的部分问题解决(上传图片失败)
上传图片js判断图片尺寸和格式兼容IE
上传图片预览JS脚本 Input file图片预览的实现示例
使用JS+plupload直接批量上传图片到又拍云
轻松创建nodejs服务器(10):处理上传图片
function DrawImage(ImgD){ var image = new Image(); image.src=ImgD.src; var width = $(ImgD).attr("width"); var height = $(ImgD).attr("height"); if(width >100 && height>80){ ImgD.width=100; ImgD.height=80; ImgD.alt=image.width+"×"+image.height; }else{ if(image.width>0 && image.height>0){ flag=true; if(image.width>300 || image.height>200){ ImgD.width=image.width/2; ImgD.height=image.height/2; ImgD.alt=image.width+"×"+image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; ImgD.alt=image.width+"×"+image.height; } } } }
下面分享一段关于js实现上传图片及时预览
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>图片上传本地预览</title> <style type="text/css"> #preview{width:260px;height:190px;border:1px solid #000;overflow:hidden;} #imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);} </style> <script type="text/javascript"> //图片上传预览 IE是用了滤镜。 function previewImage(file) { var MAXWIDTH = 260; var MAXHEIGHT = 180; var div = document.getElementById('preview'); if (file.files && file.files[0]) { div.innerHTML ='<img id=imghead>'; var img = document.getElementById('imghead'); img.onload = function(){ var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); img.width = rect.width; img.height = rect.height; // img.style.marginLeft = rect.left+'px'; img.style.marginTop = rect.top+'px'; } var reader = new FileReader(); reader.onload = function(evt){img.src = evt.target.result;} reader.readAsDataURL(file.files[0]); } else //兼容IE { var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="'; file.select(); var src = document.selection.createRange().text; div.innerHTML = '<img id=imghead>'; var img = document.getElementById('imghead'); img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src; var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight); status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height); div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;"+sFilter+src+"\"'></div>"; } } function clacImgZoomParam( maxWidth, maxHeight, width, height ){ var param = {top:0, left:0, width:width, height:height}; if( width>maxWidth || height>maxHeight ) { rateWidth = width / maxWidth; rateHeight = height / maxHeight; if( rateWidth > rateHeight ) { param.width = maxWidth; param.height = Math.round(height / rateWidth); }else { param.width = Math.round(width / rateHeight); param.height = maxHeight; } } param.left = Math.round((maxWidth - param.width) / 2); param.top = Math.round((maxHeight - param.height) / 2); return param; } </script> </head> <body> <div id="preview"> <img id="imghead" width=100 height=100 border=0 src='<%=request.getContextPath()%>/images/defaul.jpg'> </div> <input type="file" onchange="previewImage(this)" /> </body> </html>
您可能感兴趣的文章:
JS上传图片前的限制包括(jpg jpg gif及大小高宽)等JS限制上传图片大小不使用控件在本地实现
JSP上传图片产生 java.io.IOException: Stream closed异常解决方法
jsp中实现上传图片即时显示效果功能
js实现上传图片之上传前预览图片
关于jsp版ueditor1.2.5的部分问题解决(上传图片失败)
上传图片js判断图片尺寸和格式兼容IE
上传图片预览JS脚本 Input file图片预览的实现示例
使用JS+plupload直接批量上传图片到又拍云
轻松创建nodejs服务器(10):处理上传图片
相关文章推荐
- javascript省市区三级联动下拉框菜单实例演示
- JavaScript页面加载执行多个函数
- javascript权威指南学习笔记(一)
- js操作表格的属性
- Visual Studio Code For Mac 支持 ejs 模版视窗编辑识别为html
- 【JavaScript】js操作本地文件
- 【南大软院大神养成计划:第十二天&第十三天】JS,进阶的巨人(二)
- 南大软院大神养成计划--JS
- javascript
- js解析json
- Modernizr.js入门指南
- 【JavaScript】开发谷歌浏览器插件
- 表单开发的一些细节技巧(jsp+js)
- JS贪吃蛇游戏
- 神之门V8(2):GC的混乱之治(下)
- JsonConfig的jsonConfig.setExcludes的用法
- 一个朋友js图表开发遇到的问题 解决思路c和js
- ECMAScript定义类或对象
- #学习笔记#(1)JavaScript动态时间
- C# Winform处理Json数据