嘿傢伙, 我似乎無法得到它的工作。我有可拖動的黑條,充當導航內容的頂部。jQuery:可拖動的酒吧 - 記得與cookie的位置?
我只是想瀏覽器記住用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 – Niklas 2011-06-06 11:22:06
所應用的資源加載哪些你沒有提到的是,你[使用jQuery插件(HTTP:// plugins.jquery.com/project/Cookie)。 – 2011-06-06 11:23:10