HTML实现时间轴跳转功能

HTML实现时间轴跳转功能

最近在做一个微信聊天数据备份的项目,导出的聊天记录保存为HTML格式与微信界面相似,较为美观。但在聊天数据太多的情况下,很难找到指定日期的消息,于是开发了时间轴功能,支持时间轴点击跳转至指定日期消息。

效果展示

  1. 有消息的月份黑色显示、无消息的月份灰色显示、当前消息月份特殊标注

image.png

  1. 点击年份自动折叠\释放月份

timeline1.gif

  1. 点击年份、月份自动跳转至当月首条消息
    timeline2.gif

代码实现

CSS代码

时间轴区域

这里用CSS代码定义了时间轴的区域,容器和年份、月份的CSS样式

.bar {
    width: 300px;
    position: relative;
    background-color: #ebebeb;
    border-right: solid 1px #d6d6d6;
    --left: 60%;
    --color: #0d5dff;
}
.timeline-area {
    flex: 1;
    height: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    /* padding-bottom: 10px; */
    padding-right: 5px;
    border: 1px solid #ccc;
}

.timeline-wrapper {
    flex: 1;
    overflow-y: auto;
    padding: 30% 0;
}

.timeline {
    position: relative;
    height: 100%;
    width: 100%;
    display: flex;
    overflow: hidden;
    overflow-y: auto;
    /* 添加 Flex 布局 */
    flex-direction: column;
    /* 将项目垂直排列 */
}

.timeline-item-year {
    height: 40px;
    position: relative;
    display: flex;
    /* line-height: 50px; */
}

.timeline-item-month {
    height: 25px;
    display: flex;
    /* line-height: 40px; */
}

添加虚线

这里用伪元素before实现时间轴的虚线,定义了年份、月份和当前月份的小圆点CSS样式

.timeline::before {
    content: "";
    position: absolute;
    left: var(--left);
    width: 1px;
    height: 600px;
    top: 20px;
    bottom: 0;
    background-image: linear-gradient(to bottom,
            rgba(144, 156, 173, 0.6) 60%,
            rgba(255, 255, 255, 0) 0%);
    background-position: left;
    background-size: 1px 5px;
    background-repeat: repeat-y;
}

.timeline-dot-month {
    left: var(--left);
    width: 7px;
    height: 7px;
    /* position: absolute; */
    position: relative;
    border-radius: 50%;
    box-shadow: 0 0 0 1px #d8d8d8;
    background: white;
    text-align: center;
    top: 50%;
    transform: translateY(-50%);
    line-height: 40px;
    margin-left: -3px;
}

.timeline-dot-year {
    left: var(--left);
    width: 10px;
    height: 10px;
    /* position: absolute; */
    position: relative;
    border-radius: 50%;
    box-shadow: 0 0 0 1px #d8d8d8;
    background: black;
    text-align: center;
    top: 50%;
    transform: translateY(-50%);
    line-height: 40px;
    margin-left: -4.5px;
}

.timeline-item-month.current .timeline-dot-month {
    width: 7px;
    height: 7px;
    background-color: var(--color);
    box-shadow: 0 0 4px var(--color);
    border: 1px solid white;
    margin-left: -3px;
}

设置字体样式

这里定义了年份、月份的间距、字体样式等

.timeline-item-year .timeline-right {
    position: relative;
    margin: 0px 0px 0px calc(var(--left) + 15px);
    /* background: greenyellow; */
    height: 40px;
    line-height: 40px;
}

.timeline-item-month .timeline-right {
    position: relative;
    margin: 0px 0px 0px calc(var(--left) + 15px);
    /* background: greenyellow; */
    height: 25px;
    line-height: 25px;
}

.timeline-right:hover {
    color: red;
    /* 设置字体颜色为红色,可以根据需求修改颜色值 */
}

.no-msg-month {
    color: #9e9898;
}

.hidden-month {
    display: none;
}

HTML代码

<div class="bar">
        <div class="timeline-area">
            <div class="timeline-wrapper">
                <div class="timeline" id="timeline">
                    <div id="2023">
                        <div class="timeline-item-year">
                            <div class="timeline-dot-year"></div>
                            <div class="timeline-right">2022</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">01月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">02月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">03月</div>
                        </div>
                        <div class="timeline-item-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">04月</div>
                        </div>
                        <div class="timeline-item-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">05月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">06月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">07月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">08月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">09月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">10月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">11月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month hidden-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">12月</div>
                        </div>
                    </div>
                    <div id="2023">
                        <div class="timeline-item-year">
                            <div class="timeline-dot-year"></div>
                            <div class="timeline-right">2023</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">01月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">02月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">03月</div>
                        </div>
                        <div class="timeline-item-month current">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">04月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">05月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">06月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">07月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">08月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">09月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">10月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">11月</div>
                        </div>
                        <div class="timeline-item-month no-msg-month">
                            <div class="timeline-dot-month"></div>
                            <div class="timeline-right">12月</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue中实现时间功能可以通过以下步骤: 1. 定义数据结构:时间轴需要一个数据结构来存储每个时间点的信息,可以定义一个数组,每个元素包含时间、标题、描述等信息。 2. 创建组件:创建一个组件来展示时间轴,可以使用v-for指令遍历数据结构,将每个时间点渲染为一个卡片。 3. 样式设计:设计时间轴的样式,可以使用CSS实现,包括卡片的布局、字体颜色、背景色等。 4. 添加交互:为了让用户可以通过点击时间点来查看详细信息,可以在组件中添加点击事件,通过传递参数来显示对应的卡片。 下面是一个简单的时间轴组件实现: ``` <template> <div class="timeline"> <div class="timeline-item" v-for="(item, index) in items" :key="index" @click="showDetail(item)"> <div class="timeline-time">{{ item.time }}</div> <div class="timeline-content"> <h3>{{ item.title }}</h3> <p>{{ item.description }}</p> </div> </div> </div> </template> <script> export default { data() { return { items: [ { time: "2021-01-01", title: "事件1", description: "事件1描述" }, { time: "2021-02-01", title: "事件2", description: "事件2描述" }, { time: "2021-03-01", title: "事件3", description: "事件3描述" }, { time: "2021-04-01", title: "事件4", description: "事件4描述" }, ], }; }, methods: { showDetail(item) { // 弹出详细信息 console.log(item); }, }, }; </script> <style> .timeline { border-left: 2px solid #ccc; padding: 10px; } .timeline-item { display: flex; margin-bottom: 20px; cursor: pointer; } .timeline-time { font-size: 16px; font-weight: bold; color: #333; width: 100px; } .timeline-content { margin-left: 20px; } .timeline-content h3 { font-size: 18px; font-weight: bold; color: #333; margin-bottom: 10px; } .timeline-content p { font-size: 14px; color: #666; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

枕石zs

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

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

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

打赏作者

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

抵扣说明:

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

余额充值