jQuery可拖动,重新加载页面后是否保存位置?
我做了大量的研究,但我需要一个更个性化的答案,我非常肯定保存可拖动的值需要一个cookies解决方案,不像jQuery.slider那样可以保存这样的值jQuery可拖动,重新加载页面后是否保存位置?,jquery,position,save,draggable,Jquery,Position,Save,Draggable,我做了大量的研究,但我需要一个更个性化的答案,我非常肯定保存可拖动的值需要一个cookies解决方案,不像jQuery.slider那样可以保存这样的值 //jQuery UI Slider jQuery( ".slider" ).slider({ animate: true, range: "min", value: $("input.slider-value").val(), min: 50,
//jQuery UI Slider
jQuery( ".slider" ).slider({
animate: true,
range: "min",
value: $("input.slider-value").val(),
min: 50,
max: 700,
step: 1,
//this gets a live reading of the value and prints it on the page
slide: function( event, ui ) {
jQuery( "#slider-result" ).html( ui.value );
jQuery( ".static_preview_image" ).height( ui.value );
},
//this updates the hidden form field so we can submit the data using a form
change: function(event, ui) {
jQuery('#hidden').attr('value', ui.value);
}
});
$( ".static_preview_image" ).css({height: $("input.slider-value").val()}); //This gives my element the height of the slider, it get stored in my slider function...
根据我的研究,draggable不能以同样的方式工作,它需要cookies
如果这是真的,有人能解释一下如何以最干净的方式使用cookies吗?我只想保存top,right,left,bottom值
这是我的可拖动函数
$( ".MYELEMNT" ).draggable({
containment: '#_moon_static_bg_status', // I set some containment
// Read this in another post, this outputs a live value of .MYELEMNT from the start of the drag.
start: function(event, ui) {
// Show start dragged position of image.
var Startpos = $(this).position();
$("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top);
},
// Same as above but prints the current position where MYELEMNT stops
stop: function(event, ui) {
// Show dropped position.
var Stoppos = $(this).position();
$("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
}
});
我想说,这就像写一些类似于如何获得滑块值的东西一样简单
$( ".static_preview_image" ).css({height: $("input.slider-value").val()});
但是我认为我需要使用cookies…两种方法:首先,cookies在页面刷新之间保持不变时可以工作。第二,如果您使用的是PHP,您可以使用服务器端的客户端会话数据来记住它,但是这种方法要求您在客户端将项目移动到服务器后从客户端发送数据(通过ajax或类似的方式),以通知服务器新的位置,以便它能够记住它
编辑:上面关于使用localstorage的评论听起来是一个很好的方法,我会先尝试一下。这看起来是一个很好的使用方法。现在就阅读它,这是一种不用数据库存储数据的方法吗?这似乎有点让我不知所措……localstorage是一种使用JavaScript在客户端数据库中存储信息的标准API。如果您不希望用户能够转到另一台计算机并访问其保存的数据,那么这应该是可行的。这个参考可能更容易理解。很好,我不知道本地存储。听起来是为客户端存储临时数据的一种非常好的方法。Cookie和localstorage有不同的用例。这个答案有一个非常好的描述:我在使用PHP,我实际上需要重用数据,这是管理端的预览,用户将元素拖动到所需位置,我希望在页面刷新后保存该位置,并在前端重用该位置。。localstorage听起来很酷,但我确实有一个数据库,听起来我需要使用ajax来发送数据,有人能帮我吗,只使用过一次ajax。。显然有很多方法可以存储可拖动的数据,但我想要一个简单且不需要大量代码的解决方案:)@MichaelJosephAubry-是的,你是对的。如果你需要将数据存储在服务器上,那么localstorage肯定不适合你。我们看了你的评论,我明白你现在说的“Cookie和localstorage有不同的用例”DaoWen说链接很好,它向我解释得更好一些,如果我读对了,它说无论哪种方式都需要cookie,“但不管怎样,您都希望将会话cookie作为cookie保留。”如果在管理端是本地存储,而在前端输出数据是服务器端呢?