目录
  • 一、实现组件timelineh.vue
  • 二、调用组件

本文主要介绍了vue实现两列水平时间轴的示例代码,分享给大家,具体如下:

先上图,主要实现两列水平时间轴,查看了很多人实现的,水平只有一列,并且elementui的时间轴只有竖着的,不支持横向,最后只能含泪自己实现了,没想到还可以,只是如果数据很多翻页还没实现,有实现过这种的掘友可以艾特我一下。

一、实现组件timelineh.vue

timelineh.vue中的h代表横,起名字烦恼,哈哈。

<template>
    <ul class="timelinehengbox">
        <li class="timelinehengitem" v-for="(item,index) in timelinelist" :key="index" 
            :style="{left: index % 2 != 0 ? (lihalf*index)+52+'px':0,'border-right': index == timelinelist.length - 2 ?'1px solid #fefefe' : 'none'}">
            <div class="timeline_dot" :style="{top: index % 2 != 0 ? '-5px' : '93px'}"></div>
            <div class="timeline_dot" v-show="index == timelinelist.length - 2" style="right: -6px;top:93px;left: unset;"></div>
            <div class="timeline_wapper flex" :class="{'mt20': index % 2 != 0}">
                <img src="../../static/img/excelicon.png" class="bjicon" :style="{'margin': index % 2 != 0 ? '11px 44px 0 42px' :''}">
                <div>{{item.content}}</div>
            </div>
            <div class="timelinedate_bottom" :style="{'top': index % 2 != 0 ? '-20px' : '103px'}">{{item.datetime}}</div>
        </li>
    </ul>
</template>

<script>
    export default {
        name: 'timelineh',
        props: {
            timelinelist: {
                type: array,
                default: []
            }
        },
        data () {
            return {
                liwidth: 496,//整个li的宽度,要和下面的li样式统一
                lihalf: 248,//这里是li一半的宽度,使中间横线上的点可以找到准确的位置
            }
        }
    }
</script>
<style scoped>
    .timelinehengbox {
        color: #fff;
        margin: 0;
        padding: 0 26px;
        width: 92%;
        padding-top: 18px;
        padding-bottom: 10px;
        margin-left: 26px;
        height: 87px;
        border-bottom: 3px solid #fefefe;
    }
    .timelinehengitem {
        list-style: none;
        height: 97px;
        width: 496px;
        border-left: 1px solid #fefefe;
        float: left;
        border-bottom: 3px solid #fefefe;
        position: relative;
    }
    .timelinehengitem:nth-child(2n) {
        position: absolute;
        left: 248px;
        border-bottom: 0;
        top: 115px;
    }
    .timeline_dot {
        width: 10px;
        height: 11px;
        background: #fefefe;
        position: absolute;
        left: -5px;
        top: 93px;
    }
    .timeline_dot:nth-child(2n) {
        top: -7px;
    }
    .timeline_wapper {
        width: 400px;
        text-align: left;
        font-size: 12px;
        color: #ffffff;
        line-height: 20px;
    }
    .mt20 {
        margin-top: 20px;
    }
    .bjicon {
        width: 32px;
        height: 32px;
        margin: 31px 44px 0 42px;
    }
    .timelinedate_bottom {
        width: 80px;
        height: 20px;
        position: absolute;
        top: 103px;
        left: -60px;
        text-align: left;
        font-size: 14px;
        font-weight: bold;
        color: #ffba00;
        margin-left: 77px;
    }
</style>

实现思路:

  • 竖线的实现,通过li设置左边框实现,这里主要是要吧每第二个li放到前一个li的中间去,所以要设置li整个宽度的一半用绝对定位left实现,距离顶部的距离也要计算好。
  • 每个时间点的方块通过绝对定位实现,需要注意的是上面列的节点和下面列的节点距离top是不一样的,所以我用了css的:nth-child(2n)实现每第二个li的top距离。
  • 最后就是日期节点文字,也是判断li的奇偶数设置不同的top值
  • 因为没有翻页功能,所以li的长度要做适配或者数据量多要把li的宽度减少,不然数据量多就很不好看,暂时没有优化,但是如果是适应笔记本电脑还是可以通过修改li的宽度和lihalf来实现。

二、调用组件

<div class="timelinehengcontainer">
     <timelineh :timelinelist='timelinelist' />
</div>

js:

import timelineh from "@/components/timelineh.vue";
components: {
        timelineh
},

data() {
    return {
          timelinelist: [

                {
                    datetime: '2021-09',
                    content: '欢迎挖矿,天天挖矿,得到矿石,哈哈哈,欢迎挖矿,天天挖矿,得到矿石,哈哈哈,欢迎挖矿,天天挖矿,得到矿石,哈哈哈,欢迎挖矿,天天挖矿,得到矿石,哈哈哈。'
                },{
                    datetime: '2021-10',
                    content: '冬天要注意保暖,太冷了呢,冬天要注意保暖,太冷了呢,冬天要注意保暖,太冷了呢,冬天要注意保暖,太冷了呢。'
                },{
                    datetime: '2021-11',
                    content: '更文挑战三十天正式开始啦,我想要投影仪,一直想买的。'
                },{
                    datetime: '2021-12',
                    content: '就要到月底啦,新的一年开始,新年快乐,新的一年开始,新年快乐,新的一年开始,新年快乐。'
                }
            ]
    }
}

css:

.timelinehengcontainer {
        width: 100%;
        height: 227px;
        background-image: url('../../static/img/timelinebg.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
}

好啦,这就实现了水平两列的时间轴了,可以把代码复制下来直接用(使用cv大法吧~)。当时弄了两天,参考了elementui的纵向时间轴的画法,没有做出来,又用纯div和css实现也没有成功,主要是两列的竖线不知道该怎么画,还有节点,最后想起用li直接加个边框实现。

到此这篇关于vue实现两列水平时间轴的示例代码的文章就介绍到这了,更多相关vue 两列水平时间轴内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!