如何使用 javascript 在嵌入的 YouTube 视频上创建可点击的时间戳 link?

How to create a clickable timestamp link on an embedded youtube video with javascript?

我真的很想在我的网站上有可点击的时间链接,这些链接将跳转到我嵌入的 YouTube 视频中的特定时间。我知道这在 youtube 本身是可能的(youtube 上的内置功能,你只需将你想要的时间放在格式 (0:00) 中,用户可以点击它跳转到那个点)。

我想知道是否可以通过嵌入的 youtube 视频和 javascript 以某种方式模仿此功能?

您可以使用Youtube iFrame API。有一个获取当前时间戳的函数:

player.getCurrentTime():Number 

Returns the elapsed time in seconds since the video started playing.

player.getDuration():Number

Returns the duration in seconds of the currently playing video. Note that getDuration() will return 0 until the video's metadata is loaded, which normally happens just after the video starts playing.

像这样使用它们:

ytplayer = document.getElementById("youtube_player");
ytplayer.getCurrentTime();