Javascript 我如何保存拖拽的位置
对于jQuery,我有点不在行,我想知道如何在代码中保存draggables的位置,以便在浏览器刷新时它们保持在相同的位置。这是我的代码:Javascript 我如何保存拖拽的位置,javascript,jquery,html,css,Javascript,Jquery,Html,Css,对于jQuery,我有点不在行,我想知道如何在代码中保存draggables的位置,以便在浏览器刷新时它们保持在相同的位置。这是我的代码: <style> #draggable1 { width: 25px; height: 25px; margin-right: 10px; margin-bottom: 10px; text-align: center; color: white;
<style>
#draggable1 {
width: 25px;
height: 25px;
margin-right: 10px;
margin-bottom: 10px;
text-align: center;
color: white;
background-color: #19467c;
border-radius: 3px;
float: left;
font-size: 13px;
line-height: 15%;
}
#draggable2 {
width: 25px;
height: 25px;
margin-right: 10px;
margin-bottom: 10px;
text-align: center;
color: white;
background-color: #19467c;
border-radius: 3px;
float: left;
font-size: 13px;
line-height: 15%;
}
#draggable3 {
width: 25px;
height: 25px;
margin-right: 10px;
margin-bottom: 10px;
text-align: center;
color: white;
background-color: #19467c;
border-radius: 3px;
cursor: grab;
float: left;
font-size: 13px;
line-height: 15%;
}
#draggable4 {
width: 25px;
height: 25px;
margin-right: 10px;
margin-bottom: 10px;
text-align: center;
color: white;
background-color: #19467c;
border-radius: 3px;
cursor: grab;
float: left;
font-size: 13px;
line-height: 15%;
}
#draggable5 {
width: 25px;
height: 25px;
margin-right: 10px;
margin-bottom: 10px;
text-align: center;
color: white;
background-color: #19467c;
border-radius: 3px;
cursor: grab;
float: left;
font-size: 13px;
line-height: 15%;
}
#container{
width: 35px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#draggable1" ).draggable();
} );
$( function() {
$( "#draggable2" ).draggable();
} );
$( function() {
$( "#draggable3" ).draggable();
} );
$( function() {
$( "#draggable4" ).draggable();
} );
$( function() {
$( "#draggable5" ).draggable();
} );
</script>
</head>
<body>
<div id="container">
<div id="draggable1" class="ui-widget-content">
<p>1</p>
</div>
<div id="draggable2" class="ui-widget-content">
<p>2</p>
</div>
<div id="draggable3" class="ui-widget-content">
<p>3</p>
</div>
<div id="draggable4" class="ui-widget-content">
<p>4</p>
</div>
<div id="draggable5" class="ui-widget-content">
<p>5</p>
</div>
</div>
<img src="motor.jpg" style="float;">
</body>
</html>
#draggable1{
宽度:25px;
高度:25px;
右边距:10px;
边缘底部:10px;
文本对齐:居中;
颜色:白色;
背景色:#19467c;
边界半径:3px;
浮动:左;
字体大小:13px;
线高:15%;
}
#draggable2{
宽度:25px;
高度:25px;
右边距:10px;
边缘底部:10px;
文本对齐:居中;
颜色:白色;
背景色:#19467c;
边界半径:3px;
浮动:左;
字体大小:13px;
线高:15%;
}
#可拖动3{
宽度:25px;
高度:25px;
右边距:10px;
边缘底部:10px;
文本对齐:居中;
颜色:白色;
背景色:#19467c;
边界半径:3px;
光标:抓取;
浮动:左;
字体大小:13px;
线高:15%;
}
#draggable4{
宽度:25px;
高度:25px;
右边距:10px;
边缘底部:10px;
文本对齐:居中;
颜色:白色;
背景色:#19467c;
边界半径:3px;
光标:抓取;
浮动:左;
字体大小:13px;
线高:15%;
}
#可拖动5{
宽度:25px;
高度:25px;
右边距:10px;
边缘底部:10px;
文本对齐:居中;
颜色:白色;
背景色:#19467c;
边界半径:3px;
光标:抓取;
浮动:左;
字体大小:13px;
线高:15%;
}
#容器{
宽度:35px;
}
$(函数(){
$(“#draggable 1”).draggable();
} );
$(函数(){
$(“#draggable 2”).draggable();
} );
$(函数(){
$(“#draggable 3”).draggable();
} );
$(函数(){
$(“#draggable 4”).draggable();
} );
$(函数(){
$(“#draggable 5”).draggable();
} );
一,
二,
三,
四,
五,
正如这篇文章所建议的:
实际上,您可以将位置保存在JSON文件中,每次加载文档时都会读取该文件,并在拖动鼠标后启动鼠标时进行更新
var sPositions=localStorage.positions | |“{}”,
positions=JSON.parse(sPositions);
$。每个(位置、功能(id、位置){
$(“#”+id).css(pos)
})
$(“#可拖动3”)。可拖动({
安全壳:“安全壳包装”,
卷轴:错,
停止:功能(事件、用户界面){
positions[this.id]=ui.position
localStorage.positions=JSON.stringify(positions)
}
});代码>
.draggable{
宽度:90px;
高度:90px;
填充:0.5em;
浮动:左;
利润率:0 10px 10px 0;
}
#可拖动,#可拖动2{
边缘底部:20px;
}
#拖拉的{
光标:n-调整大小;
}
#可拖动的3{
光标:移动;
}
#安全壳包装{
宽度:700px;
高度:500px;
边框:1px实心#000;
填充物:5px;
}
h3{
清除:左;
}
在下面测试我:
拖我
嘿,马特,这应该是对OP帖子的评论,或者你需要给他们一个代码解决方案。不要只是链接到另一个答案。我还不能评论,因为它需要50个代表。但是谢谢,我会用代码编辑这个答案。没关系,如果你不能评论,你需要等到有足够的代表发表评论。通过提交好的解决方案,您将获得声誉。谢谢您的回答!当我使用这段代码时,对象不再是可拖动的,你知道为什么吗?(投票给你了,但是因为我的代表它不会显示;-)嗯,我觉得可能是因为你使用的ID。在提供的代码片段中,只有一个“draggable”div,并且使用了id#draggable3。因此,请确保您有#draggable1、#draggable2、#draggable3、#draggable4、#draggable5{光标:移动;}