直接上程式碼
var kingwolfofsky = {
/**
* 取得輸入遊標在頁面中的座標
* @param {HTMLElement} 輸入框元素
* @return {Object} 回傳left和top,bottom
*/
getInputPositon: function (elem) {
if (document.selection) { //IE 支援
elem.重點();
var Sel = document.selection.createRange();
return {
左:Sel.boundingLeft,
上:Sel.boundingTop,
底部:Sel.boundingTop Sel.boundingHeight
};
} else {
var that = this;
var cloneDiv = '{$clone_div}',cloneLeft ='{$cloneLeft}',cloneFocus ='{$cloneFocus}',cloneRight ='{$cloneRight}';
var none = '; ';
var div = elem[cloneDiv] || document.createElement('div'), focus = elem[cloneFocus] || document.createElement('span');
var text = elem[cloneLeft] || document.createElement('span');
var offset = that._offset(elem), index = this._getFocus(elem), focusOffset = { left: 0, top: 0 };
if (!elem[cloneDiv]) {
elem[cloneDiv] = div, elem[cloneFocus] = focus;
elem[cloneLeft] = 文字;
div.appendChild(text);
div.appendChild(焦點);
document.body.appendChild(div);
focus.innerHTML = '|';
focus.style.cssText = 'display:inline-block;width:0px;overflow:hidden;z-index:-100;word-wrap:break-word;word-break:break-all;';
div.className = this._cloneStyle(elem);
div.style.cssText = '可見性:隱藏;隱藏:內聯區塊;位置:絕對;z-index:-100;自動換行:break-word;斷字:break-all;浪費:溢出; ';
};
div.style.left = this._offset(elem).left "px";
div.style.top = this._offset(elem).top "px";
var strTmp = elem.value.substring(0, index).replace(/, '/g, '>').replace(/n/ g, '').replace(/s/g, 無);
text.innerHTML = strTmp;
focus.style.display = '內聯塊';
嘗試{ focusOffset = this._offset(focus); } catch (e) { };
focus.style.display = '無';
return {
left: focusOffset.left,
top: focusOffset.top,
bottom: focusOffset.bottom
};
}
},
// 複製元素樣式並傳回類別
_cloneStyle: function (elem, cache) {
if (!cache && elem['${cloneName}'])返回elem['${cloneName}'];
var className, name, rstyle = /^(number|string)$/;
var rname = /^(內容|大綱|大綱寬度)$/; // 歌劇:內容; IE8:outline && outlineWidth
var cssText = [], sStyle = elem.style;
for (name in sStyle) {
if (!rname.test(name)) {
val = this._getStyle(elem, name);
if (val !== '' && rstyle.test(typeof val)) { // Firefox 4
name = name.replace(/([A-Z])/g, "-$1").toLowerCase ();
cssText.push(名稱);
cssText.push(':');
cssText.push(val);
cssText.push(';');
};
};
};
cssText = cssText.join('');
elem['${cloneName}'] = className = 'clone' (new Date).getTime();
this._addHeadStyle('.' className '{' cssText '}');
回傳類別名稱;
},
//向頁頭插入樣式
_addHeadStyle: function (content) {
var style = this._style[document];
if (!style) {
style = this._style[document] = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
};
style.styleSheet && (style.styleSheet.cssText = 內容) || style.appendChild(document.createTextNode(content));
},
_style: {},
//取得最終樣式
_getStyle: 'getCompulatedStyle' in window ? function (elem, name) {
return getCompulatedStyle(elem, null)[name];
} : function (elem, name) {
return elem.currentStyle[name];
},
// 在文字方塊的位置取得對話方塊
_getFocus: function (elem) {
var index = 0;
if (document.selection) {// IE 支援
elem.focus();
var Sel = document.selection.createRange();
if (elem.nodeName === 'TEXTAREA') {//textarea
var Sel2 = Sel.duplicate();
Sel2.moveToElementText(elem);
var 索引 = -1;
while (Sel2.inRange(Sel)) {
Sel2.moveStart('character');
索引;
};
}
else if (elem.nodeName === 'INPUT') {// 輸入
Sel.moveStart('character', -elem.value.長度);
index = Sel.text.length;
}
}
else if (elem.selectionStart || elem.selectionStart == '0') { // Firefox 支援
index = elem.selectionStart;
}
回傳(索引);
},
//在頁面中位置取得元素
_offset: function (elem) {
var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement;
var clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0;
var top = box.top(self.pageYOffset || docElem.scrollTop) - clientTop, left = box.left(self.pageXOffset || docElem.scrollLeft) - clientLeft;
return {
left: 左,
top: 上,
right: 左 box.width,
bottom: 上 box.height
};
}
};
function getPosition(ctrl) {
var p = kingwolfofsky.getInputPositon(ctrl);
document.getElementById('show').style.left = p.left "px";
document .getElementById('show').style.top = p.bottom "px";
}
------------------ -------------------------------------------------- -------------------------------------------------- ------------------
呼叫程式碼:
var elem = document.getElementById(控制項ID);
var p = kingwolfofsky.getInputPositon(elem);
p.leftft; /取得指定位置座標
p.top;//同上
p.bottom;//同上