css3实现渐变条形进度条

107 阅读1分钟
  <div class="progress">
    <div class="ignore-progress-container" :style="{ width: progressColorWidth }">
      <div class="ignore-progress-color"></div>
    </div>
    <!-- <div class="ignore-progress-icon"></div> -->
  </div>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps({
  width: {
    type: [Number, String],
    default: 0
  }
})

const progressColorWidth = computed(() => {
  if (!props.width) return props.width
  if (props.width.includes('%') || props.width.includes('px')) {
    return props.width
  } else if (typeof props.width === 'string' && !isNaN(props.width) && props.width < 1) {
    return Number(props.width) * 100 + '%'
  } else {
    return !isNaN(props.width) ? Number(props.width) + 'px' : undefined
  }
})
</script>
<style lang="scss" scoped>
.progress {
  width: 100%;
  height: 10px;
  background: #2e3646;
  box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 0.33) inset;
  position: relative;
}

.ignore-progress-container {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
}

.ignore-progress-color {
  position: relative;
  width: 100%;
  height: 100%;
  background: linear-gradient(270deg, #ffcf5d 14%, #f47021 67%);
  animation: fadeWidth 5s infinite;

  &::after {
    content: '';
    position: absolute;
    right: 3px;
    top: 50%;
    transform: translateY(-50%);
    height: 20px;
    width: 1px;
    background-color: rgba(255, 207, 93, 0.14);
    box-shadow: 3px 0px 3px 1px #fff;
    border-radius: 50%;
  }
}

@keyframes fadeWidth {
  0% {
    width: 0px;
  }

  100% {
    width: 100%;
  }
}
</style>