vue 斑马线进度条,渐变进度条

15 篇文章 0 订阅
13 篇文章 0 订阅

功能:

  • 自定义渐变(是否渐变,渐变颜色)
  • 自定义斑马线(角度,宽度)
  • 自定义百分比位置以及前缀文字、后缀文字
  • 自定义动画时间

1 进度条

<template>
  <div class="wrapper" :style="{ width: width, height: height }">
    <div class="progress">
      <div
        class="bg"
        :style="{
          width: curProgress,
          background: percent >= 100 ? '#00EE00' : background,
          backgroundSize: backgroundSize,
        }"
      ></div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { computed, onMounted, onBeforeUnmount } from "vue";

const props = defineProps({
  width: {
    type: Number, // 进度条总宽度
    default: 300,
  },
  height: {
    type: Number, // 进度条高度
    default: 10,
  },
  percent: {
    type: Number, // 当前进度
    default: 50,
  },
  stripeAngle: {
    type: Number, // 进度条线的角度  和-x的夹角,顺时针
    default: 45,
  },
  stripe: {
    type: Array, // 进度条斑马线数组
    default: () => [
      {
        color: "#FF4500",
        width: 15,
      },
      {
        color: "#00EE00",
        width: 15,
      },
      {
        color: "#00FFFF",
        width: 15,
      },
    ],
  },
  gradient: {
    type: Boolean, // 渐变
    default: false,
  },
  duration: {
    type: Number, //动画时间
    default: 2,
  },
  animation: {
    type: String,
    default: "move",
  },
});

const width = computed(() => {
  return props.width ? `${props.width}px` : "100%";
});
const height = computed(() => {
  return props.height ? `${props.height}px` : "100%";
});
const curProgress = computed(() => {
  return (props.percent >= 100 ? 100 : props.percent) + "%";
});
const background = computed(() => {
  const positions = [];
  const stripeCopy = JSON.parse(JSON.stringify(props.stripe));
  if (props.gradient) {
    stripeCopy.push(stripeCopy[0]);
  }
  stripeCopy.forEach((s, index) => {
    const sum = stripeCopy.slice(0, index).reduce((a, b) => a + b.width, 0);
    if (!props.gradient) {
      positions.push(sum);
    }
    positions.push(sum + s.width);
  });
  return `repeating-linear-gradient(
       ${props.stripeAngle}deg, ${stripeCopy
    .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 totalStripeWidth = computed(() => {
  return props.stripe.reduce((a, b) => a + b.width, 0);
});
const backgroundSize = computed(() => {
  let size = "";
  if (props.stripeAngle === 0) {
    size = "";
  }
  if (props.stripeAngle % 90 === 0) {
    size = `${totalStripeWidth.value}px ${props.height}px`;
  } else {
    size = `${
      totalStripeWidth.value / Math.sin((props.stripeAngle * Math.PI) / 180)
    }px ${props.height}px`;
  }
  return size;
});

onMounted(() => {
  initAnimation();
});
onBeforeUnmount(() => {
  document.styleSheets[0].deleteRule(0);
  document.styleSheets[0].deleteRule(1);
});
const initAnimation = () => {
  document.styleSheets[0].insertRule(
    `@keyframes move{
         from {
           background-position: 0 0;
         }
         to {
           background-position: ${
             totalStripeWidth.value /
             Math.sin((props.stripeAngle * Math.PI) / 180)
           }px 0;
         }
       }`,
    0
  );

  document.styleSheets[0].insertRule(
    `.bg {
              animation: ${props.animation} ${props.duration}s linear infinite
           }`,
    1
  );
};
</script>
<style lang="scss" scoped>
.wrapper {
  width: 100%;
  position: relative;
  display: flex;
  align-items: center;

  .progress {
    height: 100%;
    display: inline-block;
    width: 100%;
    background: #c7c7c7;
    border-radius: 100px;
    display: flex;
    justify-content: space-between;
    .bg {
      height: 100%;
      text-align: center;
      border-radius: 100px;
      transition: all 0.3s cubic-bezier(0.08, 0.82, 0.17, 1);
    }
  }
}

.percent {
  font-size: 14px;
  line-height: 24px;
  font-weight: bold;
  color: rgba(0, 0, 0, 0.45);
}
</style>

动图(省略)

2 增加百分比显示,位置选择以及前缀后缀文字


<template>
  <div class="wrapper" :style="{ width: width, height: height }">
    <slot name="prefix">
      <span v-if="prefix" class="prefix">
        {{ prefix }}
      </span>
    </slot>
    <div class="progress">
      <div
        class="bg"
        :style="{
          width: curProgress,
          background: percent >= 100 ? '#00EE00' : background,
          backgroundSize: backgroundSize,
        }"
      >
        <span
          v-if="position === 3 && percent >= 10"
          class="percent"
          :style="{
            fontSize: height,
            lineHeight: height,
            color: 'rgba(0, 0, 0, 0.8)',
          }"
        >
          {{ curProgress }}
        </span>
      </div>
      <span
        v-if="position === 2 || percent < 10"
        class="percent"
        :style="{
          fontSize: height,
          lineHeight: height,
          marginRight: '10px',
        }"
      >
        {{ curProgress }}
      </span>
    </div>
    <span
      v-if="position === 1"
      class="percent"
      :style="{
        marginLeft: '10px',
      }"
    >
      {{ curProgress }}
    </span>
    <slot name="suffix">
      <span v-if="suffix" class="suffix">
        {{ suffix }}
      </span>
    </slot>
  </div>
</template>
<script setup lang="ts">
import { computed, onMounted, onBeforeUnmount } from "vue";

const props = defineProps({
  width: {
    type: Number, // 进度条总宽度
    default: 300,
  },
  height: {
    type: Number, // 进度条高度
    default: 10,
  },
  percent: {
    type: Number, // 当前进度
    default: 50,
  },
  stripeAngle: {
    type: Number, // 进度条线的角度  和-x的夹角,顺时针
    default: 45,
  },
  stripe: {
    type: Array, // 进度条斑马线数组
    default: () => [
      {
        color: "#FF4500",
        width: 15,
      },
      {
        color: "#00EE00",
        width: 15,
      },
      {
        color: "#00FFFF",
        width: 15,
      },
    ],
  },
  gradient: {
    type: Boolean, // 渐变
    default: false,
  },
  prefix: {
    type: String, //前缀
    default: "",
  },
  suffix: {
    type: String, //后缀
    default: "",
  },
  position: {
    type: Number, // 百分比位置 0 不显示 1 外部  2 内部  3 进度条上
    default: 1,
  },
  duration: {
    type: Number, //动画时间
    default: 2,
  },
  animation: {
    type: String,
    default: "move",
  },
});

const width = computed(() => {
  return props.width ? `${props.width}px` : "100%";
});
const height = computed(() => {
  return props.height ? `${props.height}px` : "100%";
});
const curProgress = computed(() => {
  return (props.percent >= 100 ? 100 : props.percent) + "%";
});
const background = computed(() => {
  const positions = [];
  const stripeCopy = JSON.parse(JSON.stringify(props.stripe));
  if (props.gradient) {
    stripeCopy.push(stripeCopy[0]);
  }
  stripeCopy.forEach((s, index) => {
    const sum = stripeCopy.slice(0, index).reduce((a, b) => a + b.width, 0);
    if (!props.gradient) {
      positions.push(sum);
    }
    positions.push(sum + s.width);
  });
  return `repeating-linear-gradient(
       ${props.stripeAngle}deg, ${stripeCopy
    .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 totalStripeWidth = computed(() => {
  return props.stripe.reduce((a, b) => a + b.width, 0);
});
const backgroundSize = computed(() => {
  let size = "";
  if (props.stripeAngle === 0) {
    size = "";
  }
  if (props.stripeAngle % 90 === 0) {
    size = `${totalStripeWidth.value}px ${props.height}px`;
  } else {
    size = `${
      totalStripeWidth.value / Math.sin((props.stripeAngle * Math.PI) / 180)
    }px ${props.height}px`;
  }
  return size;
});

onMounted(() => {
  initAnimation();
});
onBeforeUnmount(() => {
  document.styleSheets[0].deleteRule(0);
  document.styleSheets[0].deleteRule(1);
});
const initAnimation = () => {
  document.styleSheets[0].insertRule(
    `@keyframes move{
         from {
           background-position: 0 0;
         }
         to {
           background-position: ${
             totalStripeWidth.value /
             Math.sin((props.stripeAngle * Math.PI) / 180)
           }px 0;
         }
       }`,
    0
  );

  document.styleSheets[0].insertRule(
    `.bg {
              animation: ${props.animation} ${props.duration}s linear infinite
           }`,
    1
  );
};
</script>
<style lang="scss" scoped>
.wrapper {
  width: 100%;
  position: relative;
  display: flex;
  align-items: center;

  .prefix,
  .suffix {
    font-size: 14px;
    color: #000;
    position: absolute;
    left: -70px;
  }

  .progress {
    height: 100%;
    display: inline-block;
    width: 100%;
    background: #c7c7c7;
    border-radius: 100px;
    display: flex;
    justify-content: space-between;
    .bg {
      height: 100%;
      text-align: center;
      border-radius: 100px;
      transition: all 0.3s cubic-bezier(0.08, 0.82, 0.17, 1);
    }
  }
}

.percent {
  font-size: 14px;
  line-height: 24px;
  font-weight: bold;
  color: rgba(0, 0, 0, 0.45);
}
</style>

默认效果:

改变斑马线角度:

改变斑马线宽度:

开启渐变:

改变百分比位置:

动图(省略)

  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 LVGL(Light and Versatile Graphics Library)来创建渐变效果的进度条。下面是一个使用 LVGL 创建渐变进度条的示例代码: ```c /* 创建一个父容器 */ lv_obj_t *parent = lv_cont_create(lv_scr_act(), NULL); lv_obj_set_size(parent, 200, 30); lv_obj_center(parent); /* 创建一个背景样式 */ static lv_style_t bg_style; lv_style_init(&bg_style); lv_style_set_bg_color(&bg_style, lv_color_hex(0xCCCCCC)); /* 创建一个进度条 */ lv_obj_t *progress_bar = lv_bar_create(parent, NULL); lv_obj_set_size(progress_bar, 180, 20); lv_obj_align(progress_bar, parent, LV_ALIGN_CENTER, 0, 0); lv_bar_set_range(progress_bar, 0, 100); // 设置进度条的范围 lv_bar_set_value(progress_bar, 40, LV_ANIM_ON); // 设置进度条的当前值,并启用动画 /* 创建一个前景样式 */ static lv_style_t fg_style; lv_style_init(&fg_style); lv_style_set_bg_color(&fg_style, lv_color_hex(0xFF0000)); // 设置前景样式为红色 /* 创建一个渐变样式 */ static lv_style_t grad_style; lv_style_init(&grad_style); lv_style_set_bg_grad_color(&grad_style, lv_color_hex(0xFF0000), lv_color_hex(0x00FF00), LV_GRAD_DIR_HOR); // 设置渐变色为红到绿的水平渐变 /* 应用样式到进度条 */ lv_bar_set_style(progress_bar, LV_BAR_STYLE_BG, &bg_style); lv_bar_set_style(progress_bar, LV_BAR_STYLE_INDIC, &grad_style); while (1) { lv_task_handler(); /* 持续更新界面 */ usleep(5000); } ``` 这个示例代码中,我们创建了一个父容器和一个进度条。我们设置进度条的范围为0到100,并将当前值设置为40,启用动画。然后,我们创建了背景样式和前景样式,并使用渐变样式设置进度条渐变效果。最后,我们将样式应用到进度条上,并使用 `lv_task_handler()` 函数来持续更新界面。 请注意,这只是一个简单的示例代码,你可以根据自己的需求进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值