首页 > 其他分享 >Vue3实现印章徽章组件

Vue3实现印章徽章组件

时间:2024-08-09 16:06:56浏览次数:13  
标签:stamp height content width 徽章 Vue3 组件 ring badge

1、组件结构

2、组件封装

src/components/StampBadge/src/StampBadge.vue 文件代码

<template>
    <div
      class="first-ring"
      v-bind="getBindValue"
      :class="getStampBadgeClass"
    >
      <div class="second-ring" :class="getStampBadgeClass">
      <div class="third-ring" :class="getStampBadgeClass">
      <div class="forth-ring" :class="getStampBadgeClass">
      <div class="content-rectangle ellipsis" :class="getStampBadgeClass">
        <span class="">{{content}}</span>
      </div>
      </div>
      </div>
      </div>
    </div>
  </template>
  
  <script lang="ts">
    import { defineComponent } from "vue";
    export default defineComponent({
      name: "StampBadge",
      inheritAttrs: false,
    });
  </script>
  
  <script lang="ts" setup>
    import { computed, onMounted, unref } from "vue";
    import { stampBadgeProps } from "./props";
    // import { useAttrs } from "/@/hooks/core/useAttrs";

    const props = defineProps(stampBadgeProps);
    // get component class
    // const attrs = useAttrs({ excludeDefaultKeys: false });
    const getStampBadgeClass = computed(() => {
      const { color, size } = props;
      return [
      {
      [`stamp-badge-${color}`]: !!color,
      [`stamp-badge-${size}`]: !!size,
      },
      ];
    });
    onMounted(() => {
      rotate()
    });
    async function rotate() {
      const { rotate ,opacity} = props;
      var element = document.getElementsByClassName('first-ring')[0]; // 获取你想要旋转的元素
      element.style.webkitTransform = 'rotate('+ rotate +'deg)'; // Chrome, Safari 和 Opera
      element.style.msTransform = 'rotate('+ rotate +'deg)'; // 旧版的IE
      element.style.transform = 'rotate('+ rotate +'deg)'; // 标准语法
      element.style.opacity = opacity;
    };
  
    // get inherit binding value
    // const getBindValue = computed(() => ({ ...unref(attrs), ...props }));
    const getBindValue = computed(() => ({  ...props }));
  </script>
  
  <style lang="less" scoped>
    .first-ring {
      display: flex;
      align-items: center;
      justify-content: center;
      
      /* 生成的图片是有一张,开启repeat自动填充 */
      pointer-events: none;
      background: repeat;
      border-radius: 100px;

      /* 核心部分,决定水印层与内容部分的结合方式 */
      mix-blend-mode: multiply;
    }
  
    .second-ring {
      display: flex;
      align-items: center;
      justify-content: center;
      background: #fff;
      border-radius: 100px;
    }
  
    .third-ring {
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 100px;
    }
  
    .forth-ring {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #fff;
      border-radius: 100px;
    }
  
    .content-rectangle {
      position: absolute;
      font-weight: bold;
      text-align: center;
      background: #fff;
    }
  
    .ellipsis {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  
    // primary
    .stamp-badge-primary.first-ring {
      background: #1890ff;
    }

    .stamp-badge-primary.third-ring {
      background: #1890ff;
    }

    .stamp-badge-primary.content-rectangle {
      color: #1890ff;
      border: 1px solid #1890ff;
    }
    // success
    .stamp-badge-success.first-ring {
      background: #52c41a;
    }

    .stamp-badge-success.third-ring {
      background: #52c41a;
    }

    .stamp-badge-success.content-rectangle {
      color: #52c41a;
      border: 1px solid #52c41a;
    }
    // error
    .stamp-badge-error.first-ring {
      background: #ff4d4f;
    }

    .stamp-badge-error.third-ring {
      background: #ff4d4f;
    }

    .stamp-badge-error.content-rectangle {
      color: #ff4d4f;
      border: 1px solid #ff4d4f;
    }
    // warning
    .stamp-badge-warning.first-ring {
      background: #faad14;
    }

    .stamp-badge-warning.third-ring {
      background: #faad14;
    }

    .stamp-badge-warning.content-rectangle {
      color: #faad14;
      border: 1px solid #faad14;
    }
    // info
    .stamp-badge-info.first-ring {
      background: #ccc;
    }

    .stamp-badge-info.third-ring {
      background: #ccc;
    }

    .stamp-badge-info.content-rectangle {
      color: #ccc;
      border: 1px solid #ccc;
    }
    // large
    .stamp-badge-large.first-ring {
      width: 84px;
      height: 84px;
    }

    .stamp-badge-large.second-ring {
      width: 80px;
      height: 80px;
    }

    .stamp-badge-large.third-ring {
      width: 74px;
      height: 74px;
    }

    .stamp-badge-large.forth-ring {
      width: 64px;
      height: 64px;
    }

    .stamp-badge-large.content-rectangle {
      width: 90px;
      font-size: 1.2rem;
    }
    // middle
    .stamp-badge-middle.first-ring {
      width: 64px;
      height: 64px;
    }

    .stamp-badge-middle.second-ring {
      width: 60px;
      height: 60px;
    }

    .stamp-badge-middle.third-ring {
      width: 56px;
      height: 56px;
    }

    .stamp-badge-middle.forth-ring {
      width: 48px;
      height: 48px;
    }

    .stamp-badge-middle.content-rectangle {
      width: 70px;
      font-size: 1rem;
    }
    // small
    .stamp-badge-small.first-ring {
      width: 54px;
      height: 54px;
    }

    .stamp-badge-small.second-ring {
      width: 50px;
      height: 50px;
    }

    .stamp-badge-small.third-ring {
      width: 46px;
      height: 46px;
    }

    .stamp-badge-small.forth-ring {
      width: 38px;
      height: 38px;
    }

    .stamp-badge-small.content-rectangle {
      width: 60px;
      font-size: 0.8rem;
    }
    // auto
    .stamp-badge-auto.first-ring {
      width: 100%;
      height: 100%;
    }

    .stamp-badge-auto.second-ring {
      width: 80%;
      height: 80%;
    }

    .stamp-badge-auto.third-ring {
      width: 80%;
      height: 80%;
    }

    .stamp-badge-auto.forth-ring {
      width: 80%;
      height: 80%;
    }

    .stamp-badge-auto.content-rectangle {
      width: 80%;
      font-size: 80%;
      border-width:0.3rem;
    }
  </style>

src/components/StampBadge/src/props.ts 文件代码

export const stampBadgeProps = {
  color: {
    type: String,
    default: "primary",
    validator: (v) =>
      ["primary", "error", "warning", "success", "info"].includes(v),
  },
  /**
   * stamp badge size.
   * @default: middle
   */
  size: {
    type: String,
    default: "middle",
    validator: (v) => ["large", "middle", "small", "auto"].includes(v),
  },
  /**
   * stamp badge rotate deg.
   * @default: 0
   */
  rotate: { type: Number, default: 0 },
  opacity: { type: Number, default: 1 },
  content: { type: String, default: "Unknown" },
};

src/components/StampBadge/index.ts 文件代码

import { withInstall } from "@/utils/StamoBadge";
import type { ExtractPropTypes } from "vue";
import stampbadge from "./src/StampBadge.vue";
import { stampBadgeProps } from "./src/props";

export const StampBadge = withInstall(stampbadge);
export declare type ButtonProps = Partial<
  ExtractPropTypes<typeof stampBadgeProps>
>;

src/utils/index.ts 文件代码

export const withInstall = <T>(component: T, alias?: string) => {
  const comp = component as any;
  comp.install = (app: App) => {
    app.component(comp.name || comp.displayName, component);
    if (alias) {
      app.config.globalProperties[alias] = component;
    }
  };
  return component as T & Plugin;
};

3、组件应用

···



标签:stamp,height,content,width,徽章,Vue3,组件,ring,badge
From: https://www.cnblogs.com/axingya/p/18350904

相关文章

  • Vue3水印组件
    1、组件封装<template><divref="watermarkContainerRef"class="watermark-container"><!--插槽--><slot></slot></div></template><scriptsetup>import{ref,onMounted,watc......
  • Vue3入门项目 简洁清晰保姆级内容讲解
    序章vue3的后台管理项目,从0到1搭建,内容非常丰富涵盖项目搭建、路由配置、用户鉴权、首页报表、用户列表、前后端联调等功能,推荐指数:5颗星!视频学习链接:vue3通用后台管理-零基础从0到1详细的入门保姆级别教程——哔哩哔哩bilibili环境配置node版本:需要Node.js版本1......
  • Vue3拖拽功能 vue-draggable-plus
    Vue拖拽功能vue-draggable-plus,支持V2和V3文章目录Vue拖拽功能vue-draggable-plus,支持V2和V3介绍VueDraggablePlus一、使用说明版本支持安装二、使用实例1.双列表拖拽2.更多拖拽效果总结介绍VueDraggablePlus最近需要pc上做拖拽功能,之前在移动端使用的是Sor......
  • OpenTiny HUICharts开源发布,带你了解一个简单、易上手的图表组件库
    摘要:目前OpenTinyHUICharts已经成功落地在华为内部100多个产品中,持续提升了用户的可视化体验。本文分享自华为云社区《OpenTinyHUICharts正式开源发布,一个简单、易上手的图表组件库》,作者:OpenTiny。引言大家好!我们非常高兴地跟大家宣布,今天正式发布我们的新产品——Open......
  • 使用 defineNuxtComponent`定义 Vue 组件
    title:使用defineNuxtComponent`定义Vue组件date:2024/8/9updated:2024/8/9author:cmdragonexcerpt:摘要:本文介绍了在Nuxt3中使用defineNuxtComponent辅助函数定义类型安全的Vue组件的方法,适用于习惯OptionsAPI的开发者。defineNuxtComponent支持asyncData获取异......
  • EasyPlayer.js在使用vue3中使用
    npminstall@easydarwin/easyplayer--save把node_modules/@easydarwin/easyplayer/dist/element目录下的文件 复制到public内index.html<scripttype="text/javascript"src="/js/EasyPlayer-element.min.js"></script>在使用的vue内直接写<templ......
  • 一个升级的多租户权限管理系统,组件化,模块化,轻耦合,高扩展企业级的应用框架,功能强大(
    前言在现代软件开发中,多租户权限管理系统是企业级应用中的一个关键组件。然而,现有的一些框架,如RuoYi,虽然提供了一些基本的功能,但在面对更复杂的企业级需求时,如原生的MyBatis使用、复杂的分页处理,以及一些高级功能支持上,仍然存在一些不足和痛点。为了解决这些问题,并提供一个更......
  • element--tree树形组件
    一、有一个default-expand-all是否默认展开所有节点的属性,只在第一次初始化tree的时候有效,改变这个属性的值好像不能控制展开折叠给出示例代码:<template><div><el-tree:data="treeData"ref="tree":default-expand-all="false"></el-tree><el-button@clic......
  • vue3(nuxt3)+Aliplayer播放器进行直播播流
    前言:    上一篇讲到使用自定义的一个播放器去进行播流进行观看直播,由于之前都是自己研发的,服务器不是特别好,所以决定使用阿里的推流以及阿里的播放器去进行拉流也更加的适配吧,至少后面出现问题可以有文档看比较完善实践    1.这里的话先把官方文档的地......
  • 一个基于 vue 的强大表单和高性能表格组件,简洁API设计,支持虚拟树,列拖拽,懒加载,快捷
    前言在现代Web应用开发中,表单和表格是两个核心组件,它们对于数据展示和用户交互至关重要。然而,现有的解-决方案往往存在一些痛点,如不够灵活、性能问题、以及难以实现复杂功能等。这些问题限制了开发者的创造力,也影响了用户体验。为了解决这些痛点,开发者需要一款功能强大、灵活......