title: 简单拖拽保存
date: 2014-04-15 15:59:05
tags: js
category: javascript
<html>
<head>
<title>提示</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
.drag{
width:200px;
height:200px;
background:rgba(204,255,102,1);
position:absolute;
cursor:move;
top:100px;left:20px;
text-align:center;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(function () {
if(window.localStorage){
$(".drag").css({top:localStorage.getItem("top"),left:localStorage.getItem("left") });
}
var _move = false;
var _x, _y;
$(".drag").click(function () {
}).mousedown(function (e) {
_move = true;
_x = e.pageX - parseInt($(".drag").css("left"));
_y = e.pageY - parseInt($(".drag").css("top"));
$(".drag").fadeTo(20, 0.5);
});
$(document).mousemove(function (e) {
if (_move) {
var x = e.pageX - _x;
var y = e.pageY - _y;
$(".drag").css({
top: y,
left: x
});
window.localStorage.setItem("top",y);
window.localStorage.setItem("left",x);
}
}).mouseup(function () {
_move = false;
$(".drag").fadeTo("fast", 1);
});
});
</script>
</head>
<body >
<div class="drag">O(∩_∩)O哈哈~</div>
</body>
</html>