用js写一个可以拖拽的浮动窗口

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>dom3.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script language="javascript" type="text/javascript">
    	var x =0;
    	var y = 0;
    	var x1 =0;
    	var y1 = 0;
    	var moveable = false;
    	var index = 20000;
    	
  		function down(obj,evt){
  			e = evt?evt:window.event;
  			//通过这个来判断是IE还是FF  浏览器的兼容性问题
 // 			if(!window.captureEvents()){
  				obj.setCapture();
 // 			}else{
 // 				window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
 // 			}
  			var win = obj.parentNode;//取得父窗口
  			x = e.clientX;//取得当前坐标的X坐标
  			y = e.clientY;//取得当前坐标的Y坐标
  			x1 = parseInt(win.style.left.substr(0,win.style.left.length-2));//将父窗体距离浏览器左边界的距离转换为number
  			y1 = parseInt(win.style.top.substr(0,win.style.top.length-2));//将父窗体距离浏览器左边界的距离转换为number
  			moveable= true;
  			
  		}
  		
  		function move(obj, evt){
  			e = evt?evt:window.event;
  			if(moveable){
  				var win = obj.parentNode;
  				win.style.left = x1 + e.clientX - x +"px";
  				win.style.top = y1 + e.clientY -y +"px";
  				
  			}
  		}
  		//停止拖动
  		function up(obj){
 // 			if(!window.captureEvents()){
  				obj.releaseCapture();
 // 			}else{
 // 				window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
 // 			}
  			moveable= false;
  		}
    </script>

  </head>
  
  <body >
	<div style="width:400px;height:400px;top:100px;left:100px;background-color:red;position:absolute;">
		<div οnmοusedοwn="down(this,event)" οnmοusemοve="move(this,event)" οnmοuseup="up(this)"
		style="width:400px;height:100px;top:0px;left:0px;background-color:blue;position:absolute;font-size:30px;cursor:default">
		浮动窗口
		</div>
	</div>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 JS 实现弹出浮动窗口的详细步骤: 1. HTML 结构 首先,我们需要在 HTML 中添加一个按钮,用于触发弹出浮动窗口的事件。同时,为了方便后续操作,我们需要在 HTML 中添加一个空的 div 元素,用于存放弹出浮动窗口的内容。 ```html <button id="open-btn">打开浮动窗口</button> <div id="float-window"></div> ``` 2. CSS 样式 接下来,我们需要为浮动窗口添加样式。这里我们使用绝对定位来实现浮动效果,同时添加一些基本的样式,如背景颜色、边框样式等。 ```css #float-window { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; height: 300px; background-color: #fff; border: 1px solid #ccc; display: none; /* 默认隐藏 */ } ``` 3. JS 代码 最后,我们需要使用 JS 代码来实现弹出浮动窗口的功能。具体步骤如下: 1. 获取按钮元素和浮动窗口元素,并添加事件监听器。 ```javascript var openBtn = document.getElementById('open-btn'); var floatWindow = document.getElementById('float-window'); openBtn.addEventListener('click', function() { floatWindow.style.display = 'block'; }); ``` 2. 实现拖拽效果。 ```javascript var isDragging = false; // 是否正在拖动 var mouseOffset = { x: 0, y: 0 }; // 鼠标相对于浮动窗口的偏移量 floatWindow.addEventListener('mousedown', function(e) { isDragging = true; mouseOffset.x = e.pageX - floatWindow.offsetLeft; mouseOffset.y = e.pageY - floatWindow.offsetTop; }); document.addEventListener('mousemove', function(e) { if (isDragging) { floatWindow.style.left = e.pageX - mouseOffset.x + 'px'; floatWindow.style.top = e.pageY - mouseOffset.y + 'px'; } }); document.addEventListener('mouseup', function() { isDragging = false; }); ``` 3. 实现关闭效果。 ```javascript var closeBtn = document.createElement('div'); closeBtn.innerHTML = '关闭'; closeBtn.style.position = 'absolute'; closeBtn.style.top = '10px'; closeBtn.style.right = '10px'; floatWindow.appendChild(closeBtn); closeBtn.addEventListener('click', function() { floatWindow.style.display = 'none'; }); ``` 最终的 JS 代码如下: ```javascript var openBtn = document.getElementById('open-btn'); var floatWindow = document.getElementById('float-window'); openBtn.addEventListener('click', function() { floatWindow.style.display = 'block'; }); var isDragging = false; // 是否正在拖动 var mouseOffset = { x: 0, y: 0 }; // 鼠标相对于浮动窗口的偏移量 floatWindow.addEventListener('mousedown', function(e) { isDragging = true; mouseOffset.x = e.pageX - floatWindow.offsetLeft; mouseOffset.y = e.pageY - floatWindow.offsetTop; }); document.addEventListener('mousemove', function(e) { if (isDragging) { floatWindow.style.left = e.pageX - mouseOffset.x + 'px'; floatWindow.style.top = e.pageY - mouseOffset.y + 'px'; } }); document.addEventListener('mouseup', function() { isDragging = false; }); var closeBtn = document.createElement('div'); closeBtn.innerHTML = '关闭'; closeBtn.style.position = 'absolute'; closeBtn.style.top = '10px'; closeBtn.style.right = '10px'; floatWindow.appendChild(closeBtn); closeBtn.addEventListener('click', function() { floatWindow.style.display = 'none'; }); ``` 这样,我们就完成了使用 JS 实现弹出浮动窗口的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值