Using sessionstorage for saving dark mode

↘锁芯ラ 提交于 2019-12-30 16:21:14

问题


I successfully added dark mode to my site using This fiddle

JS:

$('#mode').change(function(){   

if ($(this).prop('checked'))
{
    $('body').addClass('dark-mode');
}
else
{
    $('body').removeClass('dark-mode');
}
});

However, when refreshing the page the theme switches back obviously. I can't find out how to use sessionstorage to keep dark mode over the domain.

Can someone help me? Thanks!


回答1:


You can use local storage for storing the data

function darkmode(){
    $('body').addClass('dark-mode');
    localStorage.setItem("mode", "dark");
    }



function nodark(){
        $('body').removeClass('dark-mode');
        localStorage.setItem("mode", "light");
        }

  if(localStorage.getItem("mode")=="dark")
        darkmode();
  else
    nodark();

$('#mode').change(function(){   

    if ($(this).prop('checked'))
    {
        darkmode();
    }
    else
    {
        nodark();
    }

});


来源:https://stackoverflow.com/questions/42468553/using-sessionstorage-for-saving-dark-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!