当前位置:网站首页>js,e.pageX、pageY模态框拖动
js,e.pageX、pageY模态框拖动
2022-07-26 10:24:00 【唐策】
手写模态框拖动功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.tk-modal{
position: absolute;
width: 300px;
}
.tk-modal-header{
border: 1px solid #ccc;
width: 200px;
}
.tk-modal-close{
width: 50px;
}
</style>
</head>
<body>
<div class="tk-modal tk-ask-modal">
<div class="tk-modal-header">
<div class="tk-modal-close fright">关闭</div>
</div>
<div class="tk-modal-maintk-modal-main"></div>
</div>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
// 模态窗
// 模态窗关闭
tkModalOpen($(".tk-ask-modal"));
$(document).on("click",".tk-modal-close,.tk-modal-footer-cancel",function(){
tkModalClose($(this).parents(".tk-modal"));
});
var _move=false;//移动标记
var _x,_y;//鼠标离控件左上角的相对位置
$(document).on("click",".tk-modal-header",function(){
//alert("click");//点击(松开后触发)
}).on("mousedown",".tk-modal-header",function(e){
_move=true;
_x=e.pageX-parseInt($(this).parent(".tk-modal").css("left"));
_y=e.pageY-parseInt($(this).parent(".tk-modal").css("top"));
// console.log(_x+_x)
});
$(document).mousemove(function(e){
if(_move){
var x=e.pageX-_x;//移动时根据鼠标位置计算控件左上角的绝对位置
var y=e.pageY-_y;
$(".tk-modal").css({top:y,left:x});//控件新位置
// console.log(x)
}
}).mouseup(function(){
_move=false;
});
// 打开模态框
function tkModalOpen(obj){
$(".tk-overlay").show();
$("body").height($(window).height()).css({
"overflow-y": "hidden"
});
$left=(document.body.clientWidth-obj.width())/2;
$top=document.body.clientHeight>obj.height()?(document.body.clientHeight-obj.height())/2:0;
obj.css({"left":$left,"top":$top});
obj.show();
};
// 关闭模态框
function tkModalClose(obj){
obj.hide();
$(".tk-overlay").hide();
$("body").css({
"overflow-y": "visible"
});
};
})
</script>
</body>
</html>
边栏推荐
- Study notes at the end of summer vacation
- 【Halcon视觉】形态学膨胀
- 输入整数后输入整行字符串的解决方法
- Session based recommendations with recurrent neural networks
- 如何写一篇百万阅读量的文章
- 网易云UI模仿--&gt;侧边栏
- 【Halcon视觉】编程逻辑
- Data communication foundation TCPIP reference model
- Force deduction DFS
- Like, "new programmer" e-book is free for a limited time!
猜你喜欢
数通基础-二层交换原理
30 minutes to thoroughly understand the synchronized lock upgrade process
The difference between equals and = =
SQL优化的魅力!从 30248s 到 0.001s
数通基础-STP原理
AirTest
[Halcon vision] image filtering
Jpg to EPS
[qualcomm][network] QTI service analysis
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
随机推荐
Study notes of the third week of sophomore year
Data communication foundation STP principle
【Halcon视觉】图像的傅里叶变换
软件打不开了
输入整数后输入整行字符串的解决方法
Force deduction DFS
Learning about tensorflow (II)
Application of crosstab in SQL Server
Learning about tensor (III)
About automatic operation on Web pages
30分钟彻底弄懂 synchronized 锁升级过程
Flask framework beginner-03-template
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
数通基础-网络基础知识
简单化构造函数的继承方法(一)- 组合继承
【Halcon视觉】图像滤波
[C language] named type and anonymous type
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication