时间轴组件 by Vue.js

时间:2022-06-18
本文章向大家介绍时间轴组件 by Vue.js,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在繁重的业务工作中,如何提升自己的技术能力,而不是变为特定业务领域的熟练工,方法之一就是在业务工作当中提炼、抽象出通用的技术内容,总结归纳,并进一步思考是否有更好的解决办法。

在公司的项目开发中,涉及到了移动端H5页面的时间轴展示效果。现有的轮子比如ElementUI、iView中,都没有专门的时间轴组件,于是就萌生了自己封装一个的想法。

说干就干,杜绝拖延症,正好今天周末,就封装了一个时间轴的组件上传到了npm,大家有需求可以安装试一下。

npm install uni-time-line -S

效果如下图:

实现思路

开发一个vue组件,首先要确定好三要素propsslotevent。因为第一版功能比较简单,所以只使用了props

要实现一个时间轴,可以让用户自定义的内容包括,icon图标的样式、标题的文字、内容体的文字,还有两个条目之间的距离。

于是我们props的定义就出来了:

    uni_data: {//要使用时间轴的内容列表,包含标题和内容
      type: Array,
      default() {
        return [
          {
            title: "这是标题",
            content: "这是内容"
          },
          {
            title: "标题过长会显示省略号",
            content: "内容过长会隐藏"
          },
          {
            title: "更多效果可以自定义",
            content: "更多效果可以自定义"
          }
        ];
      }
    },
    space: {//条目之间的距离,默认100px
      type: String,
      default: "100px"
    },
    uni_icon: {//icon的地址,默认提供一个我觉得还算好看的
      type: String,
      default: "http://qiniu.iborge.cn/dian.png"
    }

上面的定义中,稍微不太好理解的是,如何将条目之间的距离动态的设定到style属性上呢?在这里我们使用了computed,即:

  computed: {
    uni_space() {
      return "height:" + this.space;
    }
  },

这样,用户传入100px,设置到属性上就是style="height:100px".

下一步就是如何实现时间轴的效果,在这里我想到的一个简单的思路是使用div的左边框。

其实大家看到的每条时间轴的竖线,都是一个box的左边框。将其相对定位一下,将包含icon和标题的p绝对定位在竖线顶端,就实现了时间轴的效果。代码如下:

.uni_listbox {
  box-sizing: border-box;
  margin: 20px;
  height: 60px;
  position: relative;
}

.uni_listbox p {
  position: absolute;
  top: 0;
  left: 0;
  margin-left: -9px;
  margin-top: -18px;
  vertical-align: middle;
  height: 16px;
  line-height: 16px;
}

这么一来,基本上完成了时间轴的效果,但还有一点小小的问题。就是列表的最后一项,会有一条竖线,但是下面已经没有了新的内容。这样看上去就会很难看,应该如何处理呢?其实只需要简单的一句表达式就可以:

:class="[uni_data.length==index+1?uni_listbox_last:uni_listbox_notlast,uni_listbox]"

上面这句话的意思是,如果判断当前为数据的最后一项,那么就使用uni_listbox_last的样式,否则就用uni_listbox_notlast,而uni_listbox的样式,则不管条件如何都会生效。

这样,我们只要把uni_listbox_last的边框去掉,高度降低,就实现了文章开头中图片中的效果。

至此,时间轴的组件基本上就开发完了。

还可以根据自己的需求,添加标题行内容溢出显示省略号等各种效果。

下面贴上完整的源码:

<template>
  <div id="uni_timeline">
    <div
      :style="uni_space"
      v-for="(item,index) in uni_data"
      :key="index"
      :class="[uni_data.length==index+1?uni_listbox_last:uni_listbox_notlast,uni_listbox]"
    >
      <p>
        <img :src="uni_icon" :class="uni_timeline_icon">
        <span class="uni_timeline_title">{{item.title}}</span>
      </p>
      <div class="uni_timeline_content">{{item.content}}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      uni_timeline_icon: "uni_timeline_icon",
      uni_listbox_last: "uni_listbox_last",
      uni_listbox_notlast: "uni_listbox_notlast",
      uni_listbox: "uni_listbox"
    };
  },
  computed: {
    uni_space() {
      return "height:" + this.space;
    }
  },
  props: {
    uni_data: {
      type: Array,
      default() {
        return [
          {
            title: "这是标题",
            content: "这是内容"
          },
          {
            title: "标题过长会显示省略号",
            content: "内容过长会隐藏"
          },
          {
            title: "更多效果可以自定义",
            content: "更多效果可以自定义"
          }
        ];
      }
    },
    space: {
      type: String,
      default: "100px"
    },
    uni_icon: {
      type: String,
      default: "http://qiniu.iborge.cn/dian.png"
    }
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
.uni_listbox {
  box-sizing: border-box;
  margin: 20px;
  height: 60px;
  position: relative;
}
.uni_listbox_last {
  height: 20px;
}
.uni_listbox_notlast {
  height: 60px;
  margin-left: 18px;
  border-left: 3px solid rgb(228, 228, 228);
}
.uni_listbox p {
  position: absolute;
  top: 0;
  left: 0;
  /* transform: translateY(-50%); */
  margin-left: -9px;
  margin-top: -18px;
  vertical-align: middle;
  height: 16px;
  line-height: 16px;
}
.uni_timeline_icon {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  width: 16px;
  height: 16px;
  vertical-align: middle;
}
.uni_timeline_title {
  font-weight: bold;
  text-align: left;
  display: inline-block;
  width: 300px;
  margin: 0 30px;
  color: rgb(100, 100, 100);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.uni_timeline_content {
  text-align: left;
  height: 70%;
  padding: 10px 21px;
  font-size: 14px;
  color: rgb(100, 100, 100);
  overflow: hidden;
}
</style>