vue实现移动端的开关按钮

2022-10-14,,,

这篇文章主要为大家详细介绍了vue实现移动端开关按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现移动端的开关按钮的具体代码,供大家参考,具体内容如下

逻辑:

1.写一个椭圆形的div

2.动态改变这个椭圆形的div的背景颜色

3.写一个圆点,这个圆点采用绝对定位的方式,定位在椭圆形的div上

4.开关来回切换的时候,要使用translateX移动圆点的位置,并且动态改变椭圆形 div的背景颜色

代码:

html:

<!--部门功能-->
        <div class="department">
            <div class="department-l">部门功能</div>
            <div class="department-r">
                {{isShow?'开启':'关闭'}}
                <span class="switch" :class="{on:isShow}" @click.stop="switchDepartment">
                    <div class="switch-circle" :class={right:isShow}></div>
                </span>
            </div>
</div>

css:

 .department {
    height: px2rem(178);
    background: #ffffff;
    padding: 0 px2rem(66) 0;
    margin-top: px2rem(4);
    display: flex;
    justify-content: space-between;
    .department-l {
      line-height: px2rem(178);
      font-size: px2rem(53);
      ccolor: #303030;
    }
    .department-r {
      line-height: px2rem(178);
      font-size: px2rem(50);
      color: #454545;
    }
  }
  .switch{
    display: inline-block;
    width: px2rem(140);
    height: px2rem(86);
    background: #DBDBDB;
    border-radius: px2rem(331);
    position: relative;
    vertical-align: middle;
    margin-left: px2rem(31);
    .switch-circle{
      position: absolute;
      left: px2rem(6);
      top: px2rem(6);
      width: px2rem(73);
      height: px2rem(73);
      border-radius: 50%;
      background: #fff;
    }
  }
  .on{
    background: -webkit-linear-gradient(left, #19A89F, #9CDD97); /* Safari 5.1 - 6.0 */
    background: -o-linear-gradient(right, #19A89F, #9CDD97 ); /* Opera 11.1 - 12.0 */
    background: -moz-linear-gradient(right, #19A89F , #9CDD97); /* Firefox 3.6 - 15 */
    background: linear-gradient(to right, #19A89F, #9CDD97); /* 标准的语法(必须放在最后) */
  }
  .right{
    transform :translateX(px2rem(55))
  }

js:

<script>
    export default {
        name: "clientCreate",
        data() {
            return {
    
                isShow:false
            }
        },
        created: function () {
 
        },
        mounted: function () {
            
        },
        methods: {
 
            switchDepartment:function(){
                this.isShow=!this.isShow;
            },
            
        }
    }
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • vue实现可移动的悬浮按钮
  • Vue实现按钮旋转和移动位置的实例代码
  • vue项目实现按钮可随意移动

《vue实现移动端的开关按钮.doc》

下载本文的Word格式文档,以方便收藏与打印。