日期:2014-05-18 浏览次数:20587 次
/// <reference path="jquery-1.4.4.min.js" />
/*
* jQuery插件
*
* 任意元素扩展为单选上传按钮
*
* By hongfei
*/
(function () {
var defaults = {
url: '',
success: null,
error: null,
loadBegin: null,
loadEnd: null,
mouseOver: null,
mouseOut: null
};
function uploadWrapper($wrapper, options) {
this.$wrapper = $wrapper;
this.url = options.url;
this.success = $.delegate(options.success);
this.error = $.delegate(options.error);
this.loadBegin = $.delegate(options.loadBegin);
this.loadEnd = $.delegate(options.loadEnd);
this.mouseOver = $.delegate(options.mouseOver);
this.mouseOut = $.delegate(options.mouseOut);
this.initPos = {};
this._init();
}
uploadWrapper.prototype = {
constructor: uploadWrapper,
_init: function () {
this.$wrapper.removeClass('ui_upload_wrap').addClass('ui_upload_wrap');
this.$image = this.$wrapper.find('img');
if (this.$wrapper.find('.ui_uploader_s_file').length == 0) {
this.$input = $('<input name="ui_uploader_s_file" id="ui_uploader_s_file" type="file" hidefocus="true"/>');
this.$wrapper.append(this.$input);
}
return this._bindEvents();
},
_bindEvents: function () {
this.$wrapper.unbind()
.mouseover(this._fileFollowBegin.delegate(this))
.mousemove(this._fileFollow.delegate(this))
.mouseout(this._fileFollowEnd.delegate(this));
this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this));
if (this.$btnCancel) {
this.$btnCancel.click(_cancelUpload);
}
return this;
},
/// 开始input:file跟随鼠标
_fileFollowBegin: function (e) {
this.initPos = this.$wrapper.offset();
this._fileFollow(e);
this.mouseOver();
},
/// 使input:file跟随鼠标
_fileFollow: function (e) {
this.initPos && this.$input.css({
left: e.pageX - this.initPos.left - 40,
top: e.pageY - this.initPos.top - 15
});
},
/// 结束input:file跟随鼠标
_fileFollowEnd: function (e) {
this.$input.css({
left: 0,
top: 0
});
this.mouseOut();
},
_uploadImage: function () {
if (this.$input.val().trim() == '') {
return;
}
var data = {
input: this.$input,
url: this.url,
dataType: 'json',