日期:2014-05-16 浏览次数:20430 次
$("#drag").mousedown(function (e) {
iDiffX = e.pageX - $(this).offset().left;
iDiffY = e.pageY - $(this).offset().top;
$(document).mousemove(function (e) {
$("#drag").css({ "left": (e.pageX - iDiffX), "top": (e.pageY - iDiffY) });
});
});
$("#drag").mouseup(function () {
$(document).unbind("mousemove");
});
一般放在文档加载完的事件里
$(document).ready(function() {
$("#drag").mousedown(function (e) {
iDiffX = e.pageX - $(this).offset().left;
iDiffY = e.pageY - $(this).offset().top;
$(document).mousemove(function (e) {
$("#drag").css({ "left": (e.pageX - iDiffX), "top": (e.pageY - iDiffY) });
});
});
$("#drag").mouseup(function () {
$(document).unbind("mousemove");
});
});
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.box{position:absolute;width:100px;height:100px;background:#000;opacity:0.5;filter:alpha(opacity:50);}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function(){
$("#drag").mousedown(function (e) {
iDiffX = e.pageX - $(this).offset().left;
iDiffY = e.pageY - $(this).offset().top;
$(document).mousemove(function (e) {
&nb