javascript - 如何在浏览器窗口的边缘保留一个框?

标签 javascript html css jquery

我想制作一个工具提示 div,当某个输入元素获得焦点时,它会出现在该元素旁边并与该元素顶部对齐:

                bla bla blah
 ____________    ___________________
| this       |  [_button____clicked_]
| appears    |
| on side    |  body content dafjsd fdjskf sdf
|____________|  content djsfs df skfjs daskf kdf s
                text texty djkf jsk fdjfs
                texty hehe blabla ajdfskfsd

我可以用绝对定位来做到这一点。但是我不希望所有的工具提示在不滚动的情况下都可见,所以如果输入框恰好位于窗口的底部,工具提示应该调整以保持在 View 中:

                content djsfs df skfjs daskf kdf s
                text texty djkf jsk fdjfs
                texty hehe blabla ajdfskfsd
 ____________   bla bla blah
| this       |   ___________________
| appears    |  [_button____clicked_]
| on side    |
|____________|  edge of broswer window  vvv
------------------------------------------------------------

也就是说,当用户上下滚动时,工具提示应尽可能贴近其正确位置,同时保持在窗口内。

最佳答案

如果您阅读这个相当简单的基于 javascript 的工具提示脚本的代码,您就可以找到难题的答案:http://www.dynamicdrive.com/dynamicindex5/dhtmltooltip.htm .

它会检测浏览器窗口的边缘,并根据该输入非常流畅地显示在不同的位置。测试页面顶部附近的工具提示演示,您就会明白我的意思。

但要直接回答你的问题 - 最好的方法是使用 javascript,首先检查正在使用的浏览器,然后使用适当的屏幕宽度计算根据宽度、高度和当前 '浏览器窗口的位置。可以在上面链接的代码中找到此条件浏览器逻辑和屏幕宽度计算的示例:)

希望这对您有所帮助。

编辑:

经过一番思考,我想我应该只包括上面链接的代码的 JS 方面,以防有一天链接失败,仅供引用。它有点神秘,但它仍然是一个处理基于浏览器窗口的工具提示定位的脚本:

var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

关于javascript - 如何在浏览器窗口的边缘保留一个框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031054/

相关文章:

javascript - 为什么这个表达式在 JavaScript 中被评估为 "a"?

javascript - HTML5 Canvas 物理

html - CSS - 如何更改 rtl 方向页面中复选框输入的位置

javascript - 在 ul/li 菜单中隐藏部分边框

css - XP 上的 IE7 呈现媒体 ="print"样式表

html - 放大浏览器时站点显示问题

javascript - VB Now.Ticks 等价于 javascript

javascript - 如何通过匹配特定字符串将一个 ng-model 中的值绑定(bind)到另一个

javascript - 在请求中使用字符串引用 JSON 对象

javascript - 改变ckeditor的<span style>