SoFunction
Updated on 2025-03-06

jQuery drag div, move div, pop-up layer implementation principle and example


<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Qing's Web</title>
<script src="./jquery-1.7." type="text/javascript"></script>
<style type="text/css">
.footer {
position: fixed;
bottom: 0;
width: 100%;
}
.moveBar {
position: absolute;
width: 250px;
height: 300px;
background: #666;
border: solid 1px #000;
}
#banner {
background: #52CCCC;
cursor: move;
}
</style>
</head>
<body style="padding-top: 50px;">

<div class="moveBar">
<div >Press and hold here to move the current div</div>
<div class="content">This is other content</div>
</div>
<div class="footer">
<p align="center" class="label">ALL Rights Reserved Qing All Rights Reserved Qing Copyright</p>
</div>
<script>
jQuery(document).ready(
function () {
$('#banner').mousedown(
function (event) {
var isMove = true;
var abs_x = - $('').offset().left;
var abs_y = - $('').offset().top;
$(document).mousemove(function (event) {
if (isMove) {
var obj = $('');
({'left': - abs_x, 'top': - abs_y});
}
}
).mouseup(
function () {
isMove = false;
}
);
}
);
}
);
</script>
</body>
</html>