写着玩的,哎,每次写的东西都不是那么完美,算了,当学习

效果图:

//图片预览插件
//Writer:lyfeixue
//2009-5-25 11:29:17
//参数:options json对象,w:显示的宽度,h:显示的高度,如果高度或者宽度为空,则调用原始的图片大小
//调用方法 $("#VenLogo").PicturePreview();
//$("#VenLogo").PicturePreview(w:0,h:0);默认显示图片的原始高宽

//$("#VenLogo").PicturePreview(w:300,h:300);自定义显示出来的图片高宽
//bug:只能调用第一个对象的图片URL,如果传入了多个jquery对象,有待解决
(function($) {
    $.fn.PicturePreview = function(options) {
        var o = $.extend({
            w: 200,
            h: 200
        }, options);
        var _this = this;
        var allowExt = ["jpg", "png", "bmp", "gif"]; //定义允许的图片参数
        var isCheck = false; //是否校验通过图片
        _this.mousemove(function(e) {
            var FileUrl = _this.val();
            if (FileUrl == "" || FileUrl == null) { FileUrl = _this.html() == "" || _this.html() == null ? _this.text() : _this.html(); }
            var arrList = FileUrl.split(".");
            isCheck = new RegExp(allowExt.join("\|"), "ig").test(arrList[arrList.length - 1]);
            if (isCheck) {
                var obj = $("#Ly_PicturePreview");
                if (obj.size() == 0) {
                    //创建预览对象
                    $("body").append("<img id=\"Ly_PicturePreview\" alt=\"图片预览\" style=\"border:1px solid gray;position:absolute;display:none;z-index:999;\" src=\"" + FileUrl + "\" />");
                }
                else {
                    obj.attr("src", FileUrl);
                }
                obj.css({ top: e.pageY + 5, left: e.pageX + 5, width: o.w == 0 ? obj.width() : o.w, height: o.h == 0 ? obj.height() : o.h }).show();
            }
        }).mouseout(function() {
            $("#Ly_PicturePreview").hide();
        });
        return _this;
    };
})(jQuery);

posted on 2009-05-25 11:54  站在天空下的猪  阅读(2204)  评论(0编辑  收藏  举报