vue 组件封装 按钮button 支持点击按钮带阴影效果和Loading加载效果

18 篇文章 0 订阅
2 篇文章 0 订阅

一,子组件shadowButton

<template>
    <!-- 此组件支持自定义按钮文字和自定义按钮图片 -->
    <button :style="{backgroundColor:btnBgc,color:btnColor,width:width,height:height,fontSize:fontSize,boxShadow:boxShadow}" @click="handleClick" class="rytBtn">
        <label class="btnText"><slot></slot></label>
    </button>
</template>

<script>
     export default {
           name:'shadowButton',
           props:{
               btnBgc:{
                   type:String,
                   default:'pink'
               },
               btnColor:{
                   type:String,
                   default:'#fff'
               },
               width:{
                   type:String,
                   default:'200px'
               },
               height:{
                   type:String,
                   default:'44px'
               },
               fontSize:{
                   type:String,
                   default:'16px'
               },
               boxShadow:{
                   type:String,
                   default:'0px 2px 4px 0px rgba(255,130,0,0.7)'
               }
           },
          data(){
              return{

              }

          },
          methods:{
              handleClick(){

                  this.$emit('clickBtn')

              }
          }

     }
</script>
<style scoped>
  .rytBtn{
      position: relative;
      border-radius: 22px;
  }
  /*思路:添加伪元素,设置伪元素的透明度opacity伪0,当点击的时候利用伪类:active 改变透明度伪
  0.3,从而实现透明度的添加,效果类似mint-ui中的按钮效果*/
  .rytBtn::after { 
       background-color: #000;
       border-radius: 22px;
    content: " ";
    opacity: 0;
    position:absolute;
    top:0px;
    bottom:0px;
    left:0px;
    right:0px;
  }
  .rytBtn:active::after{ 
       opacity: 0.3;
  }
 .rytBtn .btnText{
      font-size: 18px;
  }


</style>

二,父组件

<template>
  <div class="about marquee">
  
   <!--  按钮点击带阴影 -->
   <!-- <button>点击按钮带阴影效果shadowButton支持点击带loading图片</button> -->

   <shadow-button @clickBtn="clickShadowBtn" btnBgc="#FF8200">按钮<img src="../../assets/CombinedShape.png" style="width:20px;height:20px;" v-if="showLoading"></shadow-button>

  </div>
</template>

<script>
import shadowButton from '../../components/button/shadowButton.vue';  //下拉框带popup蒙层
export default {
   name:'ceButton',
   components: { //注册组件
    shadowButton,
  },
 
  data() {
    return {
     showLoading:false,
    };
  },
  methods: {
     // 按钮封装点击
     clickShadowBtn(){  //点击按钮执行逻辑
       this.showLoading = true;
       // 请求接口提交逻辑
     }
  
  },


 }
</script>


<style scoped>

</style>

三,所用知识点:slot 插槽  伪类:active  伪元素::after

四,点击效果如下

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个具有多种功能的简单Vue按钮组件示例: ```html <template> <button class="btn" :class="[type, size, { disabled: isDisabled }]" :disabled="isDisabled" @click="handleClick"> <slot></slot> <div v-if="isLoading" class="spinner"></div> </button> </template> <script> export default { name: 'Button', props: { type: { type: String, default: 'default', validator: (value) => ['default', 'primary', 'success', 'warning', 'danger', 'link'].includes(value), }, size: { type: String, default: 'medium', validator: (value) => ['small', 'medium', 'large'].includes(value), }, disabled: { type: Boolean, default: false, }, loading: { type: Boolean, default: false, }, }, computed: { isDisabled() { return this.disabled || this.isLoading; }, }, methods: { handleClick() { this.$emit('click'); }, }, }; </script> <style> .btn { display: inline-block; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; font-weight: 500; line-height: 1.5; padding: 10px 20px; text-align: center; text-decoration: none; transition: background-color 0.3s ease; } .btn.default { color: #333; background-color: #fff; border: 1px solid #ccc; } .btn.default:hover { background-color: #f5f5f5; } .btn.primary { color: #fff; background-color: #007bff; } .btn.primary:hover { background-color: #0069d9; } .btn.success { color: #fff; background-color: #28a745; } .btn.success:hover { background-color: #218838; } .btn.warning { color: #fff; background-color: #ffc107; } .btn.warning:hover { background-color: #e0a800; } .btn.danger { color: #fff; background-color: #dc3545; } .btn.danger:hover { background-color: #c82333; } .btn.link { color: #007bff; background-color: transparent; border: none; } .btn.link:hover { background-color: #f5f5f5; } .btn.small { font-size: 12px; padding: 8px 16px; } .btn.medium { font-size: 14px; padding: 10px 20px; } .btn.large { font-size: 16px; padding: 12px 24px; } .btn.disabled, .btn:disabled { opacity: 0.5; cursor: not-allowed; } .spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid #fff; border-top-color: #007bff; border-radius: 50%; animation: spinner 0.75s ease-in-out infinite; margin-left: 8px; } @keyframes spinner { to { transform: rotate(360deg); } } </style> ``` 这个按钮组件具有以下功能: - 可以设置按钮的类型,包括:默认(default)、主要(primary)、成功(success)、警告(warning)、危险(danger)和链接(link); - 可以设置按钮的大小,包括:小(small)、中(medium)和大(large); - 可以设置按钮是否禁用; - 可以设置按钮是否处于加载状态,此时将显示一个旋转动画; - 可以在按钮组件中插入任何内容,包括文字、图标等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值