当前位置: 首页> 教育> 锐评 > 苏州高端网站制作_聚名网是干什么的_东营网站建设费用_51趣优化网络seo工程师教程

苏州高端网站制作_聚名网是干什么的_东营网站建设费用_51趣优化网络seo工程师教程

时间:2025/7/13 19:52:49来源:https://blog.csdn.net/weixin_42736657/article/details/145720871 浏览次数:0次
苏州高端网站制作_聚名网是干什么的_东营网站建设费用_51趣优化网络seo工程师教程

在 JavaScript 里,你可以采用多种方式把 this.currentTime 转换为整数。在 Vue 组件的上下文中,this.currentTime 通常是一个浮点数,代表视频播放的当前时间(以秒为单位),如果你想将其转换为整数,可以按照以下几种常见的方法操作:

1. 使用 Math.floor() 方法

Math.floor() 方法会返回小于或等于一个给定数字的最大整数,也就是向下取整。

<template><div><video ref="videoRef" controls @pause="handlePause" @ended="handleEnded"><source src="your-video-url.mp4" type="video/mp4">Your browser does not support the video tag.</video><p>当前播放进度(整数): {{ currentTimeInt }} 秒</p></div>
</template><script>
export default {data() {return {currentTime: 0,currentTimeInt: 0,intervalId: null};},mounted() {this.intervalId = setInterval(() => {const video = this.$refs.videoRef;if (video) {this.currentTime = video.currentTime;// 使用 Math.floor() 向下取整this.currentTimeInt = Math.floor(this.currentTime); }}, 100);},beforeDestroy() {clearInterval(this.intervalId);},methods: {handlePause() {console.log(`暂停时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);},handleEnded() {console.log(`播放完毕时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);}}
};
</script><style scoped>
video {width: 100%;
}
</style>

2. 使用 Math.round() 方法

Math.round() 方法会返回一个数字四舍五入后最接近的整数。

<template><div><video ref="videoRef" controls @pause="handlePause" @ended="handleEnded"><source src="your-video-url.mp4" type="video/mp4">Your browser does not support the video tag.</video><p>当前播放进度(整数): {{ currentTimeInt }} 秒</p></div>
</template><script>
export default {data() {return {currentTime: 0,currentTimeInt: 0,intervalId: null};},mounted() {this.intervalId = setInterval(() => {const video = this.$refs.videoRef;if (video) {this.currentTime = video.currentTime;// 使用 Math.round() 四舍五入this.currentTimeInt = Math.round(this.currentTime); }}, 100);},beforeDestroy() {clearInterval(this.intervalId);},methods: {handlePause() {console.log(`暂停时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);},handleEnded() {console.log(`播放完毕时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);}}
};
</script><style scoped>
video {width: 100%;
}
</style>

3. 使用 parseInt() 函数

parseInt() 函数会解析一个字符串参数,并返回一个指定基数的整数。如果传入的是数字,它会直接转换为整数,并且会忽略小数部分(相当于向下取整)。

<template><div><video ref="videoRef" controls @pause="handlePause" @ended="handleEnded"><source src="your-video-url.mp4" type="video/mp4">Your browser does not support the video tag.</video><p>当前播放进度(整数): {{ currentTimeInt }} 秒</p></div>
</template><script>
export default {data() {return {currentTime: 0,currentTimeInt: 0,intervalId: null};},mounted() {this.intervalId = setInterval(() => {const video = this.$refs.videoRef;if (video) {this.currentTime = video.currentTime;// 使用 parseInt() 转换为整数this.currentTimeInt = parseInt(this.currentTime, 10); }}, 100);},beforeDestroy() {clearInterval(this.intervalId);},methods: {handlePause() {console.log(`暂停时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);},handleEnded() {console.log(`播放完毕时的播放进度(整数): ${this.currentTimeInt} 秒`);localStorage.setItem('videoProgress', this.currentTimeInt);}}
};
</script><style scoped>
video {width: 100%;
}
</style>

你可以根据具体需求选择合适的方法来进行转换。如果希望直接舍去小数部分,可使用 Math.floor()parseInt();如果需要四舍五入,就使用 Math.round()

关键字:苏州高端网站制作_聚名网是干什么的_东营网站建设费用_51趣优化网络seo工程师教程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: