换肤功能的实现以及监听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 ...
随机推荐
- LibreOJ #514. 「LibreOJ β Round #2」模拟只会猜题意
内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 给定一个长度为 nnn 的序列 AAA . 定义 f(l,r)=∑i=lrAif(l,r ...
- Codeforces 464E #265 (Div. 1) E. The Classic Problem 主席树+Hash
E. The Classic Problem http://codeforces.com/problemset/problem/464/E 题意:给你一张无向带权图,求S-T的最短路,并输出路径.边权 ...
- MovieReview—Wile Hunter(荒野猎人)
Faith is Power Faith is power, this sentence is not wrong. Find your own beliefs, and strug ...
- [dp]uestc oj 邱老师看电影
定义状态dp[w][b]表示有w只白老鼠,b只黑老鼠时妹子赢的概率,分两种情况妹子抓到白老鼠概率为w/(w+b)和否则只有妹子抓黑老鼠和邱老师抓黑老鼠妹子才可能赢,再分两种情况:酱神抓白老鼠,状态 ...
- JS函数的length属性
length 是函数对象的一个属性值,指该函数有多少个必须要传入的参数,那些已定义了默认值的参数不算在内,比如function(xx = 0)的length是0.. 另外在函数内部:arguments ...
- targetcli save error
iscsi configuration unable to save python error “ValueError: 'Implict and Explict' is not in list” / ...
- python之道08
1.有如下文件,a1.txt,里面的内容为: 某某是最好的学校, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: a,将原文件全部读出来并打印. 答案 f ...
- modelformset
class StudyRecordDeialView(View): def get(self, request, class_record_id): class_record_obj = models ...
- SCOPE_IDENTITY和@@IDENTITY[转]
本文转自:http://www.cnblogs.com/daydayupanan/archive/2008/09/04/1283648.html SCOPE_IDENTITY和@@IDENTITY的作 ...
- module.exports exports 和export export default
首先可以知道的是这是两组不同模块规范. module.exports 是CommonJS模块规范,通过require 导入 a.js: var x = 'hello' module.exports.x ...