仿iPhone开关按钮switch

前端开发checkbox 单选按钮做开关


https://github.com/weatherstar/switch

在这里插入图片描述github下载

渲染事件方法

defaults = {
size : ‘default’
, checked : undefined
, onText : ‘Y’
, offText : ‘N’
, onSwitchColor : ‘#64BD63’
, offSwitchColor : ‘#fff’
, onJackColor : ‘#fff’
, offJackColor : ‘#fff’
, showText : false
, disabled : false
, onInit : function(){}
, beforeChange : function(){}
, onChange : function(){}
, beforeRemove : function(){}
, onRemove : function(){}
, beforeDestroy : function(){}
, onDestroy : function(){}
};

属性注释
在这里插入图片描述引入jQuery
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
引入
<script src="switch.js"></script>
<link href="switch.css" rel="stylesheet">

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

    <script src="switch.js"></script>
    <link href="switch.css" rel="stylesheet">
</head>
<body>
    <div>
        <table>
            <tr>
                <td><label>LED开关:</label></td>
                <td><input id="user" type="checkbox" class="demo-size-1" /></td>
            </tr>
            <tr>
                <td><label>继电器开关:</label></td>
                <td><input id="user" type="checkbox" class="demo-size-2" /></td>
            </tr>
        </table>
    </div>
</body>
</html>
<script>
    $(function () {
        var switches = {};
        var switchConfig = {
            'demo-size-1': {
                size: 'small',
                checked: false,
                //点击事件
                onChange: function (status) {
                    switchled(status);
                }
            },
            'demo-size-2': {
                size: 'small',
                checked: false,
                //点击事件
                onChange: function (status) {
                    switchtjdq(status);
                }
            }
            /*渲染方法及属性     */
            //'demo-text-1': {
            //    checked: true,
            //    disabled: true,
            //    showText: true,
            //    onText: 'O',
            //    offText: 'X'
            //},
            //'demo-color-1': {
            //    onSwitchColor: '#34B363',
            //    offSwitchColor: '#D6B3A3',
            //    onJackColor: '#1453B3',
            //    offJackColor: '#A4B363'
            //}

        };
        Object.keys(switchConfig).forEach(function (key) {
            switches[key] = new Switch(document.querySelector('.' + key), switchConfig[key]);
        });
    })
    function switchled(status) {
        console.log(status);
        
    }

    function switchtjdq(status) {
        console.log(status);
    }

</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值