单击图像即可启动/播放嵌入式 (iframe) youtube 视频
我正在尝试通过单击图像来开始播放嵌入式 YouTube 视频。这个想法是在视频上方放置一个图像,当单击该图像时,它会淡出并开始播放视频。
我正在使用 jquery 淡化图像,并希望找到一种使用 jquery 播放或单击视频的方法。
淡入淡出效果很好,但我不知道如何触发视频播放。 我通过将视频设置为自动播放并隐藏它,然后在单击图像时淡入视频,让它在几个浏览器上工作。在大多数浏览器上,视频在淡入时会自动播放,但在 Chrome 中,即使隐藏时它也会开始自动播放。它在 iOS 中也运行得不好。
由于我对此很陌生,我什至不确定我写的是否100%正确,但我尝试过类似的方法但没有成功:
$('#IMAGE').click(function() { $('#VIDEO').play(); });
那么,我将如何让视频在图片点击? 有没有办法只使用 jquery 在单击图像时播放视频?
先感谢您。
I'm trying to start playing an embedded youtube video by clicking an image. The idea is to have an image on top of a video, and when the image is clicked it fades out and starts playing the video.
I'm using jquery to fade the image, and was hoping to find a way to play or click the video using jquery as well.
The fading is working fine, but I can't figure out how to trigger the video to play.
I got it to work on a couple of browsers by setting the video to autoplay and hide it, and then fade in the video when the image was clicked. On most browsers the video would autoplay when faded in, but in chrome it started to autoplay even when it was hidden. It didn't work well in iOS either.
Since I'm pretty new at this, I'm not even sure if I'm writing it 100% correct, but I've tried something like this without success:
$('#IMAGE').click(function() { $('#VIDEO').play(); });
So, how would I go about to make the video play on an image click?
Is there a way to play the video when the image is clicked, just using jquery?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
快速而肮脏的方法是简单地将
iframe
替换为使用 jQuery 设置了autoplay=1
的框架。HTML
占位符:
自动播放链接:
JQUERY
调用函数的
onClick
捕获器函数
The quick and dirty way is to simply swap out the
iframe
with one that hasautoplay=1
set using jQuery.THE HTML
Placeholder:
Autoplay link:
THE JQUERY
The
onClick
catcher that calls the functionThe function
您可以像这样简单地执行此操作
,其中 image_id 是您单击的图像 id,some_id 是 div 的 id,其中 iframe 也可以直接使用 iframe id。
You can do this simply like this
where image_id is your image id you are clicking and some_id is id of div in which iframe is also you can use iframe id directly.
启动视频
停止视频
您可能需要将“&autoplay=1”替换为“?autoplay=1”,以防没有其他参数
适用于 FF 和 YouTube 上的 vimeo 和 youtube。铬合金
To start video
To stop video
You may want to replace "&autoplay=1" with "?autoplay=1" incase there are no additional parameters
works for both vimeo and youtube on FF & Chrome
youtube iframe 示例
点击播放 HTML 元素
用于播放视频的 jQuery 代码
感谢 https://codepen.io/马丁沃尔夫/笔/dyLAC
Example youtube iframe
The click to play HTML element
jQuery code to play the Video
Thanks to https://codepen.io/martinwolf/pen/dyLAC
您应该能够指定一个可以安全编写脚本的域。 api文档提到
“作为额外的安全措施,您还应该在 URL 中包含 origin 参数”
http://code.google.com/apis/youtube/iframe_api_reference.html
src="http://www.youtube.com/embed/J---aiyznGQ?enablejsapi=1&origin=mydomain.com"
将是你的 iframe 的 src 。
然而它并没有很好的记录。我现在正在尝试类似的事情。
You are supposed to be able to specify a domain that is safe for scripting. the api document mentions
"As an extra security measure, you should also include the origin parameter to the URL"
http://code.google.com/apis/youtube/iframe_api_reference.html
src="http://www.youtube.com/embed/J---aiyznGQ?enablejsapi=1&origin=mydomain.com"
would be the src of your iframe.
however it is not very well documented. I am trying something similar right now.
Html 代码:-
Jquery 代码:-
就是这样!
Html Code:-
Jquery Code:-
Thats It!
您可以使用这个。
图像将位于视频 div 的顶部。如果您单击图像,则会出现视频并开始播放。
这也适用于桌面设备和响应式设备。在视频 URL 末尾添加 ?autoplay=1 不会在移动设备上自动播放视频。
但这个解决方案适用于所有设备。
注意:在 stackoverflow 中运行代码后,这可能不起作用,但在本地或生产站点上可以正常工作。
You can work with this one.
The image will be on top of the video div. If you click on the image then the video will appear and will start playing.
This works on desktop and also responsive devices also. Adding ?autoplay=1 at the end of the video URL does not auto play the video on the mobile device.
But this solution work on every devices.
Note: This may not work after running the code in stackoverflow but works fine on local or production site.
这应该可以完美运行,只需复制这个 div 代码
This should work perfect just copy this div code