vue 流光边框矩形圆形容器

实现流光边框一般是用渐变背景加动画实现,然后使用内部盒子遮挡内部空间,达到边框流光的效果

思路:背景渐变+旋转动画

功能:

  • 自定义渐变(是否渐变<不渐变没有流光效果>,渐变颜色,渐变角度,渐变宽度)
  • 自定义动画时间

1 基础实现

<template>
  <Box> 测试 </Box>
</template>
<script setup lang="ts">
import Box from "./Box.vue";
</script>
<style scoped></style>
<template>
  <div class="box">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>
<script setup lang="ts"></script>
<style scoped lang="scss">
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 5px;
  border-radius: 10px;
  overflow: hidden;
  &:before {
    content: "";
    background-image: linear-gradient(120deg, #5ddcff, #3c67e3 40%, #4e00c2);
    position: absolute;
    z-index: 0;
    padding-left: 130%;
    padding-bottom: 130%;
    animation: rotate 8s linear infinite;
  }

  .content {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    padding: 24px 20px;
    background: #f1d674;
    z-index: 2;
    border-radius: 6px;
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

动图(略)

2 封装组件

2.1 圆形边框

使用mask属性,使得中间部分背景不被遮挡

<template>
  <div class="box" :style="{ width: width + 'px', height: height + 'px' }">
    <slot></slot>
  </div>
</template>
<script setup lang="ts">
const props = defineProps({
  width: {
    type: Number, //容器宽
    default: 100,
  },
  height: {
    type: Number, //容器高
    default: 100,
  },
  colors: {
    //颜色数组
    type: Array,
    default: () => [
      {
        color: "#64dcfd",
        width: 0,
      },
      {
        color: "#406cf1",
        width: 100,
      },
      {
        color: "#4501ac",
        width: 101,
      },
    ],
  },
  angle: {
    //渐变角度
    type: Number,
    default: 120,
  },
  borderWidth: {
    //流光边框宽度
    type: Number,
    default: 10,
  },
  gradient: {
    //是否渐变
    type: Boolean,
    default: true,
  },
  duration: {
    //动画时间
    type: String,
    default: "5s",
  },
});

const background = computed(() => {
  const positions = [];
  const colorsCopy = JSON.parse(JSON.stringify(props.colors));

  colorsCopy.forEach((s, index) => {
    const sum = colorsCopy.slice(0, index).reduce((a, b) => a + b.width, 0);
    if (!props.gradient) {
      positions.push(sum);
    }
    positions.push(sum + s.width);
  });
  return `linear-gradient(
       ${props.angle}deg, ${colorsCopy
    .map((s, index) => {
      if (!props.gradient) {
        return `${s.color} ${positions[index]}px, ${s.color} ${
          positions[2 * index + 1]
        }px`;
      }
      return `${s.color} ${positions[index]}px`;
    })
    .join(",")})`;
});

const borderLR = computed(() => {
  return props.width / 2 - props.borderWidth + "px";
});
const borderLRShink = computed(() => {
  return props.width / 2 - props.borderWidth - 1 + "px";
});
</script>
<style scoped lang="scss">
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  overflow: hidden;
  &:before {
    content: "";
    background-image: v-bind(background);
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    animation: rotate v-bind(duration) linear infinite;
    mask: radial-gradient(
      transparent,
      transparent v-bind(borderLRShink),
      #000 v-bind(borderLR)
    );
    -webkit-mask: radial-gradient(
      transparent,
      transparent v-bind(borderLRShink),
      #000 v-bind(borderLR)
    );
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

​​​​​​​

2.2 矩形边框

使用伪元素,自定义中间部分背景

<template>
  <div class="box" :style="{ width: width + 'px', height: height + 'px' }">
    <slot></slot>
  </div>
</template>
<script setup lang="ts">
const props = defineProps({
  width: {
    type: Number, //容器宽
    default: 100,
  },
  height: {
    type: Number, //容器高
    default: 100,
  },
  colors: {
    //颜色数组
    type: Array,
    default: () => [
      {
        color: "#64dcfd",
        width: 0,
      },
      {
        color: "#406cf1",
        width: 100,
      },
      {
        color: "#4501ac",
        width: 101,
      },
    ],
  },
  angle: {
    //渐变角度
    type: Number,
    default: 120,
  },
  borderWidth: {
    //左右流光边框宽度
    type: [Array, Number],
    default: [20, 5],
  },
  gradient: {
    //是否渐变
    type: Boolean,
    default: true,
  },
  duration: {
    //动画时间
    type: String,
    default: "5s",
  },
  innerBackground: {
    //内部背景
    type: String,
    default: "#FFF",
  },
});

const background = computed(() => {
  const positions = [];
  const colorsCopy = JSON.parse(JSON.stringify(props.colors));

  colorsCopy.forEach((s, index) => {
    const sum = colorsCopy.slice(0, index).reduce((a, b) => a + b.width, 0);
    if (!props.gradient) {
      positions.push(sum);
    }
    positions.push(sum + s.width);
  });
  return `linear-gradient(
       ${props.angle}deg, ${colorsCopy
    .map((s, index) => {
      if (!props.gradient) {
        return `${s.color} ${positions[index]}px, ${s.color} ${
          positions[2 * index + 1]
        }px`;
      }
      return `${s.color} ${positions[index]}px`;
    })
    .join(",")})`;
});

const innerWidth = computed(() => {
  let doubleBorderWidth = 0;
  if (Array.isArray(props.borderWidth)) {
    if (props.borderWidth.length === 2) {
      doubleBorderWidth = props.borderWidth[1] * 2;
    } else if (props.borderWidth.length === 1) {
      doubleBorderWidth = props.borderWidth[0] * 2;
    }
  } else {
    doubleBorderWidth = props.borderWidth * 2;
  }
  return props.width - doubleBorderWidth + "px";
});
const innerheight = computed(() => {
  let doubleBorderWidth = 0;
  if (Array.isArray(props.borderWidth)) {
    if (props.borderWidth.length === 2) {
      doubleBorderWidth = props.borderWidth[0] * 2;
    } else if (props.borderWidth.length === 1) {
      doubleBorderWidth = props.borderWidth[0] * 2;
    }
  } else {
    doubleBorderWidth = props.borderWidth * 2;
  }
  return props.height - doubleBorderWidth + "px";
});
const colorSize = computed(() => {
  return (
    Math.ceil(
      Math.sqrt(props.width * props.width + props.height * props.height)
    ) + "px"
  );
});
</script>
<style scoped lang="scss">
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  &:before {
    content: "";
    background-image: v-bind(background);
    position: absolute;
    width: v-bind(colorSize);
    height: v-bind(colorSize);
    animation: rotate v-bind(duration) linear infinite;
  }
  &:after {
    content: "";
    background: v-bind(innerBackground);
    position: absolute;
    z-index: 1;
    width: v-bind(innerWidth);
    height: v-bind(innerheight);
  }
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/258575.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

鉴赏 tcp vegas

优秀的 vegas 之后&#xff0c;再鉴赏一下迄今唯一像那么回事的拥塞控制算法 vegas。 从下图可看出所有的(对&#xff0c;所有的) aimd 都毫无伸缩性(z:吞吐&#xff0c;x:rtt&#xff0c;y:丢包率&#xff0c;由 buffer_size 直接决定)&#xff1a; 一下就可看出 rtt 和 bu…

LeetCode day27

LeetCode day27 —今天做到树&#xff0c;&#xff0c;&#xff0c;对不起我的数据结构老师啊~~~ 7. 整数反转 给你一个 32 位的有符号整数 x &#xff0c;返回将 x 中的数字部分反转后的结果。 如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] &#xff0c…

springboot跨域问题,解决方法

前端访问出现CORS跨域问题 不多说&#xff0c;直接上代码~ import org.springframework.stereotype.Component;import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;Component…

OpenAI 偷偷在训练 GPT-4.5!?

最近看到有人已经套路出 ChatGPT 当前的版本&#xff0c;回答居然是 gpt-4.5-turbo&#xff1a; 实际试验下&#xff0c;用 starflow.tech&#xff0c;切换到小星 4 全能版&#xff08;同等官网最新 GPT-4&#xff09;&#xff0c;复制下面这段话问它&#xff1a; What is the…

winfrom大恒工业相机SDK二次开发、C#联合halcon开发

一、开发环境 1.在大恒图像官网下载SDK安装包&#xff0c;安装SDK后&#xff0c;打开安装目录找到Samples文件夹&#xff0c;然后找到Samples\CSharp SDK\x64\DoNET\.NET4.0文件夹下找到GxIAPINET.dll&#xff0c;如图&#xff1a; 2.打开VS2019软件&#xff0c;建立winfrom项…

JS中的异常处理:

throw: 抛出异常时我们哪个关键字&#xff1f;它会终止程序&#xff1f; throw关键字 会终止程序 抛出异常经常和谁配合使用&#xff1f; Error对象配合throw使用 代码演示&#xff1a; function fn(x,y){if(!x || !y){// console.log(11);// throw 用户没有参数传递进来;th…

【BEV感知】BEVFormer 融合多视角图形的空间特征和时序特征 ECCV 2022

前言 本文分享BEV感知方案中&#xff0c;具有代表性的方法&#xff1a;BEVFormer。 它基于Deformable Attention&#xff0c;实现了一种融合多视角相机空间特征和时序特征的端到端框架&#xff0c;适用于多种自动驾驶感知任务。 主要由3个关键模块组成&#xff1a; BEV Que…

EasyExcel合并相同内容单元格及动态标题功能的实现

一、最初版本 导出的结果&#xff1a; 对应实体类代码&#xff1a; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; import com.al…

markdown文档主题颜色修改

目录 1、选择任意想选择的markdown文档主题css文件&#xff1a; 2、修改背景颜色 1、选择任意想选择的markdown文档主题css文件&#xff1a; 使用工具Typora文件主题路径&#xff1a; C:\Users\AppData\Roaming\Typora\themes&#xff0c;此处我这边就是copy了xydark的css文…

Java 自定义注解

Java 自定义注解&#xff0c; 以及interface Target Retention Around Before After ProceedingJoinPoint JoinPoint 等用法 注解应用非常广泛&#xff0c;我们自定义注解能简化开发各种各种业务 一、关键字解释 (1) 定义注解时&#xff0c;关键字 interface 来表示注解类的类…

高级算法设计与分析(二) -- 递归与分治策略

系列文章目录 高级算法设计与分析&#xff08;一&#xff09; -- 算法引论 高级算法设计与分析&#xff08;二&#xff09; -- 递归与分治策略 高级算法设计与分析&#xff08;三&#xff09; -- 动态规划 未完待续【 高级算法设计与分析&#xff08;四&#xff09; -- 贪…

谷歌新款 Pixel 8 更小、更智能!

一、谷歌 Pixel 8 更小、更智能——比去年的 Pixel 7 贵了 100 美元——不是一点点——贵 与 Pixel 7 一样&#xff0c;Pixel同样在今天 8 比正式发布的更大的 Pixel 8 Pro 兄弟产品更小、更便宜。但今年价格有所上涨&#xff0c;128GB 存储型号的 Pixel 8 起售价为 699.99 美…

腾讯云消息队列11月产品月报 | RocketMQ 5.x 国际站上线

2023年 11月动态 消息队列 RocketMQ 版 1、5.x 形态国际站上线 国际站上线 5.x 集群全系列&#xff0c;第一批先开放新加坡和硅谷地域。 控制台链接&#xff1a;https://console.tencentcloud.com/trocketmq 2、 无感迁移能力 支持用户白屏化操作&#xff0c;将自建的 Roc…

夜莺项目发布 v6.5.0 版本,暗黑菜单来了

大家好&#xff0c;夜莺项目发布 v6.5.0 版本&#xff0c;启用新 logo&#xff0c;菜单支持换肤&#xff0c;支持了暗黑版本的菜单&#xff0c;下一步会支持全站暗黑主题&#xff0c;敬请期待&#xff0c;下面是新 logo。 暗黑菜单 页面右上角点击用户名&#xff0c;在下拉框里…

盘点ASO优化过去到现在的进步

ASO优化行业十年老兵报道&#xff01;从以下几个方面总结了ASO优化的一些变化和进步&#xff0c;给大家分享。 一、优化手段&#xff1a; 过去&#xff0c;ASO优化主要依赖于机刷&#xff0c;通过破解苹果的算法&#xff0c;对苹果账号进行一系列的定向操作行为&#xff08;搜…

React Jsx转换成真实DOM过程?

面试官&#xff1a;说说React Jsx转换成真实DOM过程&#xff1f; 一、是什么 react通过将组件编写的JSX映射到屏幕&#xff0c;以及组件中的状态发生了变化之后 React会将这些「变化」更新到屏幕上 在前面文章了解中&#xff0c;JSX通过babel最终转化成React.createElement这…

双碳背景下能耗在线监测系统硬件选型详述

Acrel-5000web建筑能耗分析系统是用户端能源管理分析系统&#xff0c;在电能管理系统的基础上增加了对水、气、煤、油、热(冷)量等集中采集与分析&#xff0c;通过对用户端所有能耗进行细分和统计&#xff0c;以直观的数据和图表向管理人员或决策层展示各类能源的使用消耗情况&…

SpringBoot访问外部接口的常见方式

文章目录 SpringBoot访问外部接口模拟服务接口RestTemplatepom.xmlRestTemplateConfigClientTestRestTemplateController.java结果 WebClientpom.xmlClientTestWebClientController.java结果 HttpClientpom.xmlClientTestHttpClientController.java结果 OkHttppom.xmlClientTes…

《数据结构、算法与应用C++语言描述》- 最小赢者树模板的C++实现

赢者树 完整可编译运行代码见&#xff1a;Github::Data-Structures-Algorithms-and-Applications/_30winnerTree 比赛规则 假定有 n 个选手参加一次网球比赛。比赛规则是“突然死亡法”(sudden-death mode)&#xff1a;一名选手只要输掉一场球&#xff0c;就被淘汰。一对一对…

【Linux 驱动】Linux设备树(四)—— 设备树驱动LED

有了设备树以后&#xff0c;我们可以将寄存器信息保存到设备树&#xff0c;即便是更换了一个设备&#xff0c;我们也无需修改驱动文件&#xff0c;只需要修改设备树文件并重新编译。 下面介绍两种通过设备树驱动 LED 的最简单的方式&#xff0c;这两种方式的主要是设备树中 re…
最新文章