惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - ㊣鑫哥

jQuery插件---获取URL参数. 利用反射,泛型,扩展方法快速获取表单值到实体类 js操作frameset frame 对象 关于“Internet Explorer无法打开站点,已终止操作” JavaScript 面向对象程序设计(下)——继承与多态 JavaScript 面向对象程序设计(上)——封装[转] JS类 检测上传图片的大小,宽,高及格式检查 .net 2.0 制作 柱状图 [转]AJAX之旅 由prototype_1.3.1进入javascript殿堂-类的初探 asp.net如何获取客户端网卡mac地址 [转]不可多得的Javascript(AJAX)开发工具 - Aptana 探究 Singleton 设计模式(构建分布式应用程序) .Net平台下开发中文语音应用程序[转] [转]在自定义Server Control中捆绑JS文件 Step by Step IE下JavaScript迁移到FireFox下的工作笔记[转] Asp.net组件开发(资料贴) 有关于JSON的一些资料 这样的UpdatePanel控件要怎么做 [转]30岁前男人应该做的16件事
[原创]jQuery PreviewImg 上传预览插件
㊣鑫哥 · 2009-06-05 · via 博客园 - ㊣鑫哥

最新版本请查看 http://www.9icool.net/

jQuery PreviewImg上传图预览插件,可自动缩略图,它是基于jQuery类库,可以预览上传控件需上传的图, 或html 元素中background-image 属性url

//====================================================================================================
// [插件名称] jQuery PreviewImg
//----------------------------------------------------------------------------------------------------
// [描    述] jQuery PreviewImg上传图预览插件,可自动缩略图,它是基于jQuery类库,可以预览上传控件需上传的图,
//           或html 元素中background-image 属性url
//----------------------------------------------------------------------------------------------------
// [作者网名] 孤叶 
// [邮    箱] solucky2008@gmail.com
// [作者博客] http://9icool.net/
// [更新日期] 2009-06-05
// [版 本 号] ver1.0
// [参数说明] FitWidth 期望预览图的最大宽
//            FitHeight     期望预览图的最大高
//            showtype     0表示为 fileupload控件,1 表示显示HTML元素 background-image
// [注意事项] 如果需要显示title ,则对应的html元素需要 title 属性.
// [使用示例] $(document).ready(function() {
//        $(":file").previewimage();
//        $(".showimgspan").previewimage({ showtype: 1 })
//    });
//====================================================================================================
(function($) {
    // plugin definition
    $.fn.previewimage = function(options) {
        var setting = {
            FitWidth: 400,
            FitHeight: 200,
            showtype: 0
        }
        $.extend(true, setting, options);
        return this.each(function() {
            if (setting.showtype == 0) {
                $(this).change(function() {
                    var htmlfile = $(this)[0];
                    var title = $(this).attr("title");
                    var imgsrc = "";
                    try {//预览代码,支持 IE6、IE7。
                        if (document.all) //IE
                            imgsrc = htmlfile.value;
                        else
                            imgsrc = htmlfile.files[0].getAsDataURL(); //FF
                        if (imgsrc == "" || imgsrc == undefined)
                            return;
                    } catch (e) {
                        return;
                    }
                    ShowImgPreview(imgsrc, title, $(this), setting);
                }).mouseover(function() {
                    $(this).trigger("change");
                });

            }

            if (setting.showtype == "1") {
                $(this).mouseover(function() {
                    var src = $(this).css("background-image").replace("url(\"", "").replace("\")", "").replace("url(", "").replace(")", "");
                    if (src == "" || src == undefined || src == "none") {
                        return;
                    }
                    ShowImgPreview(src, $(this).attr("title"), $(this), setting);
                });
            }

        });
    };

    /*加载图      
    总的原理就是new一个Image对象,设置了src属性过后,不断的检查需要载入的图片的宽和高,如果载入图片成功的话,宽和高都是不为0的数值,这个时候停止Interval ,并且执行onLoaded。
    */
    function EnhancedImage(src, onLoaded) {
        var self = this;
        this.src = src;
        this.width = 0;
        this.height = 0;
        this.onLoaded = onLoaded;
        this.loaded = false;
        this.image = null;

        this.load = function() {
            if (this.loaded)
                return;
            this.image = new Image();
            this.image.src = this.src;
            function loadImage() {
                if (self.width != 0 && self.height != 0) {
                    clearInterval(interval);
                    self.loaded = true;
                    self.onLoaded(self); //将实例传入回调函数
                }
                self.width = self.image.width; //是number类型
                self.height = self.image.height;
            }
            var interval = setInterval(loadImage, 100);
        }
    }

    function ShowImgPreview(imgsrc, title, posobj, setting) {
        if (imgsrc == "" || imgsrc == undefined)
            return;
        /*动态创建预览图层*/
        var newPreviewDiv = $("#picPreview");
        if (newPreviewDiv.length == 0) {
            var newPreviewDivHtml = "<div id=\"picPreview\" style=\"position:absolute; z-index:999;display:none;\">\
                    <span style=\"right:0; position:absolute; color:Red;\">xxx</span>\
                    <img id=\"picPreviewImg\">\
                </div>"
            newPreviewDiv = $(newPreviewDivHtml)
            $("body").append(newPreviewDiv);
        }
        var newPreview = document.getElementById("picPreviewImg");
        var _width = 0;
        var _height = 0;

        var image = new EnhancedImage(imgsrc, function() {
            if (image.width / image.height >= setting.FitWidth / setting.FitHeight) {
                if (image.width > setting.FitWidth) {
                    _width = setting.FitWidth;
                    _height = (image.height * setting.FitWidth) / image.width;
                } else {
                    _width = image.width;
                    _height = image.height;
                }
            } else {
                if (image.height > setting.FitHeight) {
                    _height = setting.FitHeight;
                    _width = (image.width * setting.FitHeight) / image.height;
                } else {
                    _width = image.width;
                    _height = image.height;
                }
            }
            newPreview.src = imgsrc;
            newPreview.style.height = _height + "px";
            newPreview.style.width = _width + "px";
            newPreviewDiv.show();
            newPreviewDiv.find("span:first").html(title);
            newPreviewDiv.css("top", posobj.offset().top - newPreviewDiv.height());
            newPreviewDiv.css("left", posobj.offset().left);
            posobj.mouseout(function() { $("#picPreview").hide(); });
            $(document).click(function() { $("#picPreview").hide(); });
        });
        image.load();
    }

})(jQuery);

更新日志:

2009-6-5 :v1.0发布

下载插件源码及示例previewimgDemo.rar (2.67 kb)