基于vue实现8小时带刻度的时间轴根据当前时间实时定位功能

yizhihongxing

下面是基于Vue实现8小时带刻度的时间轴根据当前时间实时定位功能的完整攻略:

1.需求分析

  • 时间轴分为8个小时
  • 时间轴刻度需对应具体时刻,如每小时1个刻度
  • 时间轴需根据当前实际时间进行实时定位,可自动滚动到相应时刻

2. 实现方式

使用Vue框架实现,具体步骤如下:

2.1 创建Vue项目

创建一个新项目,命名为time-axis,安装所需依赖:

// 安装Vue
npm install vue
// 安装Vue-cli脚手架
npm install -g @vue/cli
// 创建新项目
vue create time-axis

2.2 创建时间轴组件

创建一个名为TimeAxis.vue的组件,用于展示时间轴的UI布局。

在组件中,使用v-for指令遍历8个小时,并使用v-if判断当前小时数是否需要显示小时标签。

时间轴中的刻度条和当前时间的标记,可以使用CSS样式进行实现。

<template>
  <div class="time-axis">
    <div class="time-axis-hour" v-for="i in 8" :key="i" v-if="isShowHour(i)">
      {{ i }}:00
    </div>
    <div class="time-axis-line"></div>
    <div class="time-axis-cursor" ref="cursor"></div>
  </div>
</template>

<script>
export default {
  name: 'TimeAxis',
  methods: {
    isShowHour(hour) {
      if ((new Date().getHours() + hour > 8) || (new Date().getHours() + hour < 1)) {
        return false
      } else {
        return true
      }
    },
    setCursorPosition() {
      let currentHour = new Date().getHours()
      let currentMinute = new Date().getMinutes()
      let currentSecond = new Date().getSeconds()
      let currentWidth =
        (currentHour - 1) * 100 + currentMinute * 1.67 + currentSecond * 0.03
      this.$refs.cursor.style.width = `${currentWidth}px`
    }
  },
  mounted() {
    setInterval(() => {
      this.setCursorPosition()
    }, 1000)
  }
}
</script>

<style>
.time-axis {
  position: relative;
}
.time-axis-hour {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(12.5% * (var(--screen-width, 1920px) / 1920));
  padding-left: calc((var(--screen-height, 1080px) / 1080) * 10px);
  font-size: calc((var(--screen-height, 1080px) / 1080) * 14px);
  color: #999;
}
.time-axis-hour::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 1px;
  z-index: 100;
  background-color: #ddd;
}
.time-axis-line {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 100%;
  height: 1px;
  background-color: #ddd;
  z-index: 50;
}
.time-axis-cursor {
  position: absolute;
  top: 20%;
  height: 60%;
  background-color: #ccc;
  z-index: 200;
}
</style>

2.3 添加实时定位功能

TimeAxis组件的methods中添加一个setCursorPosition方法,用于更新当前时间标记的位置。

由于需求为实时定位,需要使用setInterval方法定时更新。

mounted() {
  setInterval(() => {
    this.setCursorPosition()
  }, 1000)
}

setCursorPosition方法中,获取当前时间的小时、分钟、秒数,并将它们转换为百分比宽度。

setCursorPosition() {
  // 获取当前时间的小时、分钟、秒数
  let currentHour = new Date().getHours()
  let currentMinute = new Date().getMinutes()
  let currentSecond = new Date().getSeconds()
  // 将时间转换为百分比宽度
  let currentWidth =
    (currentHour - 1) * 100 + currentMinute * 1.67 + currentSecond * 0.03
  // 更新当前时间标记的宽度
  this.$refs.cursor.style.width = `${currentWidth}px`
}

2.4 调试和优化

完成代码编写后,可以运行npm run serve命令在本地启动项目进行调试。

在调试和优化过程中,可使用Chrome浏览器自带的开发者工具进行调试。

3. 示例说明

3.1 示例1

例如,在一个在线课堂的页面中,使用TimeAxis组件展示课堂开始后8个小时的时间轴,并根据当前时间实时定位。

<template>
  <div class="classroom">
    <div class="classroom-header">
      <h2>{{ courseName }}</h2>
      <TimeAxis />
    </div>
  </div>
</template>

<script>
import TimeAxis from '@/components/TimeAxis.vue'

export default {
  name: 'Classroom',
  components: {
    TimeAxis
  },
  data() {
    return {
      courseName: 'JavaScript入门课程',
    }
  },
}
</script>

<style>
.classroom {
  width: 100%;
  height: 100%;
}

.classroom-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  height: 80px;
  background-color: #f0f0f0;
}
</style>

3.2 示例2

例如,在一个工作计划的Web应用中,使用TimeAxis组件展示工作日8个小时的时间轴,并根据当前时间实时定位。

<template>
  <div class="workplan">
    <h1>今天的工作计划</h1>
    <TimeAxis />
    <div class="workplan-tasks">
      <div class="workplan-task" v-for="(task, index) in tasks" :key="index">
        <input type="checkbox"
               :id="'task-' + index"
               :checked="task.finished"
               @change="toggleFinishTask(index)"
        >
        <label :for="'task-' + index">
          {{ task.title }}
          <span v-if="task.deadlineAt" class="workplan-task-deadline">{{ task.deadlineAt | formatDate }}</span>
        </label>
      </div>
    </div>
  </div>
</template>

<script>
import TimeAxis from '@/components/TimeAxis.vue'

export default {
  name: 'Workplan',
  components: {
    TimeAxis
  },
  data() {
    return {
      tasks: [
        {
          title: '开会',
          deadlineAt: '2021-08-26 14:00:00',
          finished: false
        },
        {
          title: '写周报',
          deadlineAt: '2021-08-26 17:00:00',
          finished: false
        },
        {
          title: '健身',
          deadlineAt: '',
          finished: false
        }
      ]
    }
  },
  methods: {
    toggleFinishTask(index) {
      this.tasks[index].finished = !this.tasks[index].finished
    }
  },
  filters: {
    formatDate(dateStr) {
      const date = new Date(dateStr)
      return date.getHours() + ':' + date.getMinutes()
    }
  }
}
</script>

<style>
.workplan {
  max-width: 1000px;
  margin: 0 auto;
  padding: 30px 0;
}

.workplan-tasks {
  margin-top: 20px;
}

.workplan-task {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 16px;
  line-height: 1.5;
  color: #333;
  margin-bottom: 10px;
}

.workplan-task input[type=checkbox] {
  margin-right: 10px;
  transform: translateY(3px);
}

.workplan-task label {
  position: relative;
}

.workplan-task-deadline {
  position: absolute;
  right: 0;
  font-size: 12px;
  color: #999;
}
</style>

以上就是使用Vue实现8小时带刻度的时间轴根据当前时间实时定位功能的完整攻略,希望对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于vue实现8小时带刻度的时间轴根据当前时间实时定位功能 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • Vue冷门技巧递归组件实践示例详解

    让我来详细讲解一下“Vue冷门技巧递归组件实践示例详解”的完整攻略。 什么是递归组件? 递归指的是一个函数在执行的过程中调用了自身。同样的,在Vue中,递归组件指的是一个组件在自身内部使用自身。这种组件的实现方式通常是通过组件的name选项和components来定义自身。 如何实现递归组件? 在Vue中,想要实现递归组件,需要在组件的选项中设置组件的nam…

    Vue 2023年5月29日
    00
  • React中的权限组件设计问题小结

    我将详细讲解“React中的权限组件设计问题小结”的完整攻略。首先,我们需要明确权限组件的概念和作用,权限组件用于控制用户在系统中的访问权限,保障系统的安全性和稳定性。在React中,我们可以使用高阶组件(HOC)来实现权限控制。 一、HOC高阶组件思路 1.定义一个高阶组件 我们首先需要定义一个高阶组件,用于封装特定的组件并添加权限控制逻辑。以下是一个基本…

    Vue 2023年5月28日
    00
  • Vue响应式原理与虚拟DOM实现步骤详细讲解

    Vue响应式原理与虚拟DOM实现步骤详细讲解 1. Vue响应式原理 Vue的响应式原理核心是利用Object.defineProperty方法对数据进行拦截,当数据发生变化时,通知对应的界面进行更新。 1.1 监听对象 在Vue中对数据的监听由Observer对象负责,在Observer对象中使用Object.defineProperty方法对数据进行监听…

    Vue 2023年5月28日
    00
  • Vue.sync修饰符与$emit(update:xxx)详解

    让我来给你详细讲解一下Vue.sync修饰符与$emit(update:xxx)的使用方法。 Vue.sync修饰符 Vue.sync修饰符是Vue.js 2.3.0版本中添加的一个修饰符,它主要用于简化父子组件之间的双向数据绑定。在Vue 2.3.0版本中,你可以使用Vue.sync修饰符来实现子组件对父组件数据的更新。 <template> …

    Vue 2023年5月28日
    00
  • vue-cli+webpack记事本项目创建

    下面是“vue-cli+webpack记事本项目创建”的完整攻略: 一、前置条件 在开始构建项目之前,请确保已经安装以下软件: Node.js: 版本>=8,用于运行vue-cli。 npm: Node.js自带的包管理工具。 二、创建vue-cli项目 打开命令行工具,输入以下命令安装vue-cli: npm install -g vue-cli 使…

    Vue 2023年5月29日
    00
  • 微信小程序实现表单验证提交功能

    讲解“微信小程序实现表单验证提交功能”的完整攻略,具体步骤如下: 1. 编写表单 首先需要在小程序页面中编写表单,包括输入框、下拉框等等常见表单元素,并且给每一个表单元素设置一个唯一的id值,以便后面表单验证时获取元素。代码示例: <form> <input id="name" placeholder="请输入…

    Vue 2023年5月27日
    00
  • vue select组件绑定的值为数字类型遇到的问题

    问题描述: 在使用 Vue.js 中的 select 组件时,如果绑定的值是数字类型,会遇到一些问题,例如选择一个选项后,绑定的值不是选项本身的值,而会是选项的索引值。 解决方法: 使用 v-model.number 来强制将输入值转为数字类型。 在 select 标签上使用 v-model.number,可以让 select 组件将选中的值强制转为数字类型…

    Vue 2023年5月29日
    00
  • vue2.0+vue-router构建一个简单的列表页的示例代码

    下面是“vue2.0+vue-router构建一个简单的列表页”的完整攻略: 步骤一:创建项目 首先,我们需要创建一个空项目,可以通过以下命令来创建: vue create my-project 创建完成后,进入项目的根目录,安装 vue-router 依赖: npm install vue-router –save 步骤二:配置路由 在 src 目录下创…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部