html主题切换,vue 切换主题(换肤)功能

一:先写好两个css样式放在static文件夹中

94e1042a5809d90a7900ef038f129ff5.png

二:在index.html中添加css link链接

3d894e2f9308a7e108a9cfe642238f8f.png

三:在App.vue中的 created中添加默认执行方法

created() {

var style = localStorage.getItem("style");

if(style){

document.getElementById('style').setAttribute("href",style); //实现将主题保存在内存中刷新浏览器不改变

}

}

9c8c0b56726327170824ff590e67aac2.png

四:最后在需要的页面写上切换主题功能

主题切换

切换到紫色主题

切换到蓝色主题

export default {

name: "ThemeBody",

data(){

return{

body_now:null

}

},

methods:{

change:function (type) {

if(type == 1){

document.getElementById('style').setAttribute("href","../static/css/one.css");

localStorage.setItem("style","../static/css/one.css");

}else if(type == 2){

document.getElementById('style').setAttribute("href","../static/css/two.css");

localStorage.setItem("style","../static/css/two.css");

}

}

}

}

db1ce35451efa082f47b11a4762e9322.png

完成效果

6b5cb8a49872bce3b58973706f50a8b0.png

9610cd45193b1b02302723c83b9059bf.png