codecamp

百度智能小程序 开关选择器

switch 开关选择器

解释:开关选择器

属性说明

属性名 类型 默认值 必填 说明

checked

Boolean

false

是否选中

type

String

switch

样式,有效值:switch,checkbox

color

Color

#3388ff

switch 的颜色,同 CSS 的 color

disabled

Boolean

false

是否禁用

bindchange

EventHandle

checked 改变时触发 change 事件,event.detail={ checked:true}

type 有效值

说明

switch

切换样式

checkbox

复选框样式

示例 

在开发者工具中打开


代码示例 1: 默认样式

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">默认样式</view>
        <switch class="init-switch" checked="true" bindchange="switchChange"></switch>
        <text class="switch-text">已开启</text>
        <switch class="init-switch-after" checked="false" bindchange="switchChange"></switch>
        <text class="switch-text">已关闭</text>
    </view>
</view>
Page({
    switchChange: e => {
        console.log(e.detail);
    }
});

代码示例 2: 设置 checkbox 样式

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">设置checkbox样式</view>
        <switch class="init-switch" type="checkbox" checked></switch>
        <text class="switch-text">已开启</text>
        <switch class="init-switch-after" type="checkbox"></switch>
        <text class="switch-text">已关闭</text>
    </view>
</view>

代码示例 3: 列表展示

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">列表展示</view>
        <view class="item-scroll block border-bottom">
            <text class="switch-text">已开启</text>
            <switch checked></switch>
        </view>

        <view class="item-scroll block">
            <text class="switch-text">已关闭</text>
            <switch></switch>
        </view>
    </view>
</view>

代码示例 4: 包含禁用选项

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">
            <view>包含禁用选项</view>
            <view>disabled</view>
        </view>
        <view class="item-scroll border-bottom block">
            <text class="switch-text">已开启</text>
            <switch checked color="#C3D1FF" disabled></switch>
        </view>
        <view class="item-scroll block">
            <text class="switch-text">已关闭</text>
            <switch disabled></switch>     
        </view>
    </view>
</view>

代码示例 5: 自定义颜色

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">
            <view>自定义颜色</view>
            <view>color="#00BC89"</view>
        </view>
         <view class="item-scroll border-bottom block">
            <text class="switch-text">已开启</text>
            <switch checked color="#00BC89"></switch>
        </view>
        <view class="item-scroll block">
            <text class="switch-text">已关闭</text>
            <switch color="#00BC89"></switch>     
        </view>
    </view>
</view>

Bug & Tip

  • Tip:switch 类型切换时在 IOS 自带振动反馈,可在系统设置 -声音与触感 -系统触感反馈中关闭。


百度智能小程序 滑动选择器
百度智能小程序 多行输入框
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }