【问题标题】:How to keep the menu state on page reload如何在页面重新加载时保持菜单状态
【发布时间】:2015-12-17 17:10:18
【问题描述】:

我有以下代码 sn-p 并想知道是否有可能对其进行更新以实现此菜单行为:

第 1 步。鼠标悬停时 Link 1 ----> 它将向右平移 1.5em(已设置);

第 2 步。在 Link 1 上单击 ----> 菜单按钮将保持固定在已翻译的位置(完成,特别感谢 @guest271314页面也会重新加载,直到单击新的菜单按钮(尚未设置)并加载另一个页面

注意:next/prev 按钮代码部分,保持不变(如果必须,可以编辑,以保持功能)。

note2:我不得不提一下,最后,解决方案将在 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:关于@Lars 评论:

此状态将适用于每个所有用户(我认为,这是最好的选择,只要菜单将显示在每个页面上,不受浏览器类型或会话的限制);

关于存储位置,在服务器端本地保存状态不是问题,但如果我有一些利弊来做出正确的决定,那就太好了;

最后,如果这有帮助,为了看全图,你可以使用这个直播链接as example;上面有一个类似的菜单,关于状态,如果有可以在这里实现的模型,我会很高兴找到它。

【问题讨论】:

  • 您要求在网络环境中保持国家的毅力。这有很多考虑。例如,是否应按浏览器/会话/用户/所有用户应用此状态。您可以在本地、服务器端保存状态,甚至使状态 URL 依赖(yoursite.com/link1 -> yourisite.com/link2)。目前尚不清楚您正在寻找哪种解决方案。您可能希望更清楚地指定用例。
  • 哦,我不知道,谢谢您的信息...只要这是一个公共站点的菜单,将不受限制地应用(如果这是问题的话)可供所有用户使用.我正在考虑这个解决方案,利用@guest271314 制作的$.holdReady()history,但不太确定如何在我的sn-p stackoverflow.com/a/30144363/4642215 中应用该序列。也请看看这个live example。这就是我正在寻找的东西,除了我可以在没有翻译的情况下点击鼠标悬停时已经翻译的位置。
  • 关于上面提到的live example,为了看全图,尝试在worksbooks等菜单项之间导航看看他们的表现如何。
  • 快速浏览了“现场示例”......在我看来,他们使用 AJAX 加载新内容,如果是这样,您无需担心在加载之间保持状态,因为菜单永远不会重新加载......这样你几乎可以用很少的编码对菜单的状态做任何事情
  • 如果你愿意,你可以通过带有变量的数据库来完成,我觉得这样做更容易。

标签: javascript jquery html css wordpress


【解决方案1】:

您可以将菜单(和页面)状态保存在 localStorage 变量中。在页面加载时检查变量是否存在并设置正确的链接/页面状态。

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

【讨论】:

  • 听起来不错,但正如我刚才所说,我是一个纯粹的菜鸟,只是抓取并整理代码,有时可以工作
  • 类似(未测试)$(function () { storage=$.localStorage; if(storage.isSet('page')){ var page= storage.get('page'); showPage(page); } if(storage.isSet('link')){ var link = storage.get('link'); $('nav ul li:nth-child('+link+')').addClass ("点击悬停"); } ... $('nav ul li').click(function (e) { storage.set('link',$('nav ul li').index($(this) )); ... ... $("#nextPage").on("click", function () { showPage(++curPage); storage.set('page',curPage); }); .. . /* 与上一个相同 */
  • 查看更新代码jsfiddle.net/ex3ntia/hrt2s221/1你使用的是jQuery,不是$
  • 因为您的网站与您的 jsfiddle 不同 :) 您有多个“nav ul li”。有一个隐藏的(#mobile-menu)和一个可见的。所以你需要使用 jQuery('nav:visible ul li') 作为选择器jsfiddle.net/ex3ntia/hrt2s221/4
  • 哈哈好吧,这些不是错误,是用于调试的“console.log”命令。只需从脚本中删除 :)
猜你喜欢
  • 2015-11-23
  • 2021-12-08
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
相关资源
最近更新 更多