简体   繁体   English

如何保持页面重新加载菜单状态

[英]How to keep the menu state on page reload

I have the following code snippet and wanted to know if there is a possibility to update it achieving this menu behaviour: 我有以下代码片段,想知道是否有可能通过更新来实现此菜单行为:

Step 1. On mouse hover Link 1 ----> it will be translated 1.5em to the right (already set); 步骤1.在鼠标悬停链接1 ---->上,它将转换为1.5em到右侧(已设置);

Step 2. On Link 1 click ----> the menu button will remain fixed in place at the already translated position (done, special thanks to @guest271314 ) on the page reload too, until a new menu button is clicked (not set yet) and another page is loaded . 步骤2.在Link 1上,单击---->,菜单按钮也将保持在页面重新加载的位置(已完成,特别感谢@ guest271314,直到 再次 单击新菜单按钮 (未设置) 为止 ), 然后加载另一个页面

note: next/prev buttons code section, remain unchanged (or can be edited if it's a must, in order to remain functional). 注意:“下一个/上一个”按钮的“代码”部分保持不变(或必须编辑才能保持其功能)。

note2: I have to mention that in the end, the solution will be implemented in wordpress not into a static html sitepage. 注意2:最后,我不得不提到,该解决方案将在wordpress中实现,而不是在静态html页面中实现。

 $(function () { $('nav ul li').click(function (e) { //Set the aesthetics (similar to :hover) $('nav ul li') .not(".clicked").removeClass('hovered') .filter(this).addClass("clicked hovered") .siblings().toggleClass("clicked hovered", false); }).hover(function () { $(this).addClass("hovered") }, function () { $(this).not(".clicked").removeClass("hovered") }); var pageSize = 4, $links = $(".pagedMenu li"), count = $links.length, numPages = Math.ceil(count / pageSize), curPage = 1; showPage(curPage); function showPage(whichPage) { var previousLinks = (whichPage - 1) * pageSize, nextLinks = (previousLinks + pageSize); $links.show(); $links.slice(0, previousLinks).hide(); $links.slice(nextLinks).hide(); showPrevNext(); } function showPrevNext() { if ((numPages > 0) && (curPage < numPages)) { $("#nextPage").removeClass('hidden'); $("#msg").text("(" + curPage + " of " + numPages + ")"); } else { $("#nextPage").addClass('hidden'); } if ((numPages > 0) && (curPage > 1)) { $("#prevPage").removeClass('hidden'); $("#msg").text("(" + curPage + " of " + numPages + ")"); } else { $("#prevPage").addClass('hidden'); } } $("#nextPage").on("click", function () { showPage(++curPage); }); $("#prevPage").on("click", function () { showPage(--curPage); }); }); 
 .hidden { display: none; } body { font: normal 1.0em Arial, sans-serif; } nav.pagedMenu { color: red; font-size: 2.0em; line-height: 1.0em; width: 8em; position: fixed; top: 50px; } nav.pagedMenu ul { list-style: none; margin: 0; padding: 0; } nav.pagedMenu ul li { height: 1.0em; padding: 0.15em; position: relative; border-top-right-radius: 0em; border-bottom-right-radius: 0em; -webkit-transition: -webkit-transform 220ms, background-color 200ms, color 500ms; transition: transform 220ms, background-color 200ms, color 500ms; } nav.pagedMenu ul li.hovered { -webkit-transform: translateX(1.5em); transform: translateX(1.5em); } nav ul li:hover a { transition: color, 1200ms; color: red; } nav.pagedMenu ul li span { display:block; font-family: Arial; position: absolute; font-size:1em; line-height: 1.25em; height:1.0em; top:0; bottom:0; margin:auto; right: 0.01em; color: #F8F6FF; } a { color: gold; transition: color, 1200ms; text-decoration: none; } #pagination, #prevPage, #nextPage { font-size: 1.0em; color: gold; line-height: 1.0em; padding-top: 250px; padding-left: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="pagedMenu"> <ul style="font-size: 28px;"> <li class="" style="margin-bottom: 5px;"><a href="#">Link 1</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 2</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 3</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 4</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 5</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 6</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 7</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 8</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 9</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 10</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 11</a></li> <li class="" style="margin-bottom: 5px;"><a href="#">Link 12</a></li> </ul> </nav> <div id="pagination"> <a href="#" id="prevPage" class="hidden">Previous</a>&nbsp;&nbsp; <a href="#" id="nextPage" class="hidden">Next</a> <span id="msg"></span> </div> 

LE: regarding @Lars comment: LE:关于@Lars评论:

This state will be applied per all users (I think?, it's the best option as long as the menu will be displayed on every pages, unconditioned by the browser type or the session); 此状态将应用于所有用户 (我认为?,这是最好的选择,只要菜单将显示在每个页面上,而不受浏览器类型或会话的限制);

Also regarding the storage location, it's not a problem saving the state locally, server side, but it will be great if I have some pro/cons to make the right decision; 另外,关于存储位置,在服务器端本地保存状态不是问题,但是如果我有一些赞成/反对意见,可以做出正确的决定,那将会很好。

In the end, if this helps, in order to see the whole picture, you can use this live link as example ; 最后,如果有帮助,为了查看整个图片,您可以使用此实时链接作为示例 there is a similar menu with the above described and regarding the state, if there is a model that could be implemented here, I'll be glad to find it. 这里有一个与上述类似的菜单,并且关于状态,如果这里有一个可以实现的模型,我会很高兴找到它。

You can save menu (and page) status in a localStorage variable. 您可以将菜单(和页面)状态保存在localStorage变量中。 On page load check if the variable exists and set correct Link/page status. 在页面加载时,检查变量是否存在,并设置正确的链接/页面状态。

https://github.com/julien-maurel/jQuery-Storage-API https://github.com/julien-maurel/jQuery-Storage-API

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

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