cookie、localStorage和jquery数据用于存储大小和位置信息
Cookies, localStorage vs. jquery data for storing size and position info
为可调整大小的可拖动div存储大小和位置信息的最佳选择是什么?我有一个页面,将有许多可调整大小的可移动div。div的状态(顶部,左侧,宽度,高度)必须在用户再次访问页面时恢复。我用饼干做了一个测试,效果不错,但如果有很多div,这可能就不正确了。我测试了jquery数据,但似乎有一个问题。这段代码对于在下次页面加载时检索到的数据返回undefined。
<script>
function getdivinfo() {
var position = $( "#compage" ).position();
var width = $( "#compage" ).width();
var height= $( "#compage" ).height();
var left = position.left;
var top = position.top;
$( "#compage" ).data("layout", { "dleft": left, "dtop": top, "dwidth": width, "dheight": height });
}
function divlayout() {
var restleft = $( "#compage" ).data("layout").dleft;
var resttop = $( "#compage" ).data("layout").dtop;
var restwidth = $( "#compage" ).data("layout").dwidth;
var restheight = $( "#compage" ).data("layout").dheight;
$( "#compage" ).css("top", resttop);
$( "#compage" ).css("left", restleft);
$( "#compage" ).css("width", restwidth);
$( "#compage" ).css("height", restheight);
}
</script>
HTML信息<body onload="divlayout();">
<div class="demo">
<div id="compage" class="ui-widget-content">
<h3 class="ui-widget-header">Sample</h3>
</div>
<a href="#" onclick="getdivinfo();">Get div info</a>
<a href="#" onclick="savedivinfo();"> Save div info</a>
</div><!-- End demo -->
JQuery数据不持久,它只在HTML加载时有效。本地存储是最好的解决方案,但是旧的浏览器不支持本地存储,而且仍然有很多旧的浏览器。cookie是一个很好的解决方案,最终使用更少的具有较长值的cookie,或者使用许多具有较小值的cookie,这取决于您认为将达到的限制。
显然,将它保存在服务器端将是伟大的:)
决定使用包含转换为字符串的数组的cookie。对于每个div,我创建一个cookie,该cookie使用join函数从变量数组中生成一个字符串。为了读取cookie信息,我将字符串拆分回一个数组。
<script>
function getdivinfo() {
var position = $( "#compage" ).position();
var width = $( "#compage" ).width();
var height= $( "#compage" ).height();
var left = position.left;
var top = position.top;
var divarray = new Array();
divarray[0] = left;
divarray[1] = top;
divarray[2] = width;
divarray[3] = height;
arraycookie = divarray.join('|');
setCookie("compage", arraycookie, 600);
}
function restorediv() {
var arraycookie = getCookie("compage");
if ( arraycookie == "" ) return;
var divarray = arraycookie.split('|');
var restleft = divarray[0];
/* if (restleft == "") return; */
var resttop = divarray[1];
/* if (resttop == "") return; */
var restwidth = divarray[2];
/* if (restwidth == "") return; */
var restheight = divarray[3];
/* if (restheight == "") return; */
$( "#compage" ).css("top", resttop);
$( "#compage" ).css("left", restleft);
$( "#compage" ).css("width", restwidth);
$( "#compage" ).css("height", restheight);
}
</script>
HTML信息<body onload="restorediv();">
<div class="demo">
<div id="compage" class="ui-widget-content">
<h3 class="ui-widget-header">Sample</h3>
</div>
<a href="#" onclick="getdivinfo();">Get div info</a>
</div><!-- End demo -->
相关文章:
- 谷歌地图API-信息窗口显示谷歌位置API信息
- 更改信息窗口的位置显示在谷歌地图API v3中
- 谷歌地图API地理位置文本搜索地点详细信息
- 谷歌地图偏移标记/信息窗口位置(panTo/setCenter)
- 点的访问器's修改海拔高度的位置标记信息
- 是否可以在javascript中基于地理位置获取街道详细信息
- 地图位置带有照片和其他详细信息的详细信息
- 如何使用html/html5中的用户输入获取完整的地理位置详细信息
- 休息标记位置 + 信息窗口位置
- 在谷歌地图中设置信息窗口位置
- 包含 Mongodb 中位置信息的文档
- 无法使用 JavaScript 获取位置信息
- 将地图位置信息从PHP传递到javascript,以便与Google地图v3api一起使用
- 试图在领英与javascript api的第一次连接上拉位置信息
- 在Google Maps API的标记上显示位置信息
- 如何获取用户的浏览器详细信息、IP地址和位置信息
- Mvc4:传递位置信息到javascript变量的地理位置
- 触发谷歌地图位置信息窗口
- cookie、localStorage和jquery数据用于存储大小和位置信息
- Google 如何在未经用户许可的情况下获取当前位置信息