<script type="text/javascript">
(function($, window, undefined) {
var elems = $([]),
jq_resize = $.resize = $.extend($.resize, {}),
timeout_id,
str_setTimeout = 'setTimeout',
str_resize = 'resize',
str_data = str_resize + '-special-event',
str_delay = 'delay',
str_throttle = 'throttleWindow';
jq_resize[str_delay] = 250;
jq_resize[str_throttle] = true;
$.event.special[str_resize] = {
setup: function() {
if (!jq_resize[str_throttle] && this[str_setTimeout]) {
return false;
}
var elem = $(this);
elems = elems.add(elem);
$.data(this, str_data, {
w: elem.width(),
h: elem.height()
});
if (elems.length === 1) {
loopy();
}
},
teardown: function() {
if (!jq_resize[str_throttle] && this[str_setTimeout]) {
return false;
}
var elem = $(this);
elems = elems.not(elem);
elem.removeData(str_data);
if (!elems.length) {
clearTimeout(timeout_id);
}
},
add: function(handleObj) {
if (!jq_resize[str_throttle] && this[str_setTimeout]) {
return false;
}
var old_handler;
function new_handler(e, w, h) {
var elem = $(this),
data = $.data(this, str_data);
data.w = w !== undefined ? w : elem.width();
data.h = h !== undefined ? h : elem.height();
old_handler.apply(this, arguments);
}
if ($.isFunction(handleObj)) {
old_handler = handleObj;
return new_handler;
} else {
old_handler = handleObj.handler;
handleObj.handler = new_handler;
}
}
};
function loopy() {
timeout_id = window[str_setTimeout](function() {
elems.each(function() {
var elem = $(this),
width = elem.width(),
height = elem.height(),
data = $.data(this, str_data);
if (width !== data.w || height !== data.h) {
elem.trigger(str_resize, [data.w = width, data.h = height]);
}
});
loopy();
}, jq_resize[str_delay]);
}
})(jQuery, this);
function reSizeParentIframe() {
var realHeight = 1;
if (navigator.userAgent.indexOf("Firefox") > 0 || navigator.userAgent.indexOf("Mozilla") > 0 || navigator.userAgent.indexOf("Safari") > 0 || navigator.userAgent.indexOf("Chrome") > 0) { // Mozilla, Safari,Chrome, ...
realHeight = window.document.documentElement.offsetHeight + 35;
} else if (navigator.userAgent.indexOf("MSIE") > 0) { // IE
var bodyScrollHeight = window.document.body.scrollHeight + 21; //取得body的scrollHeight
var elementScrollHeight = window.document.documentElement.scrollHeight + 1; //取得documentElement的scrollHeight
realHeight = Math.min(bodyScrollHeight, elementScrollHeight); //取当中比较大的一个
} else {//其他浏览器
realHeight = window.document.body.scrollHeight + window.document.body.clientHeight + 1;
}
if (realHeight < 400) {
realHeight = 400;
}
//我们项目中iframe的id是rightFrame
if ($("#rightFrame", window.parent.document).is("iframe")) {
$("#rightFrame", window.parent.document).height(realHeight);
}
}
//.m-input是你指定的元素
window.οnlοad=function(){
$(".m-input").resize(function() {
//我们项目中用到的是iframe,初始化iframe高度固定,但元素变化后,需重新计算iframe高度
reSizeParentIframe();
});
}
</script>