Vue3实现横向交错时间轴组件

先上图
在这里插入图片描述

前提===>保证项目安装有ant-design-vue组件库

项目使用的antdv的按需加载,所以没有导入组件语句

gitee联系作者:https://gitee.com/zlflying
<template>
  <div class="main">
    <LeftCircleOutlined @click="prev" class="sw_icon"
                        :style="{ color: isFirst? '#eee' : 'var(--main-color)'}"/>
    <div class="timeline" ref="timeline">
      <div class="timeline-item" v-for="(item, index) in props.items" :key="index" @click="current = index"
           :style="{marginLeft: index!==0? `-${60}px`: '20px'}">
        <div class="card-top" v-if="index%2 === 0" :class="index===current ? 'card-active' : ''">
          <img :src="item.imgUrl" alt="" style="width: 90px;height:90px;border-radius: 8px;">
          <div class="content">
            <div>{{ item.date }}</div>
            <div>{{ item.title }}</div>
          </div>
        </div>
        <!--        占位-->
        <div v-else style="height: 120px">
        </div>
        <div class="cirque" :class="index===current ? 'active' : ''">
          <div class="dot"></div>
        </div>
        <div class="card-bottom" v-if="index%2 !== 0" :class="index===current ? 'card-active' : ''">
          <img :src="item.imgUrl" alt="" style="width: 90px;height:90px;border-radius: 8px;">
          <div class="content">
            <div>{{ item.date }}</div>
            <div>{{ item.title }}</div>
          </div>
        </div>
        <!--        占位-->
        <div v-else style="height: 120px">
        </div>
      </div>
    </div>
    <RightCircleOutlined @click="next" class="sw_icon"
                         :style="{ color: isLast? '#eee' : 'var(--main-color)'}"/>
  </div>
</template>

<script setup>
import {computed, ref} from "vue";
import {LeftCircleOutlined, RightCircleOutlined} from "@ant-design/icons-vue";

const props = defineProps({
  items: {
    type: Array,
    required: true,
    default: () => [],
  },
});

/** 当前显示的卡片索引 */
const current = ref(0);

/** 滚动容器 */
const timeline = ref(null);

/** 下一个 **/
const next = () => {
  if (current.value < props.items.length - 1) {
    timeline.value.children[current.value + 1].scrollIntoView({
      behavior: 'smooth',
      block: 'center',
      inline: 'center',
    });
    current.value++;
  }
};

/** 上一个 **/
const prev = () => {
  if (current.value > 0) {
    timeline.value.children[current.value - 1].scrollIntoView({
      behavior: 'smooth',
      block: 'center',
      inline: 'center',
    });
    current.value--;
  }
};

/** 是否是第一个卡片 */
const isFirst = computed(() => {
  return current.value === 0;
});

/** 是否是最后一个卡片 */
const isLast = computed(() => {
  return current.value === props.items.length - 1;
});


</script>

<style scoped>
.main {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: sroll linear 2s;
}

.main .timeline {
  width: calc(100% - 200px);
  display: flex;
  overflow-x: hidden;
  padding: 16px;
  transition: sroll linear 2s;
  position: relative;
}

.main .timeline::before {
  content: ''; /* 伪元素需要内容属性,即使它是空的 */
  position: absolute;
  top: 50%; /* 将线定位在容器的垂直中心 */
  left: 0;
  right: 0;
  height: 2px; /* 线的高度 */
  width: 1000000000000px;
  background-color: rgba(157, 158, 178, 0.75); /* 线的颜色 */
}

.cirque {
  width: 29px;
  height: 29px;
  border-radius: 50px;
  border: 1px solid #9D9EB2;
  background: #FFFFFF;
  margin: 20px;
  z-index: 3;
}

.cirque .dot {
  width: 23px;
  height: 23px;
  transform: translate(0.5px, 0.5px);
  border-radius: 50px;
  background: #9D9EB2;
  margin: 2px;
}

.cirque.active {
  transition: all ease-in 0.3s;
  border-color: var(--main-color);
}

.cirque.active .dot {
  background: var(--main-color);
}

.timeline .timeline-item {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0 10px;
}


.card-top, .card-bottom {
  position: relative;
  width: 220px;
  height: 120px;
  box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  z-index: 3;
}

.card-top:hover, .card-bottom:hover {
  color: var(--main-color);
  transition: all ease-in 0.2s;
}

.content {
  margin-left: 10px;
  display: flex;
  flex-direction: column;
}

.card-top.card-active,
.card-bottom.card-active {
  border: 2px solid var(--main-color);
  transition: all ease-in 0.2s;
}

.sw_icon {
  font-size: 38px;
  user-select: none;
}

</style>

使用

<template>
<TimeLine :items="itemList"></TimeLine>
</template>
<script setup>
import TimeLine from "@/components/TimeLine.vue";
/** 条目列表 */
const itemList = ref([
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    },
    {
        imgUrl: '/test.png',
        title: '标题',
        date: '2021-01-01',
    }
]);
</script>
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以尝试使用 Vue.js 开发时间线组件,具体实现可以参考以下步骤: 1. 安装必要的依赖: ```bash npm install vue moment ``` 2. 创建一个 TimelineItem.vue 组件,用于展示单个时间线条目的内容: ```vue <template> <div class="timeline-item"> <div class="timeline-item__date">{{ formattedDate }}</div> <div class="timeline-item__content"> <slot></slot> </div> </div> </template> <script> import moment from 'moment'; export default { props: { date: { type: String, required: true } }, computed: { formattedDate() { return moment(this.date).format('YYYY-MM-DD'); } } } </script> <style scoped> .timeline-item { position: relative; padding: 1rem; border-left: 2px solid #e5e5e5; } .timeline-item__date { position: absolute; top: 0; left: -7rem; width: 6rem; text-align: right; font-size: 0.8rem; color: #999; } .timeline-item__content { margin-left: 2rem; } </style> ``` 3. 创建一个 Timeline.vue 组件,用于容纳多个时间线条目,并实现横向滚动: ```vue <template> <div class="timeline"> <div class="timeline__wrapper" ref="wrapper"> <div class="timeline__container" ref="container"> <slot></slot> </div> </div> <div class="timeline__controls"> <button @click="scrollLeft">←</button> <button @click="scrollRight">→</button> </div> </div> </template> <script> export default { methods: { scrollLeft() { this.$refs.wrapper.scrollBy({ left: -200, behavior: 'smooth' }); }, scrollRight() { this.$refs.wrapper.scrollBy({ left: 200, behavior: 'smooth' }); } } } </script> <style scoped> .timeline { position: relative; padding: 1rem 0; overflow-x: auto; } .timeline__wrapper { display: flex; flex-direction: row; align-items: flex-start; } .timeline__container { display: flex; flex-direction: row; } .timeline__controls { position: absolute; top: 0; right: 0; display: flex; flex-direction: row; align-items: center; justify-content: space-between; width: 4rem; height: 100%; background-color: rgba(255, 255, 255, 0.8); z-index: 1; } .timeline__controls button { border: none; background-color: transparent; font-size: 1.2rem; cursor: pointer; outline: none; } </style> ``` 4. 在需要展示时间线的页面中使用 Timeline 和 TimelineItem 组件: ```vue <template> <div> <h1>My Timeline</h1> <Timeline> <TimelineItem date="2021-01-01"> <h3>Event A</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </TimelineItem> <TimelineItem date="2021-02-01"> <h3>Event B</h3> <p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </TimelineItem> <TimelineItem date="2021-03-01"> <h3>Event C</h3> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </TimelineItem> <TimelineItem date="2021-04-01"> <h3>Event D</h3> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p> </TimelineItem> </Timeline> </div> </template> <script> import Timeline from './Timeline.vue'; import TimelineItem from './TimelineItem.vue'; export default { components: { Timeline, TimelineItem } } </script> ``` 通过以上步骤,你就可以创建一个简单的横向时间线组件,用户可以通过左右滚动查看不同时间点的事件。你可以根据自己的需求对组件进行进一步的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值