zoukankan      html  css  js  c++  java
  • 页面切换主题风格,利用本地缓存

    //默认为黑色主题,全局变量
    var saveThemeColor = {
    'Name': 'ThemeColor',
    'Color': 'darktheme'
    }

    var storage = getLocalStorage();

    // 判断用户是否已有自己选择的主题风格
    if (storageLoad('ThemeColor')) {
    var themecolor = storageLoad('ThemeColor').Color;

    $('body').attr('class',themecolor);

    } else {
    storageSave(saveThemeColor);
    $('body').attr('class', 'darktheme');
    }


    // 本地缓存
    function storageSave(obj) {
    storage.setItem(obj.Name, JSON.stringify(obj));
    }

    function storageLoad(objectName) {
    if (storage.getItem(objectName)) {
    return JSON.parse(storage.getItem(objectName));
    } else {
    return false;
    }
    }

    //兼容只支持globalStorage的浏览器
    function getLocalStorage(){
    if(typeof localStorage == 'object'){
    return localStorage;
    }else if (typeof globalStorage == 'object') {
    return globalStorage[location.host];
    } else {
    throw new Error('Local storage not available');
    }
    }

    //切换主题
    $('.themeli').find('span').on('click',function(){

    $('body').attr('class', $(this).attr('data-color'))

    saveThemeColor.Color = $(this).attr('data-color');
    // 保存选择项
    storageSave(saveThemeColor);

    });

  • 相关阅读:
    myeclipse连接并运行sql文件
    搜集的一些常用的方法
    使用SolrJ代码导入,发布搜索服务
    solr客户端的使用
    Ubuntu搭建solr搜索服务器
    Intersecting Lines(叉积,方程)
    Labyrinth(记忆化BFS)
    Segments(叉积)
    TOYS(叉积)
    Treasures and Vikings(两次搜索)
  • 原文地址:https://www.cnblogs.com/wuting/p/8718497.html
Copyright © 2011-2022 走看看