• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

网络口袋

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

[JavaScript]跟随鼠标移动的DIV

关键事件

[body|img|...].onmousemove

关键代码

<div style="position:absolute;"></div>

 

obj.style.visibility = "visible";
div.style.left 
= event.x +getScrollL() + 10;//getScrollL()获取滚动条的Left值
div.style.top = event.y + getScrollT() + 10;//getScrollT()获取滚动条的Top值

 

附:获取滚动条参数的函数

/// <summary>
//
/ 获取滚动条的Left值
//
/ </summary>
//
/ <returns>scrollLeft值</returns> 
function getScrollL()
{     
    
var l    
    
if (document.documentElement && document.documentElement.scrollTop)
    {           
        l 
= document.documentElement.scrollLeft;                  
    } 
    
else if (document.body)
    {              
        l 
= document.body.scrollLeft;           
    }    
        
return l; 
} 

/// <summary>
//
/ 获取滚动条的Height值
//
/ </summary>
//
/ <returns>scrollHeight值</returns> 
function getScrollH()
{     
    
var h;          
    
if (document.documentElement && document.documentElement.scrollTop)
    {                  
       h 
= document.documentElement.scrollHeight;     
    }
    
else if (document.body)
    {              
       h 
= document.body.scrollHeight;     
    }   
    
return h; 
} 

/// <summary>
//
/ 获取滚动条的Width值
//
/ </summary>
//
/ <returns>scrollWidth值</returns> 
function getScrollW() {     
    
var w;         
    
if (document.documentElement && document.documentElement.scrollTop)
    {                   
        w 
= document.documentElement.scrollWidth;             
    }
    
else if (document.body)
    {           
       w 
= document.body.scrollWidth;            
    }    
    
return w; 
} 

/// <summary>
//
/ 获取滚动条的Top值
//
/ </summary>
//
/ <returns>scrollTop值</returns> 
function getScrollT() {     
    
var t;          
    
if (document.documentElement && document.documentElement.scrollTop)
    {           
        t 
= document.documentElement.scrollTop;  
    }
    
else if (document.body)
    {         
       t 
= document.body.scrollTop;         
    }
    
return t; 
} 

 

posted on 2009-04-06 06:52  俊  阅读(3366)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3