在线等!急!如何定位层 你要加上document.body.scrollLeft和document.body.scrollTop就可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是怎么定位置的?是不是设置的style.left和style.top当鼠标经过那个关键字的时候,就是说触发了onmouseover事件,然后获取鼠标的位置。。然后让鼠标的位置加上document.body.scrollLeft、document.body.scrollTop即就是这个关键子在网页中的位置。这里有个window的特效你可以简单看下。http://community.csdn.net/Expert/topic/5283/5283616.xml?temp=.7460749 我是这样子弄的:当onclick关键字的时候用ajax调用数据,然后显示到这个层里面,就是出现滚动条的时候,这个层的位置不准确了.function tagshow(varVal){ data_win.style.top=window.event.y+"px"; data_win.style.left=window.event.x+"px"; data_win.style.display = "block"; voucharTip = document.getElementById("data_win"); totaourl = "../ajaxCheck.php?method=gettag&tag="+encodeURIComponent(varVal)+"&now=" + new Date().getTime(); send_request(totaourl);}<div id="data_win" style="display: none;position:absolute;"></div>页面上没有滚动条是正确的,就在关键字右下角,有滚动条就不对了. 就是这个http://www.sales007.com/news/detail-1-0.html样子,不会改了 data_win.style.top = document.body.scrollTop + window.event.y + "px";data_win.style.left = document.body.scrollLeft + window.event.x + "px";就可以了。不过你这样弄不兼容FF... a piece of cake! 给你一个例子:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">function Point(iX, iY){ this.x = iX; this.y = iY;}function fGetXY(aTag){ var oTmp = aTag; var pt = new Point(0,0); do { pt.x += oTmp.offsetLeft; pt.y += oTmp.offsetTop; oTmp = oTmp.offsetParent; } while(oTmp.tagName!="BODY"); pt.x+=parseInt(document.body.leftMargin); pt.y+=parseInt(document.body.topMargin); return pt;}function showDiv(div,pos){ var pt=fGetXY(pos); div.style.left=pt.x+pos.offsetWidth; div.style.top=pt.y+pos.offsetHeight; div.style.display='block';}function hideDiv(div){ div.style.display='none'; alert()}</script><style type="text/css"><!--#Layer1 { position:absolute; width:150px; height:50px; z-index:1; background-color: #FFFFCC; display:none;}.key{cursor:hand;color:blue;font-size:12px;}--></style></head><body><div id="Layer1">啦啦啦,出现了。</div><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p><p><span class="key">测试链接</span></p></body></html> 谢谢 muxrwc(王辰),要兼容ff怎么弄呢?sina的新闻页面 他的js我看不明白http://news.sina.com.cn/w/2006-12-08/001311729059.shtml <html><head><script type="text/javascript">function onmouseMove() { function createDIV(id) { //创建并且返回DIV var div = document.createElement("div") div.setAttribute("id", "temp_DIV_" + id); document.body.appendChild(div); return document.getElementById("temp_DIV_" + id); } function change(e) { //Load数据并且生成DIV设置参数 var e = window.event || e; //我就会这么一种兼容获取EVENT的方法,好象还有一种可以查找,但是那个好麻烦所以没记.. var loadValue = "WC" //AJAX加载数据,比如加载完后得到了WC - -# this.div = createDIV(this.id); with (this.div.style) { position = "absolute"; display = "block"; left = document.body.scrollLeft + this.offsetLeft + "px"; top = document.body.scrollTop + (this.offsetTop + this.offsetHeight - 5) + "px"; } this.div.innerHTML = loadValue; this.onmousemove = showDIV; this.onmouseout = nshowDIV; } function showDIV() { //显示 this.div.style.display = "block"; } function nshowDIV() { //隐藏 this.div.style.display = "none"; } this.add = function (o) { //添加 o.onmouseover = change; }}window.onload = function () { var mo = new onmouseMove; mo.add(document.getElementById("wc1")) mo.add(document.getElementById("wc2")) mo = null;}</script></head><body><a id="wc1">KEY</a><a id="wc2">KEY</a></body></html> 求助extjs4.0 多条件检索 我在导入jQuery和NiceJForms库一起用时冲突,急,发分了!!! FireFox中div设置宽度的问题 请教高手:关于JS 函数的定义 如何得到一个text的默认宽度 回车键才能提交表单问题 自助设计 如何得到input中输入的字符的位置 文件夹名称的正则问题 在用DHTML動態生成「Buton」時、怎麼加「Click Event」 关于按钮提交问题! 字符串比较问题!
是不是设置的style.left和style.top
当鼠标经过那个关键字的时候,就是说触发了onmouseover事件,然后获取鼠标的位置。。
然后让鼠标的位置加上document.body.scrollLeft、document.body.scrollTop即就是这个关键子在网页中的位置。
这里有个window的特效你可以简单看下。
http://community.csdn.net/Expert/topic/5283/5283616.xml?temp=.7460749
function tagshow(varVal){
data_win.style.top=window.event.y+"px";
data_win.style.left=window.event.x+"px";
data_win.style.display = "block";
voucharTip = document.getElementById("data_win");
totaourl = "../ajaxCheck.php?method=gettag&tag="+encodeURIComponent(varVal)+"&now=" + new Date().getTime();
send_request(totaourl);
}<div id="data_win" style="display: none;position:absolute;"></div>页面上没有滚动条是正确的,就在关键字右下角,有滚动条就不对了.
就是这个http://www.sales007.com/news/detail-1-0.html样子,不会改了
data_win.style.left = document.body.scrollLeft + window.event.x + "px";
就可以了。不过你这样弄不兼容FF...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function Point(iX, iY){
this.x = iX;
this.y = iY;
}function fGetXY(aTag){
var oTmp = aTag;
var pt = new Point(0,0);
do {
pt.x += oTmp.offsetLeft;
pt.y += oTmp.offsetTop;
oTmp = oTmp.offsetParent;
} while(oTmp.tagName!="BODY");
pt.x+=parseInt(document.body.leftMargin);
pt.y+=parseInt(document.body.topMargin);
return pt;
}function showDiv(div,pos){
var pt=fGetXY(pos);
div.style.left=pt.x+pos.offsetWidth;
div.style.top=pt.y+pos.offsetHeight;
div.style.display='block';
}function hideDiv(div){
div.style.display='none';
alert()
}
</script><style type="text/css">
<!--
#Layer1 {
position:absolute;
width:150px;
height:50px;
z-index:1;
background-color: #FFFFCC;
display:none;
}.key{
cursor:hand;
color:blue;
font-size:12px;
}
-->
</style>
</head><body>
<div id="Layer1">啦啦啦,出现了。</div>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span onclick="showDiv(Layer1,this)" class="key">测试链接</span></p>
<p><span class="key">测试链接</span></p>
</body></html>
要兼容ff怎么弄呢?
sina的新闻页面 他的js我看不明白
http://news.sina.com.cn/w/2006-12-08/001311729059.shtml
<head>
<script type="text/javascript">
function onmouseMove() {
function createDIV(id) {
//创建并且返回DIV
var div = document.createElement("div")
div.setAttribute("id", "temp_DIV_" + id);
document.body.appendChild(div);
return document.getElementById("temp_DIV_" + id);
}
function change(e) {
//Load数据并且生成DIV设置参数
var e = window.event || e; //我就会这么一种兼容获取EVENT的方法,好象还有一种可以查找,但是那个好麻烦所以没记..
var loadValue = "WC" //AJAX加载数据,比如加载完后得到了WC - -#
this.div = createDIV(this.id);
with (this.div.style) {
position = "absolute";
display = "block";
left = document.body.scrollLeft + this.offsetLeft + "px";
top = document.body.scrollTop + (this.offsetTop + this.offsetHeight - 5) + "px";
}
this.div.innerHTML = loadValue;
this.onmousemove = showDIV;
this.onmouseout = nshowDIV;
}
function showDIV() {
//显示
this.div.style.display = "block";
}
function nshowDIV() {
//隐藏
this.div.style.display = "none";
}
this.add = function (o) {
//添加
o.onmouseover = change;
}
}
window.onload = function () {
var mo = new onmouseMove;
mo.add(document.getElementById("wc1"))
mo.add(document.getElementById("wc2"))
mo = null;
}
</script>
</head>
<body>
<a id="wc1">KEY</a>
<a id="wc2">KEY</a>
</body>
</html>