简体   繁体   English

localStorage 保存导航栏状态

[英]localStorage to save navbar state

I want to use localStorage to store the sidebar toggle so browser can remember the last toggle.我想使用 localStorage 来存储侧边栏切换,以便浏览器可以记住上次切换。 Can someone please take a look at this code and tell me why it's not working?有人可以看看这段代码并告诉我为什么它不起作用吗?

$(document).ready(function() {
    var sidebarshow = localStorage.getItem('sidebar') == 'true';
    $('#sidebar').toggleClass('active', sidebar);

    $("#toggle").click(function() {
         $("#sidebar").toggleClass("slow",function() {
             localStorage.setItem('sidebarshow', 'active');
         });
         $("#sidebar").toggleClass("active");
    }); 
}); 

CSS CSS

#sidebar.active {
    position: fixed;
    top: 0;
    left: 0;
    background: #1F6482;
    width: 250px;
    height: 100%;
    color: #FFF;
}
#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    background: #1F6482;
    width: 75px;
    height: 100%;
    color: #FFF;
}

Problem: 1) you did not set the localStorage for key 'sidebar', But accessing will result into unexpected values.问题:1)您没有为键'侧边栏'设置localStorage,但访问将导致意外值。

var sidebarshow = localStorage.getItem('sidebar') == 'true';

Problem: 2) you are setting the localStorage for key 'sidebarshow', But you are not used any where.问题:2)您正在为键'sidebarshow'设置localStorage,但您没有在任何地方使用。 (at least in above code) (至少在上面的代码中)

localStorage.setItem('sidebarshow', 'active');

Think of localStorage as a just object with key and values are of type string .将 localStorage 视为具有键和值的类型为 string 的对象 Make sure to set the value first and get them later.确保先设置值,然后再获取它们。

Here is sample code working.这是示例代码工作。

 $(document).ready(function() { /* if (window && window.localStorage.getItem('sidebar') === 'active') { // if it active show it as active $("#sidebar").addClass("active"); } else { $("#sidebar").removeClass("active"); } */ $("#toggle").click(function() { $("#sidebar").toggleClass("active"); var updated = ''; if (window.localStorage.getItem('sidebar') === 'active') { updated = 'not_active'; } else { updated = 'active'; } window.localStorage.setItem('sidebar', updated); }); });
 #sidebar.active { position: fixed; top: 0; left: 0; background: #1F6482; width: 250px; height: 100%; color: #FFF; } #sidebar { position: fixed; top: 0; left: 0; background: #1F6482; width: 75px; height: 100%; color: #FFF; } #toggle { margin-left: 400px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="sidebar"> Sidebar here </div> <button id="toggle"> toggle </button>

Please check the screenshot of chrome debug console example with slightly changed to test.请检查 chrome 调试控制台示例的屏幕截图,稍作更改以进行测试。

在此处输入图片说明

If you're using the Admin-LTE v-3 package for front-end and wants to save the state of sidebar-collapse use this technique........ and an above solution is also working... https://stackoverflow.com/a/59504461/8455396 Just read carefully and perform an action.如果您在前端使用 Admin-LTE v-3 软件包并希望保存侧边栏折叠状态,请使用此技术........上述解决方案也有效... https: //stackoverflow.com/a/59504461/8455396只需仔细阅读并执行操作。

html tag hamburger button html 标签hamburger button

<a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
<script>
$(document).ready(function() {
 /*
  check condition what value is stored in local storage if nothing then default 
  sidebar action would be performed you don't have to worry about this.
  In Admin lte version 3 sidebar-collapse added in body tag inspect you browser and check
 */
 if (window.localStorage.getItem('sidebar-toggle-collapsed') === 'false') {
        // if it is off sidebar-collapse close
        $("body").addClass("sidebar-collapse");
    } else {
        $("body").removeClass("sidebar-collapse");
 }

// Perform click event action

 $("[data-widget='pushmenu']").click(function() {
        var buttonClickedValue = '';
        if (window.localStorage.getItem('sidebar-toggle-collapsed') === 'false') {
            buttonClickedValue = 'true';
        } else {
            buttonClickedValue = 'false';
        }
       
        // store value in local storage
        window.localStorage.setItem('sidebar-toggle-collapsed', buttonClickedValue );
  });

});
</script>

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

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