Vue.js 带拖动和播放功能的时间轴

带拖动和播放功能的时间轴

timeline-slider-vue

Demo

Github

环境

  • node V12.20.0
  • npm 6.14.8

📦 Install

npm install --save timeline-slider-vue

🔧 Usage

main.js

import TimelineSliderVue from 'timeline-slider-vue'
import 'timeline-slider-vue/lib/timeline-slider-vue.css'
Vue.use(TimelineSliderVue)

```vue
    <TimelineSliderVue
      :date="date"
      :mask="mask"
      :mark-date="markDate"
      :lock-date="lockDate"
      :play="play"
      :play-speed="playSpeed"
      @change="handleChange"
      @input="handleInput">
      <div
        slot="sliderContent"
        slot-scope="scope">
        {{ scope.data }}
      </div>
    </TimelineSliderVue>

使用示例

<template>
	<div id="app">
		<TimelineSliderVue
			:date="date"
			:mask="mask"
			:mark-date="markDate"
			:lock-date="lockDate"
			:play="play"
			:play-speed="playSpeed"
			@change="handleChange"
			@input="handleInput"
		>
			<div slot="sliderContent" slot-scope="scope">
				{{ scope.data }}
			</div>
		</TimelineSliderVue>
	</div>
</template>

<script>
export default {
	data() {
		return {
			playSpeed: 1000, // 播放速度
			play: false, // 自动播放
			lockFlag: false,
			markFlag: false,
			lockDate: [], // 锁定的日期(滑动结束时自动跳到指定的日期)
			markDate: [], // 做标记的日期
			mask: true,
			date: '2022-06-01',
		}
	},
	methods: {
		handleInput(date, value) {
			console.log('input', date, value)
		},
		handleChange(date, value) {
			console.log('change', date, value)
		},
	},
}
</script>

竖向模式

    <TimelineSliderVue
      vertical
      height="240px"
      :max-value="100"
      :min-value="0"
      :init-value="40"
      @change="handleChange"
      @input="handleInput"
    >
      <div slot="sliderContent" slot-scope="scope">
        <div>{{ scope.value }}</div>
      </div>
    </TimelineSliderVue>

Available props

参数类型默认值说明
verticalBooleanfalse竖向模式(只有滑块功能样式,没有日期等功能)
initValueNumber0v-model 绑定的初始值(仅在 vertical = true 时生效)
minValueNumber0最小值(仅在 vertical = true 时生效)
maxValueNumber100最大值(仅在 vertical = true 时生效)
dateString当日yyyy-MM-dd 格式的日期,根据传入的日期,设置滑块的位置
maskBooleantrue拖动过程中是否显示遮罩层
mark-dateArray[]一些特殊日期标注,例如 [‘2022-03-08’, ‘2022-06-18’, ‘2022-11-11’]
lock-dateArray[]锁定的日期,只能在指定日期下切换,当滑块拖动到其他位置,自动跳到离指定日期最近的日期处例如 [‘2022-03-08’, ‘2022-06-18’, ‘2022-11-11’]
playBooleanfalse播放
playSpeedNumber1000播放速度,同 setInterval milliseconds 参数

slot

参数说明
sliderContent滑块内容

Events

事件名称说明回调参数
change值改变时触发(使用鼠标拖拽时,只在松开鼠标后触发)改变后的值
input数据改变时触发(使用鼠标拖拽时,活动过程实时触发)改变后的值

Project setup


yarn install

Compiles and hot-reloads for development


yarn serve

Compiles and minifies for production


yarn build

Lints and fixes files


yarn lint

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
您可以使用Vue2和CSS3来创建一个24小时时间轴可拖动进度条。以下是实现步骤: 1. 首先,在您的Vue组件中创建一个容器元素来包含时间轴和进度条: ```html <div class="timeline-container"> <div class="timeline"></div> <div class="progress-bar"></div> </div> ``` 2. 在CSS文件中定义时间轴和进度条的样式: ```css .timeline-container { position: relative; width: 100%; height: 50px; } .timeline { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: #f2f2f2; border: 1px solid #ccc; } .progress-bar { position: absolute; top: 0; left: 0; width: 10px; height: 100%; background-color: #007bff; cursor: pointer; } ``` 在上面的示例中,我们定义了一个容器元素,一个时间轴元素和一个进度条元素。时间轴元素的宽度为100%,高度为50px,背景颜色为灰色,边框为1px实线。进度条元素的宽度为10px,高度为100%,背景颜色为蓝色,光标为指针。 3. 在您的Vue组件中使用Vue的`mounted`生命周期钩子来初始化进度条元素的位置和绑定拖拽事件: ```javascript mounted() { const progressBar = document.querySelector('.progress-bar'); const timeline = document.querySelector('.timeline'); const timelineWidth = timeline.offsetWidth - progressBar.offsetWidth; progressBar.style.left = '0px'; progressBar.addEventListener('mousedown', function(event) { const startX = event.pageX - progressBar.offsetLeft; const offsetX = timelineWidth / 24; document.addEventListener('mousemove', moveProgressBar); function moveProgressBar(event) { let left = event.pageX - startX; if (left < 0) { left = 0; } if (left > timelineWidth) { left = timelineWidth; } const hour = Math.floor(left / offsetX); const minute = Math.floor((left % offsetX) / (offsetX / 60)); const time = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`; progressBar.style.left = `${left}px`; console.log(time); } document.addEventListener('mouseup', function() { document.removeEventListener('mousemove', moveProgressBar); }); }); } ``` 在上面的示例中,我们获取了进度条和时间轴元素,然后计算出时间轴的宽度,并将进度条的位置初始化为0。我们绑定了进度条的`mousedown`事件,并在事件处理程序中计算出鼠标的起始位置和时间轴每个小时的偏移量。然后,我们绑定了文档的`mousemove`和`mouseup`事件,以便在拖动进度条时更新进度条的位置,并输出当前的时间。 4. 最后,您可以在控制台中看到输出的时间,并在事件处理程序中使用它来更新您的Vue组件中的数据。 总之,您可以使用Vue2和CSS3来创建一个24小时时间轴可拖动进度条,然后在进度条被拖动时更新进度条的位置,并输出当前的时间

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值