弹窗拖拽

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>    
  <head>    
     
        
    <title>弹出层</title>    
    <meta http-equiv="pragma" content="no-cache">    
    <meta http-equiv="cache-control" content="no-cache">    
    <meta http-equiv="expires" content="0">        
    <meta http-equiv="description" content="This is my page">    
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  </head>
<body>
<button id="show">显示</button> 
<div class="win"> 
<div class="wTop"><p style="float:right;margin:5px 5px 0px 0px;color:white" id="hidden">X</p></div> 
<div class="content"></div> 
</div> 




<style type="text/css"> 
.win{width:500px;height:600px;background:#000000;border-radius:8px;box-shadow:0px 0px 5px 10px;opacity:0.8;position:absolute;left:0;top:0;display:none} 
.win .wTop{height:30px;width:100%;cursor:move} 
.win .content{height:570px;width:100%;border-radius:5px;background:white} 
</style> 




<script language="javascript" type="text/javascript"> 
$(function(){ 
//拖拽 
dragAndDrop(); 
//初始化位置 
initPosition(); 
//点击按钮 
clickShowBtn(); 
}); 
//拖拽 
function dragAndDrop(){ 
var _move=false;//移动标记 
var _x,_y;//鼠标离控件左上角的相对位置 
$(".wTop").mousedown(function(e){ 
_move=true; 
_x=e.pageX-parseInt($(".win").css("left")); 
_y=e.pageY-parseInt($(".win").css("top")); 
//$(".wTop").fadeTo(20,0.5);//点击开始拖动并透明显示 
}); 
$(document).mousemove(function(e){ 
if(_move){ 
var x=e.pageX-_x;//移动时鼠标位置计算控件左上角的绝对位置 
var y=e.pageY-_y; 
$(".win").css({top:y,left:x});//控件新位置 

}).mouseup(function(){ 
_move=false; 
//$(".wTop").fadeTo("fast",1);//松开鼠标后停止移动并恢复成不透明 
}); 

//初始化拖拽div的位置 
function initPosition(){ 
//计算初始化位置 
var itop=($(document).height()-$(".win").height())/2; 
var ileft=($(document).width()-$(".win").width())/1.8; 
//设置被拖拽div的位置 
$(".win").css({top:itop,left:ileft}); 

//点击显示按钮 
function clickShowBtn(){ 
$("#show").click(function(){ 
$(".win").show(1000); 
}); 


$("#hidden").click(function(){ 
$(".win").hide(1000); 
}); 

</script> 
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你手写一个基于Vue3的弹窗拖拽组件示例代码。 首先,你需要安装Vue3: ``` npm install vue@next ``` 然后,创建一个Draggable.vue组件,代码如下: ```vue <template> <div class="draggable" :style="{ left: left + 'px', top: top + 'px' }"> <div class="title" @mousedown="dragStart"> {{ title }} </div> <div class="content"> <slot /> </div> </div> </template> <script> import { ref } from 'vue'; export default { props: { title: { type: String, default: '', }, }, setup(props, { emit }) { const left = ref(0); const top = ref(0); let startX, startY, offsetX, offsetY; function dragStart(e) { startX = e.clientX; startY = e.clientY; offsetX = left.value; offsetY = top.value; document.addEventListener('mousemove', drag); document.addEventListener('mouseup', dragEnd); } function drag(e) { const moveX = e.clientX - startX; const moveY = e.clientY - startY; left.value = offsetX + moveX; top.value = offsetY + moveY; } function dragEnd() { document.removeEventListener('mousemove', drag); document.removeEventListener('mouseup', dragEnd); } return { left, top, dragStart, }; }, }; </script> <style scoped> .draggable { position: absolute; width: 300px; height: 200px; background-color: #fff; border: 1px solid #ccc; box-shadow: 1px 1px 5px #ccc; } .title { cursor: move; padding: 10px; background-color: #eee; border-bottom: 1px solid #ccc; } .content { padding: 10px; } </style> ``` 在上面的代码中,我们使用了Vue3的Composition API来实现拖拽功能,使用了ref来定义left和top变量,使用了setup函数来定义组件逻辑。在dragStart函数中,我们记录了鼠标按下时的坐标和弹窗的偏移量,在drag函数中计算出弹窗的新位置,最后在dragEnd函数中移除了鼠标移动事件。 使用这个组件非常简单,只需要在父组件中引入Draggable组件,然后使用类似于普通的HTML标签的方式来使用它,例如: ```vue <template> <div> <button @click="showDialog = true">打开弹窗</button> <draggable v-if="showDialog" title="弹窗标题"> <p>这是弹窗内容</p> <button @click="showDialog = false">关闭弹窗</button> </draggable> </div> </template> <script> import Draggable from './Draggable.vue'; export default { components: { Draggable, }, data() { return { showDialog: false, }; }, }; </script> ``` 在上面的代码中,我们在父组件中引入了Draggable组件,并在父组件的data对象中定义了一个showDialog变量来控制弹窗的显示和隐藏。当点击打开弹窗按钮时,showDialog变量会被设置为true,从而显示弹窗;当点击弹窗内的关闭按钮时,showDialog变量会被设置为false,从而隐藏弹窗。 希望这个示例可以帮助你手写一个基于Vue3的弹窗拖拽组件!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值