【发布时间】:2017-04-04 01:33:28
【问题描述】:
问题:我试图让 YouTube 视频在用户滚动到它时自动播放,并在用户滚动经过它时停止。
约束:JavaScript Web 开发的新手。
试过:离开https://jsfiddle.net/kmsdev/gsfkL6xL/ 我复制粘贴的一行一行只是为了让它工作。我熟悉 C++ 和 Java,所以我至少可以阅读代码的要点。看来我的代码是正确的,但我的 HTML5 中可能没有正确调用它?
英雄部分:
<section class="video_background">
<div class="video_foreground">
<iframe id="playerA" frameborder="0" title="YouTube video player" type="text/html" src="https://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1"></iframe>
</div>
</section>
JS 部分:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/scroll.js"></script>
JS:
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
我在https://jsfiddle.net/kmsdev/gsfkL6xL/ 上面有这个,但这不重要。
【问题讨论】:
标签: javascript jquery html youtube