当前位置: 首页 > article >正文

Vue3 中组件传递 + css 变量的组合

文章目录

  • 需求
  • 效果如下图所示
  • 代码逻辑
  • 代码
  • 参考


需求

开发一个箭头组件,根据父组件传递的 props 来修改 css 的颜色

效果如下图所示

在这里插入图片描述


代码逻辑

在这里插入图片描述


代码

父组件:

<Arrow color="red" />

子组件:

<template>
  <div 
    class="arrow" 
    :style="{ 
      '--arrow-color': color, 
      '--arrow-width': `${width}px`,
      '--arrow-rotation': `${rotation}deg`
    }">
  </div>
</template>

<script lang='ts' setup>
import { defineProps } from 'vue';

const props = defineProps({
  color: {
    type: String,
    default: 'black'
  },
  width: {
    type: Number,
    default: 30
  },
  rotation: {
    type: Number,
    default: 0  // 旋转角度,默认不旋转
  }
});
</script>

<style scoped>
.arrow {
  display: inline-block;
  position: relative;
  margin: 10px;
  width: var(--arrow-width);
  transform: rotate(var(--arrow-rotation));  /* 添加旋转样式 */
}

.arrow::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: var(--arrow-width);
  border-top: 2px dotted var(--arrow-color);
  transform: translateY(-50%);
}

.arrow::after {
  content: '';
  position: absolute;
  top: 50%;
  left: calc(var(--arrow-width) - 8px);
  width: 0;
  height: 0;
  border-left: 10px solid var(--arrow-color);
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  transform: translateY(-50%);
}
</style>

参考

1. 使用 CSS 自定义属性(变量) https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties


http://www.kler.cn/news/314911.html

相关文章:

  • 深度学习03-神经网络01-什么是神经网络?
  • QT快速安装使用指南
  • OpenHarmony(鸿蒙南向开发)——小型系统芯片移植指南(二)
  • 安全热点问题
  • NCNN 源码(1)-模型加载-数据预处理-模型推理
  • MySQL深入原理
  • 【数学分析笔记】第3章第3节无穷小量与无穷大量的阶(2)
  • 国标GB28181视频融合监控汇聚平台的方案实现及场景应用
  • 机器学习和深度学习的区别:从基础到前沿
  • 35. 模型材质和几何体属性
  • Mapper核心配置文件
  • uniapp 整合 OpenLayer3
  • C++速通LeetCode中等第4题-三数之和
  • 本地快速部署一个简洁美观的个人Halo博客网站并发布公网远程访问
  • 20240918软考架构-------软考171-175答案解析
  • 数字IC设计\FPGA 职位经典笔试面试整理--语法篇 Verilog System Verilog(部分)
  • Docker修改默认的存储路径
  • 分布式锁之 防误删(优化之UUID防误删)
  • go-orm接口原生到框架
  • 小明,谈谈你对Vue nextTick的理解
  • 面试题 02.07. 链表相交 双指针
  • Unity URP APK打包物体不渲染问题
  • Leetcode42. 接雨水
  • C#(.NET FrameWork库)逆向基础流程(纯小白教程)
  • ETCD学习使用
  • VUE面试题(单页应用及其首屏加载速度慢的问题)
  • Mac 搭建仓颉语言开发环境(Cangjie SDK)
  • 蓝桥杯【物联网】零基础到国奖之路:九. I2C
  • 开源即时通讯IM框架MobileIMSDK的H5端技术概览
  • Elasticsearch 下载安装及使用总结