javascript + DIV +CSS 实现可拖动消息窗体

本文介绍了一种使用HTML、CSS及JavaScript实现的可拖动透明窗体的方法。通过鼠标事件控制窗体的位置,实现了窗体的自由移动功能。适用于网页中需要交互式浮动元素的场景。
  
None.gif<html>
None.gif
<head>
ExpandedBlockStart.gifContractedBlock.gif
<style>dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    #div
{dot.gif}{width:300px;height:200px;color=red;border-top:1px solid #000;border-bottom:1px solid #000;border-left:1px solid #000;border-right:1px solid #000;size=13px;position:absolute;bottom:50px;right=80px;}
ExpandedBlockEnd.gif    
None.gif
</style>
None.gif
</head>
None.gif
None.gif
<body>
ExpandedBlockStart.gifContractedBlock.gif    
<script language="javascript">dot.gif
InBlock.gif        
var moveable = false;
InBlock.gif        
function aa(val)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                        
InBlock.gif             
var obj = document.getElementById("div");
InBlock.gif            
if(val == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                obj.style.display
="";
InBlock.gif                        
var x,y;
InBlock.gif            
InBlock.gif                x 
= event.clientX;
InBlock.gif                y 
= event.clientY;
InBlock.gif           
InBlock.gif                obj.style.left
=x;
InBlock.gif                obj.style.top
=y;
InBlock.gif                        
//alert("X:"+x+"Y:"+y);
ExpandedSubBlockEnd.gif
            }

InBlock.gif                    
else
InBlock.gif              obj.style.display
="none";
ExpandedSubBlockEnd.gif        }
    
InBlock.gif        
function startgrap(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                        
InBlock.gif            
if(event.button==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            obj.setCapture();
InBlock.gif             
//记录鼠标和层位置;
InBlock.gif
                       x0 = event.clientX;
InBlock.gif                       y0 
= event.clientY;
InBlock.gif                       x1 
= parseInt(obj.style.left);
InBlock.gif                       y1 
= parseInt(obj.style.top);
InBlock.gif                       moveable 
= true;            
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif         }

InBlock.gif        
function stopgrap(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(moveable)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                obj.releaseCapture();
//用来释放对鼠标的捕捉
InBlock.gif
                moveable = false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
function grap(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
if(moveable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{                       
InBlock.gif                           obj.style.left 
= x1 + event.clientX - x0;
InBlock.gif                           obj.style.top  
= y1 + event.clientY - y0;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    
</script>
None.gif    
<form id="form1">
None.gif        
<input type=button value="DIV应用" onmousemove="aa(0);">    
None.gif        
<div id="div2" style="background:#ff00cc;width=100px;" onmousemove="aa(0);">鼠标移到上面试试看</div> 
None.gif        
<div id="div" style="display:none" onmousedown="startgrap(this);" onmouseup="stopgrap(this);" onmousemove="grap(this);">
None.gif            
<div style="border-top:1px solid red;position:absolute;right=1px;"><span onclick="aa(1);" style="cursor:hand"><font color="#eee"><b>关闭</b></font></span></div>
None.gif            
<span>I Like you ,do you know? <br>if you don`t know,now i tell you,i tell you!<br><br>透明窗体,可拖动</span>
None.gif        
</div>
None.gif    
</form>
None.gif    
None.gif
</body>
None.gif
</html>

本程序测试通过,大家有好的方法可一起讨论!谢谢

转载于:https://www.cnblogs.com/Lewis/archive/2006/07/14/450978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值