js获得指定控件输入光标的坐标兼容IE,Chrome,火狐等多种主流浏览器
2023-06-13 09:14:54 时间
直接上代码
复制代码代码如下:
varkingwolfofsky={
/**
*获取输入光标在页面中的坐标
*@param{HTMLElement}输入框元素
*@return{Object}返回left和top,bottom
*/
getInputPositon:function(elem){
if(document.selection){//IESupport
elem.focus();
varSel=document.selection.createRange();
return{
left:Sel.boundingLeft,
top:Sel.boundingTop,
bottom:Sel.boundingTop+Sel.boundingHeight
};
}else{
varthat=this;
varcloneDiv="{$clone_div}",cloneLeft="{$cloneLeft}",cloneFocus="{$cloneFocus}",cloneRight="{$cloneRight}";
varnone="<spanstyle="white-space:pre-wrap;"></span>";
vardiv=elem[cloneDiv]||document.createElement("div"),focus=elem[cloneFocus]||document.createElement("span");
vartext=elem[cloneLeft]||document.createElement("span");
varoffset=that._offset(elem),index=this._getFocus(elem),focusOffset={left:0,top:0};
if(!elem[cloneDiv]){
elem[cloneDiv]=div,elem[cloneFocus]=focus;
elem[cloneLeft]=text;
div.appendChild(text);
div.appendChild(focus);
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="visibility:hidden;display:inline-block;position:absolute;z-index:-100;word-wrap:break-word;word-break:break-all;overflow:hidden;";
};
div.style.left=this._offset(elem).left+"px";
div.style.top=this._offset(elem).top+"px";
varstrTmp=elem.value.substring(0,index).replace(/</g,"<").replace(/>/g,">").replace(/\n/g,"<br/>").replace(/\s/g,none);
text.innerHTML=strTmp;
focus.style.display="inline-block";
try{focusOffset=this._offset(focus);}catch(e){};
focus.style.display="none";
return{
left:focusOffset.left,
top:focusOffset.top,
bottom:focusOffset.bottom
};
}
},
//克隆元素样式并返回类
_cloneStyle:function(elem,cache){
if(!cache&&elem["${cloneName}"])returnelem["${cloneName}"];
varclassName,name,rstyle=/^(number|string)$/;
varrname=/^(content|outline|outlineWidth)$/;//Opera:content;IE8:outline&&outlineWidth
varcssText=[],sStyle=elem.style;
for(nameinsStyle){
if(!rname.test(name)){
val=this._getStyle(elem,name);
if(val!==""&&rstyle.test(typeofval)){//Firefox4
name=name.replace(/([A-Z])/g,"-$1").toLowerCase();
cssText.push(name);
cssText.push(":");
cssText.push(val);
cssText.push(";");
};
};
};
cssText=cssText.join("");
elem["${cloneName}"]=className="clone"+(newDate).getTime();
this._addHeadStyle("."+className+"{"+cssText+"}");
returnclassName;
},
//向页头插入样式
_addHeadStyle:function(content){
varstyle=this._style[document];
if(!style){
style=this._style[document]=document.createElement("style");
document.getElementsByTagName("head")[0].appendChild(style);
};
style.styleSheet&&(style.styleSheet.cssText+=content)||style.appendChild(document.createTextNode(content));
},
_style:{},
//获取最终样式
_getStyle:"getComputedStyle"inwindow?function(elem,name){
returngetComputedStyle(elem,null)[name];
}:function(elem,name){
returnelem.currentStyle[name];
},
//获取光标在文本框的位置
_getFocus:function(elem){
varindex=0;
if(document.selection){//IESupport
elem.focus();
varSel=document.selection.createRange();
if(elem.nodeName==="TEXTAREA"){//textarea
varSel2=Sel.duplicate();
Sel2.moveToElementText(elem);
varindex=-1;
while(Sel2.inRange(Sel)){
Sel2.moveStart("character");
index++;
};
}
elseif(elem.nodeName==="INPUT"){//input
Sel.moveStart("character",-elem.value.length);
index=Sel.text.length;
}
}
elseif(elem.selectionStart||elem.selectionStart=="0"){//Firefoxsupport
index=elem.selectionStart;
}
return(index);
},
//获取元素在页面中位置
_offset:function(elem){
varbox=elem.getBoundingClientRect(),doc=elem.ownerDocument,body=doc.body,docElem=doc.documentElement;
varclientTop=docElem.clientTop||body.clientTop||0,clientLeft=docElem.clientLeft||body.clientLeft||0;
vartop=box.top+(self.pageYOffset||docElem.scrollTop)-clientTop,left=box.left+(self.pageXOffset||docElem.scrollLeft)-clientLeft;
return{
left:left,
top:top,
right:left+box.width,
bottom:top+box.height
};
}
};
functiongetPosition(ctrl){
varp=kingwolfofsky.getInputPositon(ctrl);
document.getElementById("show").style.left=p.left+"px";
document.getElementById("show").style.top=p.bottom+"px";
}
----------------------------------------------------------------------------------------------------------------------------------------
调用代码:
varelem=document.getElementById(控件ID);
varp=kingwolfofsky.getInputPositon(elem);
p.left;//获得指定位置坐标
p.top;//同上
p.bottom;//同上
相关文章
- js书写原生ajax,JS 原生ajax写法
- 使用 Dapr JS SDK 让 Nest.js 集成 Dapr(微软开源的分布式应用程序运行时)
- html如何只刷新页面指定,js控制页面刷新 JS刷新当前页面的几种方法总结
- node.js(7)
- js 获取多级路径
- JS设置定时器_js设置定时器
- Node.JS 删除数组元素
- js如何实现阅读完协议后才可以注册
- JS手撕(三) 节流、防抖
- 如何使用jscythe并通过Node.js的Inspector机制执行任意JS代码
- js 全局键盘事件
- 使用JS实现文字搬运工详解编程语言
- JS的Array怎么用详解编程语言
- JS动态引入js、CSS动态创建script/link/style标签详解编程语言
- 使用JS在网页中连接Oracle数据库(js网页链接oracle)
- 使用JS控制Oracle数据库的更新(js控制oracle更新)
- 使用JS操作Oracle数据库探索潜在可能性(js和oracle数据库)
- JS获取页面窗口大小的代码解读
- js延迟加载改变JS的位置加快网页加载速度
- JS上传图片前的限制包括(jpgjpggif及大小高宽)等
- JS刷新框架外页面七种实现代码
- 一个简单的弹性返回顶部JS代码实现介绍
- js动态生成指定行数的表格
- js文本滚动效果的实例代码
- js螺旋动画效果的具体实例
- JS获取节点的兄弟,父级,子级元素的方法
- JS将所有对象s的属性复制给对象r(原生js+jquery)
- js实现文章文字大小字号功能完整实例
- node.js中的querystring.stringify方法使用说明