[VUE3] 阴影字体+渐变字体

4 篇文章 0 订阅
3 篇文章 0 订阅

key: 阴影 渐变 字体

  • 知识点 : 使用useSlots判断是否有传入默认插槽

// if(useSlots().default) 可以判断插槽是否有内容

// <BFont>渐变阴影文字<BFont>

$slots.default()[0].children 可以得到传入的文字

  • 知识点:  vue3 中 v-bind() 可以对 computed和ref使用 但不能对props使用 但可以转化

const props = defineProps({   color: {     type: String,     require: true,     default: 'linear-gradient(0deg, #9ccdff 0%, #ffffff 100%)',   },

}

const color = computed(()=> props.color)

// style

.bright-font {

  background: v-bind(color)

}

  • 知识点: less函数也可以使用vue3 style v-bind()的传值

<style lang="less" scoped> .bright-font {   .gradientShadow(v-bind(backgroundGradient);v-bind(textShadow)); } </style>

fn.less


// 字体既要渐变又要阴影
.gradientShadow(@backgroundGradient; @textShadow) {
  color: transparent;
  display: block;
  position: relative;
  text-shadow: @textShadow;
  &::after {
    content: attr(data-content);
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    background: @backgroundGradient;
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: none;
    // white-space: nowrap;
  }
}

完整代码

<template>
  <span class="bright-font" :data-content="label">
    <slot>渐变阴影字体</slot>
  </span>
</template>

<script setup>
import { defineProps, computed, useSlots } from 'vue';

const props = defineProps({
  color: {
    type: String,
    require: true,
    default: 'linear-gradient(0deg, #9ccdff 0%, #ffffff 100%)',
  },
  backgroundGradient: {
    type: String,
    require: true,
    default: 'linear-gradient(0deg, #9ccdff 0%, #ffffff 100%)',
  },
  shadow: {
    type: String,
    require: true,
    default: '2px 2px 1px rgba(20, 39, 55, 0.8), 1px 1px 4px rgba(130, 224, 255, 0.7)',
  },
  textShadow: {
    type: String,
    require: true,
    default: '2px 2px 1px rgba(20, 39, 55, 0.8), 1px 1px 4px rgba(130, 224, 255, 0.7)',
  },
});
const $slots = useSlots();

const backgroundGradient = computed(() => props.color || props.backgroundGradient);
const textShadow = computed(() => props.shadow || props.textShadow);
const label = computed(() => ($slots.default ? $slots.default()[0].children : '渐变阴影字体'));
</script>

<style lang="less" scoped>
.bright-font {
  .gradientShadow(v-bind(backgroundGradient);v-bind(textShadow));
}
</style>

使用

<template>
<BFont shadow="2px 2px 1px rgba(20, 39, 55, 0.8), 1px 1px 4px rgba(130, 224, 255, 0.7)" color="linear-gradient(0deg, #9ccdff 0%, #ffffff 100%)">需要渐变并带阴影文字</BFont>

</template>

<script setup>
import BFont from 'BFont';

<script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值