换肤功能的实现以及监听storage实现多个标签页一起换肤
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实现多个标签页一起换肤的更多相关文章
- 监听某个div或其它标签的大小改变来执行相应的处理
jquery 默认的resize只能监听到浏览器窗口大小的改变,但我们在实际使用过程中有可能还需要监听某个div或其它标签的大小改变来执行相应的处理,如果使用默认的resize就无能为力了.怎么办呢, ...
- 详解vuex结合localstorage动态监听storage的变化
这篇文章主要介绍了详解vuex结合localstorage动态监听storage的变化,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 需求:不同组件间共用同一数据,当一个 ...
- 简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)
先说一下在什么场景会使用这个事件功能. 事情大概是这样的,需求要在用户注册的时候发一些帮助邮件给用户(原本用户在注册之后已经有发别的邮件的了,短信,IM什么的) 原来这个注册的方法也就10多行代码.但 ...
- java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter
建一个服务端类ChatServer,用于设置端口接收连接 package com.swift; import java.io.IOException; import java.net.ServerSo ...
- java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)
方法一,使用匿名内部类的监听方法,因方法一致代码稍冗余 package com.swift; import java.awt.BorderLayout; import java.awt.Color; ...
- 在vue中监听storage的变化
1.首先在main.js中给Vue.protorype注册一个全局方法,其中,我们约定好了想要监听的sessionStorage的key值为’watchStorage’,然后创建一个StorageEv ...
- JS实现网页换肤功能效果
网页换肤的基本原理 使用 JS 切换对应的 CSS 样式表.例如hao123首页的右上方就有网页换肤功能.除了切换 CSS 样式表文件之外,通常的网页换肤还需要通过 Cookie 来记录用户之前更换过 ...
- Linux 动态监听进程shell
背景 前几天在研究线程的时候,看到一句话说java里的线程Thread.run都会在Linux中fork一个的轻量级进程,于是就想验证一下(笔者的机器是Linux的).当时用top命令的时候,进程总是 ...
- js基础——事件绑定(事件监听)
JavaScript事件一共有三种监听方法分别如下: 1.事件监听一夹杂在html标签内 <div id="box" onClick="alert('HELLO W ...
随机推荐
- JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用
JPA(Java Persistence API)的实现Provider有Hibernate,OpenJPA和EclipseLink等等. 本文介绍如何通过JPA + Eclipse连接SAP云平台上 ...
- Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)
因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...
- LibreOffice Writer Comment
选择文字段后输入:Ctrl+Alt+C可以插入评论,但会出现: unknown author 解决方法:Tools>Options>LibreOffice>User Data. Fi ...
- java abstraction and encapsulation
How is Abstraction different from Encapsulation? Abstraction happens at class level design. It resul ...
- Js笔记 14
<script> // <!-- 课 对象 // //对象的创建方法 // 1.var obj = {} plainobject 对象字面量 对象直接量 // 2.构造函数 ...
- iOS监听电话来电、挂断、拨号等
以下,来讲解在app内如何调用打电话功能和监听电话来电.挂断.拨号等功能. 简单的UI布局: 首先,先实现拨打电话的功能,以便于后续测试: // 拨打电话 - (IBAction)dialingBut ...
- c内置数据类型
参考 C与指针 第三章 类型 类型标识符 字节 表示数值范围 备注 整型 [signed] int 2* -32768~32767 -2^15 ~ (2^15 -1) 无符号整型 unsigned [ ...
- C语言输出多位小数
#include<stdio.h>#include<stdlib.h>int main(){int i=0;int m=19;int n=3;int s=0;s=m/n;pri ...
- 【线段树 树链剖分 差分 经典技巧】loj#3046. 「ZJOI2019」语言【未完】
还是来致敬一下那过往吧 题目分析 先丢代码 #include<bits/stdc++.h> ; ; ; struct node { int top,son,fa,tot; }a[maxn] ...
- redis学习笔记(2)
redis学习笔记第二部分 --配置文件介绍 二,解析redis的配置文件redis.conf常见配置参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通 ...