【发布时间】:2011-06-06 11:15:16
【问题描述】:
大家好, 我似乎无法让它工作。我有可拖动的黑条作为内容顶部的导航。
我只是想让浏览器记住带有 cookie 的栏的位置。每当用户拖动条并单击其他内容(页面刷新)时,条应保持在最后一个位置。
我似乎无法让它工作。此外,我希望在用户第一次访问该网站时对条形图进行简单的随机分布。
我已经建立了一个简单的例子。也许你知道我在这里做错了什么:http://jsfiddle.net/RZQ8H/
$("ul.bars li").draggable({
stop: function(event, ui) {
var currentPos = $(this).position();
var currentTop = Math.round(currentPos.top);
// save cookie when stopped dragging
$.cookie('position' + $(this).index('li').toString(), currentTop.toString());
}
});
页面加载时的位置...
// initial position of elements
$("ul.bars li").each(function() {
// if no cookie is set (first visit to page) all bars should be distributed
// randomly across the document
if ( $.cookie('position'+ $(this).index('li').toString()) ) {
var doc = $(document).height() - 50;
// random distribution of elements
$("ul.bars li").each(function() {
var randPos = Math.random(0)*doc;
$(this).css('top' , randPos+'px');
});
} else {
// if a cookie exists (the bars have already been dragged)
// remember the position
var savedPos = $.cookie('position' + $(this).index('li').toString());
$(this).css({ top: savedPos + 'px' });
}
});
【问题讨论】:
-
cookie 插件没有从 jsfiddle 上的应用资源加载
-
你没说你是using a jQuery plugin。
标签: jquery jquery-ui cookies drag-and-drop draggable