欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现可拖动的浮动层完整代码

程序员文章站 2023-10-28 16:28:16
以下是使用jquery实现可拖动的浮动层的完整代码,复制保存到html文件就可以体验效果。 光标移动到层上,按住鼠标就可以拖动层。 . 代码如下:

以下是使用jquery实现可拖动的浮动层的完整代码,复制保存到html文件就可以体验效果。

光标移动到层上,按住鼠标就可以拖动层。

. 代码如下:


<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jquery实现可拖动的浮动层(版本2) - 柯乐义</title>
<script type="text/javascript" src="https://www.keleyi.com/keleyi/pmedia/jquery-1.8.2.min.js"></script>
<style type="text/css">
.box{ position:absolute; top:200px; left:400px; width:200px; height:200px; background:#8a9bca; cursor:move;}
</style>
<script type="text/javascript">
$(document).ready(function (e) {
$(".box").mousedown(function (e) {
idiffx = e.pagex - $(this).offset().left;
idiffy = e.pagey - $(this).offset().top;
$(document).mousemove(function (e) {
$(".box").css({ "left": (e.pagex - idiffx), "top": (e.pagey - idiffy) });
});
});
$(".box").mouseup(function () {
$(document).unbind("mousemove");
});
});
</script>
</head>
<body style="width:2000px">
<p>欢迎光临!</p>
<p class="box" id="drigging">光标移动到层上,<br />按住鼠标就可以拖动该层。<br /><br /><br /><br />柯乐义</p>
<p>www.keleyi.com</p>
</body>
</html>