1:需求:项目的侧边栏实现换肤功能,核心代码:

updateSkin (val) {
const existSkinLink = document.head.querySelector('link[id="sidebar"]')
if (existSkinLink) {
existSkinLink.setAttribute('href', `//static.zhaopin.com/newcrm/static/skin/${val}02.css`)
} else {
const skinLink = document.createElement('link')
skinLink.setAttribute('id', 'sidebar')
skinLink.setAttribute('href', `//static.zhaopin.com/newcrm/static/skin/${val}02.css`)
skinLink.setAttribute('rel', `stylesheet`)
document.head.appendChild(skinLink)
}
}

注意 此处必须引用css 不能是styl sass less 等

2:需求:打个多个标签页后,A标签换肤后,B标签不会自动换肤,要刷新才行

实现思路:监听storage

核心代码:

1:在mounted钩子里增加监听,在destroyed钩子里销毁

mounted () {
window.addEventListener('storage', this.toUpdateSkin)
},
destroyed () {
window.removeEventListener('scroll', this.scrollAction)
window.removeEventListener("storage", this.toUpdateSkin)
},

2:有A、B两个标签页,在A标签页点击切换皮肤的按钮后,走的是方法1changeSkin,在changeSkin里做了些业务操作,可忽略,关键代码localStorage.setItem(${env}${empId}cmpSkin, colorBg) 修改localStorage,然后调用方法3 this.updateSkin(colorBg)实现A标签页换肤

在B标签页监听到localStorage改变之后,自动执行方法2toUpdateSkin经过一些业务代码后调用方法3this.updateSkin(colorBg)实现B标签页换肤

方法1
async changeSkin (colorBg) {
if (this.menuSkinColor === colorBg) {
return
}
const key = 'colorBg'
const url = this.api.myCustomer.saveSettings
const type = 1
const params = {
column: 'emp_query_json',
type,
json: {
[key]: colorBg,
}
}
try {
this.$http.post(url, params)
const empId = Common.getCookie('ZPSSO_USER_EMPID')
const env = process.env.NODE_ENV
localStorage.setItem(`${env}${empId}cmpSkin`, colorBg)
this.setLeftLoading(true)
this.menuSkinColor = colorBg
await this.getLeftMenuData()
this.$nextTick(() => {
setTimeout(() => {
this.setLeftLoading(false)
this.updateSkin(colorBg)
}, 0)
})
} catch (e) {
this.common.handleError(e, this)
}
},
方法2
async toUpdateSkin (e) {
const empId = Common.getCookie('ZPSSO_USER_EMPID')
const env = process.env.NODE_ENV
if (e && e.key === `${env}${empId}cmpSkin`) {
const arr = ['blackBg', 'grayBg']
let color = ''
if (arr.includes(e.newValue) && arr.includes(e.oldValue) && e.oldValue !== e.newValue) {
color = e.newValue
} else {
color = 'blackBg'
}
this.setLeftLoading(true)
this.menuSkinColor = color
await this.getLeftMenuData()
this.$nextTick(() => {
setTimeout(() => {
this.setLeftLoading(false)
this.updateSkin(color)
}, 0)
})
}
},
方法3
updateSkin (val) {
const existSkinLink = document.head.querySelector('link[id="sidebar"]')
if (existSkinLink) {
existSkinLink.setAttribute('href', `//static.zhaopin.com/newcrm/static/skin/${val}02.css`)
} else {
const skinLink = document.createElement('link')
skinLink.setAttribute('id', 'sidebar')
skinLink.setAttribute('href', `//static.zhaopin.com/newcrm/static/skin/${val}02.css`)
skinLink.setAttribute('rel', `stylesheet`)
document.head.appendChild(skinLink)
}
},

换肤功能的实现以及监听storage实现多个标签页一起换肤的更多相关文章

  1. 监听某个div或其它标签的大小改变来执行相应的处理

    jquery 默认的resize只能监听到浏览器窗口大小的改变,但我们在实际使用过程中有可能还需要监听某个div或其它标签的大小改变来执行相应的处理,如果使用默认的resize就无能为力了.怎么办呢, ...

  2. 详解vuex结合localstorage动态监听storage的变化

    这篇文章主要介绍了详解vuex结合localstorage动态监听storage的变化,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 需求:不同组件间共用同一数据,当一个 ...

  3. 简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)

    先说一下在什么场景会使用这个事件功能. 事情大概是这样的,需求要在用户注册的时候发一些帮助邮件给用户(原本用户在注册之后已经有发别的邮件的了,短信,IM什么的) 原来这个注册的方法也就10多行代码.但 ...

  4. java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter

    建一个服务端类ChatServer,用于设置端口接收连接 package com.swift; import java.io.IOException; import java.net.ServerSo ...

  5. java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)

    方法一,使用匿名内部类的监听方法,因方法一致代码稍冗余 package com.swift; import java.awt.BorderLayout; import java.awt.Color; ...

  6. 在vue中监听storage的变化

    1.首先在main.js中给Vue.protorype注册一个全局方法,其中,我们约定好了想要监听的sessionStorage的key值为’watchStorage’,然后创建一个StorageEv ...

  7. JS实现网页换肤功能效果

    网页换肤的基本原理 使用 JS 切换对应的 CSS 样式表.例如hao123首页的右上方就有网页换肤功能.除了切换 CSS 样式表文件之外,通常的网页换肤还需要通过 Cookie 来记录用户之前更换过 ...

  8. Linux 动态监听进程shell

    背景 前几天在研究线程的时候,看到一句话说java里的线程Thread.run都会在Linux中fork一个的轻量级进程,于是就想验证一下(笔者的机器是Linux的).当时用top命令的时候,进程总是 ...

  9. js基础——事件绑定(事件监听)

    JavaScript事件一共有三种监听方法分别如下: 1.事件监听一夹杂在html标签内 <div id="box" onClick="alert('HELLO W ...

随机推荐

  1. 【TensorFlow入门完全指南】模型篇·线性回归模型

    首先呢,进行import,对于日常写代码来说,第二行经常写成:import numpy as np,这样会更加简洁.第三行import用于绘图. 定义了学习率.迭代数epoch,以及展示的学习步骤,三 ...

  2. 为DataGridView控件实现复选功能

    实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...

  3. How to restrict root user to access or modify a file and directory in Linux

    Now in this article I will show you steps to prevent or restrict access of root user to access certa ...

  4. java中IO流之字节字符流的总结概述

    概念        这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都 ...

  5. 《剑指offer》56 数组中只出现一次的数字

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.   在线练习:https://www.nowcoder.com/practice/e02fdb5 ...

  6. iOS中的数据存储方式_Plist

    plist文件只能存储OC常用数据类型(NSString.NSDictionary.NSArray.NSData.NSNumber等类型)而不能直接存储自定义模型对象; 我们拿NSData举例: /* ...

  7. 51nod——1548 欧姆诺姆和糖果

    一开始以为是贪心,然后发现没法贪.暴力枚举肯定T,于是用约束关系优化: 假设wr >= wb, 第一种情况:wr >= sqrt (c), 则此时最多吃c / wr个r,且c / wr & ...

  8. Golang tcp转发 remoteAddr错误

    Golang tcp 转发 第一版本 accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址 // tcpForward package main import ...

  9. vue-cli webpack配置cdn路径 以及 上线之后的字体文件跨域处理

    昨天搞了一下vue项目打包之后静态资源走阿里云cdn. 配置了半天,终于找到了设置的地方 config/index.js 里面设置build 下的 assetsPublicPath 打包的时候便可以添 ...

  10. jquery.imgpreload.min.js插件实现页面图片预加载

    页面分享地址: http://wenku.baidu.com/link?url=_-G8miwbgDmEj6miyFtjit1duJggBCJmFjR2jky_G1VftD9eS9kwGOlFWAOR ...