我爲我的網站創建了一個面板div,當我們點擊按鈕時滑出頁面,當我們再次點擊時再次出現。在localStorage中保存菜單位置
HTML很簡單,只有面板顯示/隱藏它的按鈕。
<div id="panel">
<div id="close"></div>
</div>
而且我腳本但存儲不起作用:
function storePanel(right){
var panelData = localStorage.getItem('panel');
if (panelData!=undefined)
panelData=JSON.parse(panelData);
else
panelData={"right":"24px"};// a starting value
panelData["top"]=top;
panelData["right"]=right;
localStorage.setItem('panel', JSON.stringify(panelData));
}
// hide/show panel
document.getElementById('close').onclick = function() {
var panel = document.getElementById("panel");
var right = panel.style.right;
var icon = document.getElementById("icone");
if (right == "24px") {
right = "-200px";
}
else {
right = "24px";
}
panel.style.right = right;
storePanel(right);
};
這裏是jsfiddle
等待,爲什麼你需要保存呢?你所做的一切是否將它從視圖中移出? – Bioto
@NicholasYoung我需要保存他的位置,如果我們關閉菜單,在重新加載菜單上關閉。 – remibenault
你可以使用一個cookie。 – Adjit