uniapp Switch控件背景颜色自定义

在uniapp中引入 colorui.css 样式文件后,导致switch控件默认样式失效,经过摸索验证,终于找到原因。

目录

1、案例剖析

[1.1、App.vue 文件](#1.1、App.vue 文件)

[1.2、index.scss 文件](#1.2、index.scss 文件)

[1.3、colorui.css 文件](#1.3、colorui.css 文件)

[1.4、system-setting.vue 文件](#1.4、system-setting.vue 文件)

2、实践总结


运行环境:

  • Windows-10-x64
  • HBuilderX-4.66

以下是 uniapp 项目中,使用switch控件相关的代码片段,供参考......

1、案例剖析

1.1、App.vue 文件

在项目根目录下的 App.vue文件中,导入公共样式文件 index.scss。

css 复制代码
<style lang="scss">
    // 公共样式
    @import '@/static/scss/index.scss';
</style>

1.2、index.scss 文件

在 index.scss文件中导入 colorui.css

css 复制代码
// global

// color-ui
@import '@/static/scss/colorui.css';

// iconfont

1.3、colorui.css 文件

部分样式摘录,重点关注 switch控件、switch 样式 blue、red 等的定义。

1.4、system-setting.vue 文件

本例文件完整代码如下

javascript 复制代码
<template>
    <view class="popup-container" @tap.stop.prevent>
        <view class="control-container">
            <view class="header">
                <text class="title">设置</text>
                <text class="close-btn" @click="closeView">×</text>
            </view>
            <view class="control-item">
                <text class="control-label">震动1</text>
                <switch type="switch" :class="isVibrating ? 'blue checked' : 'blue'" color="#007AFF" :checked="isVibrating" @change="toggleVibration" />
            </view>
            <view class="control-item">
                <text class="control-label">震动2</text>
                <switch type="switch" :class="isVibrating ? 'green checked' : 'green'" color="#007AFF" :checked="isVibrating" @change="toggleVibration" />
            </view>
            <view class="control-item">
                <text class="control-label">震动3</text>
                <switch type="switch" :class="isVibrating ? 'red checked' : 'red'" color="#007AFF" :checked="isVibrating" @change="toggleVibration" />
            </view>
            <view class="control-item">
                <text class="control-label">性别</text>
                <switch type="switch" :class="isVibrating ? 'switch-sex checked' : 'switch-sex'" color="#007AFF" :checked="isVibrating" @change="toggleVibration" />
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                isVibrating: true, // 震动马达开启/关闭(默认:true)
            }
        },
        methods: {
            /** @description 关闭窗口 */
            closeView() {
                // TODO
            },
            /**
             * @description 震动马达开启/关闭事件
             * @param {Object} e 事件对象
             */
            toggleVibration(e) {
                this.isVibrating = e.detail.value; // 当前Switch的值(true:开;false:关)
                if (this.isVibrating) {
                    uni.vibrateShort(); // 短震动测试
                }
            },
        }
    }
</script>

<style lang="scss" scoped>
    // 窗口容器
    .popup-container {
        background-color: rgba(0, 0, 0, 0.5);
        width: 100%;
        height: 100%;
        position: fixed;
        z-index: 999999;
    }

    // 窗口容器
    .control-container {
        background-color: #f8f8f8;
        border-radius: 20rpx;
        padding: 20rpx;
        margin: 20rpx;
        position: fixed;
        z-index: 9999999;
        top: calc(50% - 20rpx);
        left: calc(50% - 20rpx);
        transform: translate(-50%, -50%);
        width: 100%;
        max-width: 600rpx;
    }

    // 窗口标题栏
    .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 0 20rpx 0;
        border-bottom: 1rpx solid #eee;
        margin-bottom: 20rpx;
    }

    // 窗口标题栏>>窗口标题
    .title {
        font-size: 36rpx;
        font-weight: bold;
        color: #333;
        text-align: center;
        width: calc(100% - 40rpx);
    }

    // 窗口正文>>项目
    .control-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20rpx 0;
        border-bottom: 1rpx solid #eee;
    }

    // 窗口正文>>项目>>最后一子项目底部边框
    .control-item:last-child {
        border-bottom: none;
    }

    // 窗口正文>>项目>>子项目标题
    .control-label {
        font-size: 32rpx;
        color: #333;
    }
</style>

2、实践总结

在项目中引入 colorui.css 样式文件,会导致原 uniapp的样式失效,表现有:

  • 导致原控件的 uniapp的样式失效;
  • 会忽略switch 控件的 color属性设置;

附录:

相关推荐
Dxy123931021612 小时前
CSS常用样式详解:从基础到进阶的全面指南
前端·css
爱敲代码的菜菜13 小时前
【测试】自动化测试
css·selenium·测试工具·junit·自动化·xpath
酉鬼女又兒17 小时前
入门前端CSS 媒体查询全解析:从入门到精通,打造完美响应式布局(可用于备赛蓝桥杯Web应用开发)
前端·css·职场和发展·蓝桥杯·前端框架·html5·媒体
结网的兔子19 小时前
前端学习笔记——Element Plus 栅格布局系统示例
前端·javascript·css
Predestination王瀞潞20 小时前
5.4.1 通信->WWW万维网内容访问标准(W3C):WWW(World Wide Web)基本信息&核心设计目标&现实意义
css·网络·网络协议·html·url·www
木斯佳1 天前
前端八股文面经大全:阿里云AI应用开发二面(2026-03-21)·面经深度解析
前端·css·人工智能·阿里云·ai·面试·vue
你的眼睛會笑1 天前
uni-app 实战:使用 lime-painter 实现页面内容一键生成海报并下载
uni-app
spencer_tseng1 天前
secure-keyboard.js secure-keyboard.css
javascript·css
一只程序熊1 天前
uniapp 高德地图 打开选择地址报错,也没有展示出附近的位置
android·uni-app
小J听不清1 天前
CSS 外边距(margin)全解析:取值规则 + 实战用法
前端·javascript·css·html·css3