jQuery.cookie = function (key, value, options) {
// key and at least value given, set cookie...
if (arguments.length > 1 && String(value) !== "[object Object]") {
options = jQuery.extend({}, options);
if (value === null || value === undefined) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
return (result = new RegExp('(?:^|;)' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
//------------------------------------------------END of plugin!---------------------------------
$('div:first').draggable({
stop: function(event, ui) {
$.cookie('div1x', $(this).css('left'));
$.cookie('div1y', $(this).css('top'));
}
});
$('div:last').draggable({
stop: function(event, ui) {
$.cookie('div2x', $(this).css('left'));
$.cookie('div2y', $(this).css('top'));
}
});
if($.cookie('div1x') != null){
$('div:first').css('left', $.cookie('div1x'));
}else{
$('div:first').css('left', '50px');
}
if($.cookie('div1y') != null){
$('div:first').css('top', $.cookie('div1y'));
}else{
$('div:first').css('top', '100px');
}
if($.cookie('div2x') != null){
$('div:last').css('left', $.cookie('div2x'));
}else{
$('div:last').css('left', '150px');
}
if($.cookie('div2y') != null){
$('div:last').css('top', $.cookie('div2y'));
}else{
$('div:last').css('top', '250px');
}
div
{
width:100px;
height:100px;
background-color:cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<div></div><div></div>
阅读[文档](http://api.jqueryui.com/draggable/#event-stop)兄弟 – deostroll 2015-02-24 05:57:46
@micheal你想要顶部和左侧的位置? – 2015-02-24 06:01:46
你可以尝试localstorage – wrick17 2015-02-24 06:05:33