2011-11-06 98 views
1

我想制作一个工具提示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 

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

    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 
------------------------------------------------------------ 

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

回答

1

如果您阅读这个相当简单的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 
相关问题