简体   繁体   English

HTML拖放持久性

[英]HTML drag and drop persistance

I would like give user a drag and drop screen where they can move things around. 我想给用户一个拖放屏幕,让他们可以移动东西。 I can use use HTML5 along with JQuery Draggable to achieve this experience. 我可以结合使用HTML5和JQuery Draggable来获得这种体验。

Question is, How do I persist the position of elements in server side so that new position can be recreated when ever user revisit the page. 问题是,如何在服务器端保留元素的位置,以便在用户再次访问页面时都可以重新创建新位置。 I do not want to keep track of each (x,y) co-ordinates and store in database, Instead I would like to dynamically generate a "custom" css file on the fly when user move elements, then store that css file in server. 我不想跟踪每个(x,y)坐标并存储在数据库中,而是想在用户移动元素时动态地动态生成一个“自定义” css文件,然后将该css文件存储在服务器中。 Unfortunately, I only has idea I don't know how to keep track of movements and generate css file. 不幸的是,我只有一个主意,我不知道如何跟踪运动并生成css文件。

Any tips or some framework which does this will help. 任何提示或一些框架,这将有所帮助。

Just played with this http://jqueryui.com/draggable/ , I observed whenever we move the div it changes its inline style (top, left, right, bottom). 刚玩过这个http://jqueryui.com/draggable/ ,我观察到每当我们移动div时,它都会更改其内联样式(上,左,右,下)。 I copied its style attribute after a movement and refreshed the page. 我在移动后复制了其style属性,并刷新了页面。

After refresh the div comes back to original position, now I pasted the copied style attribute to the div and it took its previous state where I copied the style. 刷新后,div返回到原始位置,现在我将复制的style属性粘贴到div上,并采用了复制样式的先前状态。

SO, you can keep an array of elements Ids as key and style attribute as value. 因此,您可以将元素ID的数组保留为键,将样式属性保留为值。 Upload it to server or you can use localStorage @ client. 将其上传到服务器,也可以使用localStorage @ client。 On the page reload fetch the array and set the inline styles back :) 在页面上重新加载以获取数组并将内联样式设置回:)

I had the similar requirement in past project but the drop targets were fixed....like tiles can be dropped onto an array of <li> elements. 我在过去的项目中也有类似的要求,但是放置目标是固定的。...就像瓦片可以放置在<li>元素数组上。 I handled it by storing only the indices of <li> element in order. 我通过仅按顺序存储<li>元素的索引来处理它。

I suggest you look into http://diveintohtml5.info/storage.html to store the values on the client side. 建议您查看http://diveintohtml5.info/storage.html,以将值存储在客户端。 Otherwise you will can sore the value as a YAML or JSON on the server, but that will a lot of more how to than is relevant for this thread. 否则,您将可以在服务器上将值作为YAML或JSON痛打,但与该线程相关的操作要多得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM