vue+css实现颜色渐变

<template>
  <div class="ColorGradient">
    <div :style="legendDivStyle">
      <div class="ui-legend-cell-div">
        <template v-for="item in colorItems">
          <div :key="item.id" :style="item.style"></div>
        </template>
      </div>
    </div>
  </div>
</template>
<script>
import { ColorUtil } from "../assets/js/colorUtil";
export default {
  name: "ColorGradient",
  data() {
    return {
      options: {
        data: {
          colors: [
            "#040274",
            "#040281",
            "#0502a3",
            "#0502b8",
            "#0502ce",
            "#0502e6",
            "#0602ff",
            "#235cb1",
            "#307ef3",
            "#269db1",
            "#30c8e2",
            "#32d3ef",
            "#3be285",
            "#3ff38f",
            "#86e26f",
            "#3ae237",
            "#b5e22e",
            "#d6e21f",
            "#fff705",
            "#ffd611",
            "#ffb613",
            "#ff8b13",
            "#ff6e08",
            "#ff500d",
            "#ff0000",
            "#de0101",
            "#c21301",
            "#a71001",
            "#911003",
          ],
          step: 30,
          isvertical: false,
        },
        style: {
          width: "500px",
          height: "70px",
        },
      },
      colorItems: [],
      legendDivStyle: {},
    };
  },
  computed: {},
  mounted() {
    this.initLegend();
  },
  methods: {
    initLegend() {
      if (!this.options || !this.options.data) {
        return;
      }
      let _data = this.options.data;
      let _style = this.options.style;
      this.isvertical = false;
      let totalWidth = parseInt(_style.width.replace("px", ""));
      let totalHeight = parseInt(_style.height.replace("px", ""));
      this.legendDivStyle = {};
      this.legendDivStyle.width = _style.width;
      let _colors = _data.colors || [];
      let _step = _data.step || 10;
      let _cellWidth = totalWidth / ((_colors.length - 1) * _step + 1);
      let _cellHeight =
        (totalHeight - 30) / (_colors.length * _step - _step + 1);
      for (let i = 0; i < _colors.length; i++) {
        let _gradient = null;
        if (i < _colors.length - 1) {
          _gradient = ColorUtil.gradientColor(
            _colors[i],
            _colors[i + 1],
            _step
          );
        } else {
          _gradient = ColorUtil.gradientColor(_colors[i], _colors[i], 1);
        }
        for (let j = 0; j < _gradient.length; j++) {
          let _id = "legend" + (i + 1) + "-" + j;
          let _style = {
            width: _cellWidth + "px",
            height: "100%",
            float: "left",
            backgroundColor: _gradient[j],
          };
          this.colorItems.push({
            id: _id,
            style: _style,
          });
        }
      }
    },
  },
};
</script>
<style scoped>
.ColorGradient {
  width: 100%;
  height: 100%;
}
.ui-legend-cell-div {
  display: flex;
  width: 100%;
  height: 20px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

侧耳倾听...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值