HMTL实现类移动端的滑动开关——前端掉坑记

为了把开关做得更加美观,不用之前一直用的简单的打勾的checkbox,而是要做成像手机选项开关那种样式。


效果图如下:



首先,你可能搜不到我这篇文章,因为,你不知道这个叫滑动开关。我拿着这东西去问前端,他一开始也说不出。他搜的是仿IOS开关。后面进一步搜索我才知道往滑动开关这个关键词去搜。


不用图片可以纯CSS生成,https://proto.io/freebies/onoff/ 这个网站的实现。叫FlipSwitch。


一、踩坑一——Label的for属性

实现三个滑动开关按钮,Label中有个for属性,作用是当你点击label的时候,会被当作是在点击其关联的input元素。
除了用for属性,还可以将input放在label里面。后面的问题还跟此属性有关。当你使用的是 checkbox和button的时候非常有用,你就不需要点击这些本身而只需要点击关联的文字。

所以,整个滑动的原理就是点击的是css样式控制好的漂亮的滑动按钮,事实上所控制的是属性为checkbox的input元素。

参考:http://stackoverflow.com/questions/18432376/what-does-for-attribute-do-in-html-label-tag


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<style type="text/css">
.onoffswitch {
    position: relative; width: 70px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 23px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 22px; padding: 0; line-height: 22px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "关";
    padding-left: 10px;
    background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "开";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 16px; margin: 3px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 44px;
    border: 2px solid #999999; border-radius: 23px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
</style>
</head>
<body>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked>
    <label class="onoffswitch-label" for="myonoffswitch1">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" checked>
    <label class="onoffswitch-label" for="myonoffswitch2">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

</body>
</html>


二、踩坑二——jQuery使用attr获取checked是undefined

alert($("#myonoffswitch").attr("checked"));
alert($("#myonoffswitch").prop("checked"));
alert($("#myonoffswitch").get(0).checked);
alert(document.getElementById("myonoffswitch").checked);

attr是拿到的是checked或者undefined,而后面两个拿到的是true或者false。


现在的学习资源虽多,教程多,但是可以将底层简单地讲一下还有理论的东西也是。所以自己再去看了下《锋利的jQuery》。

找到自己想要的东西,再去理清了一下,DOM对象和jQuery对象。

getElementsByTagName和getElementById 获取节点,这个DOM元素就是DOM对象。
jQuery对象是包装DOM对象后产生的对象,才能用jQuery方法,但不能使用DOM对象的方法,所以命名作区分,$var和var进行区分。
jQuery对象转DOM对象,前者为数组对象,[index] 或 get(index)
DOM对象转jQuery对象, $(dom)。

而上面那个问题,不想了解的话可以直接用prop,想了解的话可以看看

因为这问题不仅仅停留在解决方法上了。


三、踩坑三和四——为多个元素绑定事件和点击一次变两次的问题。

我犯了个很蠢的错误

for(var i=0;i<jsonObject.length;i++){
  拼接HMTL
}

根据后台对象再到前端解析成HTML代码,这个是没问题的。

很容易生成:

<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    <label class="onoffswitch-label" for="myonoffswitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked>
    <label class="onoffswitch-label" for="myonoffswitch1">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" checked>
    <label class="onoffswitch-label" for="myonoffswitch2">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>


但是我对这些元素绑定事件的时候这样就行不通了。

for(var i=0;i<jsonObject.length;i++){
  $("#element"+i).attr("disabled",false);
}

这样循环完,i直接变成jsonObject.length的大小,跟我最初想要的从0到 jsonObject.length的所有元素事件进行监听的是完全不同的。这算是个思路问题。


要换种思路:

既然三个div,class都是onoffswitch,可以监听整个。

$(".onoffswitch").on('click',function(e){
    var myindex = $(".onoffswitch").index($(this));
    $("#element"+myindex).attr("disabled",true);
})

还记得第一坑吗? Label的for属性,如果是onoffswitch属性的话,你会发现点击一次,这个事件发生两次。

原因就在整个滑动的原理就是点击的是css样式控制好的漂亮的滑动按钮,事实上所控制的是属性为checkbox的input元素。所以点击一次。是监听到了两次,所以直接监听onoffswitch-label就没有这个问题了。

  $(".onoffswitch-label").on('click',function(e){
          var self = $(".onoffswitch-label").index($(this));
          $("#Weight"+self).attr("disabled",!document.getElementById("element"+self).checked);
  })

这样只有一个点击了。


还有一种方法是使用each:

          $(".onoffswitch-label").each(function(i){
            $(this).on('click',function(){
              $("#element"+i).attr("disabled",true);
            })
          });


四、踩坑五——前端调试

这个不算坑,这个是在其他同事那里学来的。

不要使用alert,使用console.info(var);


坑虽踩多,总结还是需要的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iaiti

赏顿早餐钱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值