weex 实现button的点击变色

效果图

先看playground 效果图

weex 之前一直没有按钮点击时间处理,因为动态绑定class不是很好,处理起来也很麻烦
不过通过weex 的伪类 active 我们能够很简单的实现出各类按钮的点击效果,不过本来还可以添加阴影,不过人家官网说了,只有ios支持,好吧,那就暂时放弃

下面简单来看一下定义的button.vue 组件

<template>
    <text :class="['btn', 'btn-' + type]"> {{value}}</text>
</template>
<script>
  const modal = weex.requireModule('modal')
  export default {
    props: {
      value: {
        type: String,
        required: true
      },
      type: {
        type: String,
        required: true
      }
    }
  }
</script>

<style scoped>
.btn{
  padding: 20px;
  font-size: 32px;
  text-align: center;
  border-style: solid;
}
.btn-nomal {
  background-color: #26A2FF;
  color: white;
}
.btn-nomal:active {
  background-color: #2071ab;
  color: white;
}    
.btn-danger {
  background-color: #EF4F4F;
  color: white;
}
.btn-danger:active {
  background-color: #ab2048;
  color: white;
} 
.btn-default {
  border-width: 3px;
  border-radius: 5px;
  border-color: lightslategray;  
  background-color: #F6F8FA;
}
.btn-default:active {
  border-width: 3px;
  border-radius: 5px;
  border-color: lightslategray;  
  background-color: #F6F6E5;
} 

.btn-nomalborder {
  border-width: 3px;
  border-radius: 5px;
  border-color: #26A2FF;
}
.btn-nomalborder:active {
  border-width: 3px;
  border-radius: 5px;
  border-color: #2071ab;
}    
.btn-dangerborder {
  border-width: 3px;
  border-radius: 5px;
  border-color: #EF4F4F;  
}
.btn-dangerborder:active {
  border-width: 3px;
  border-radius: 5px;
  border-color: #ab2048; 
} 
.btn-defaultborder {
  border-width: 3px;
  border-radius: 5px;
  border-color: lightslategray;   
}
.btn-defaultborder:active {
  background-color: #F6F6E5;
} 
</style>

然后是使用这个组件的vue文件

<template>
  <div style="margin:20px;">
    <button type="default" :value="value" @click="click()" ></button>    
    <button type="nomal" :value="value" style="margin-top:20px;"></button>
    <button type="danger" :value="value" style="margin-top:20px;"></button>  

    <div style="flex-direction: row;flex-wrap: nowrap;justify-content: space-between;margin-top:20px;">
      <button type="default" :value="value" style="flex:1;margin:10px;"></button>    
      <button type="nomal" :value="value" style="flex:1;margin:10px;"></button>
      <button type="danger" :value="value" style="flex:1;margin:10px;"></button>       
    </div>

    <button type="defaultborder" :value="value" style="margin-top:20px;"></button>    
    <button type="nomalborder" :value="value" style="margin-top:20px;"></button>
    <button type="dangerborder" :value="value" style="margin-top:20px;"></button>  

    <div style="flex-direction: row;flex-wrap: nowrap;justify-content: space-between;margin-top:20px;">
      <button type="defaultborder" :value="value" style="flex:1;margin:10px;"></button>    
      <button type="nomalborder" :value="value" style="flex:1;margin:10px;"></button>
      <button type="dangerborder" :value="value" style="flex:1;margin:10px;"></button>             
    </div>
  </div>
</template>
<style scoped>

</style>
<script>
  import button from '../components/button.vue'
  export default {
      components:{button},
      data(){
        return{
          value:'测试',
        }
      },
      methods:{
        click(){
          console.debug("clicked")
        }
      }
  }
</script>

ok,这块目前暂时用伪类实现起来相对方便

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值