• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Vue+scss白天和夜间模式切换功能的实现方法

vue 搞代码 4年前 (2022-01-08) 22次浏览 已收录 0个评论

这篇文章主要介绍了Vue+scss白天和夜间模式切换功能的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文主要介绍了Vue+scss白天和夜间模式切换功能的实现方法,分享给大家,具体如下:

效果图

 

图片被压缩了不够清晰。

安装Scss

注:若安装失败可以考虑使用cnpm,或者切换npm源等方式安装。

 npm install node-sass --save-dev  //安装node-sass npm install sass-loader --save-dev //安装sass-loader npm install style-loader --save-dev //安装style-loader

新建页面DarkModelPage.vue

文件所在位置:src/DarkModelPage.vue

命名路由路径:/dark-model-page

  <div id="DarkModelPage"> </div> 

在src/assets新建目录scss

在src/assets/scss新建目录common

然后需要新建三个scss文件分别是

  • _themes.scss
  • _handle.scss
  • common.scss

_themes.scss

 $themes: ( light: ( background_color: #cccccc,//背景色 text-color: rgba(0, 0, 0, 0.65), // 主文本色 ), dark: ( background_color: #181c27,//背景 text-color: rgba(255, 255, 255, 0.65), // 主文本色 ) ); 

_handle.scss

 @import "./_themes.scss"; //遍历主题map @mixin themeify { @each $theme-name, $theme-map in $the<p style="color:transparent">本文来源gao!%daima.com搞$代*!码网1</p>mes { //!global 把局部变量强升为全局变量 $theme-map: $theme-map !global; //判断html的data-theme的属性值 #{}是sass的插值表达式 //& sass嵌套里的父容器标识  @content是混合器插槽,像vue的slot [data-theme="#{$theme-name}"] & { @content; } } } //声明一个根据Key获取颜色的function @function themed($key) { @return map-get($theme-map, $key); } //获取背景颜色 @mixin background_color($color) { @include themeify { background: themed($color)!important; } } //获取字体颜色 @mixin font_color($color) { @include themeify { color: themed($color)!important; } }

common.scss

 @import "@/assets/scss/common/_handle.scss"; /** 自定义的公共样式... **/

DarkModelPage.vue中使用

  <div id="DarkModelPage"> <div> <h1 class="title">天小天个人网</h1>模式切换</div></div> @import '@/assets/scss/common/common'; #DarkModelPage{ //在此使用了背景颜色变量 @include background_color("background_color"); //再次使用了文字颜色变量 @include font_color("text-color"); width: 100vw; height: 100vh; display: flex; justify-content:center; align-items: center; transition: background 1s , color 0.6s; .title{ margin-bottom: 20px; } .btn{ cursor: pointer; display: flex; justify-content: center; align-items: center; width: 100px; height: 40px; margin: 0 auto; } } 

注:如需更多颜色及样式切换,只需要在_themes.scss设置好变量,通过 _handle.scss设置切换函数,即可以在页面中通过该函数对指定样式进行设置。

本文演示视频: 点击浏览

到此这篇关于Vue+scss白天和夜间模式切换功能的实现方法的文章就介绍到这了,更多相关Vue 白天和夜间模式切换 内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是Vue+scss白天和夜间模式切换功能的实现方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Vue+scss白天和夜间模式切换功能的实现方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址