前提・実現したいこと
WordPressで、動画検索サイトを作成しています。
一覧にあるサムネイル画像をクリックするとポップアップで動画が開いて自動再生されるように実装したいのです。
下記のテーマとプラグインを使用しています。
テーマ:Underscores(https://underscores.me/)
プラグイン:colorbox(http://www.jacklmoore.com/colorbox/)
ちなみに、動画再生はポップアップでなくても、画面サイズいっぱいに表示できればいいのですが、「閉じるボタン」をクリックして閉じるような仕様にしたいので、ポップアップで画面サイズいっぱいに表示するのがいいかと思ってそうしています。
困っていること
このサイト(↓)を参考に、まずはクリックするとポップアップで動画再生する機能を作成してみました。
▼colorboxを使用してポップアップしたあとに動画を自動再生したい
http://creatornote.nakweb.com/popup_autoplay/
ですが、動かずに困っています。
具体的にいうと、
「クリックすると動画が表示されるよ」という文字をクリックしても、ポップアップも開きませんし、何も起きません。
唯一、ブラウザのURLが
「http://localhost/」→「http://localhost/#movie_test」に変化します。
該当のソースコード
html
1<section class="movie_wrap"> 2 <div class="mv_map"> 3 <a class="inline" href="#movie_test"> 4 <p>クリックすると動画が表示されるよ</p> 5 </a> 6 </div> 7 </section> 8 <div style="display: none;"> 9 <div id="movie_test" class="movie_list_wrap"> 10 11 <video preload="auto" controls id="video"> 12 <source src="http://creatornote.nakweb.com/wp-content/themes/giraffe/images/test.mp4" type="video/mp4" class="mv01"> 13 </video> 14 </div> 15</div>
CSS
1/* 2 Colorbox Core Style: 3 The following CSS is consistent between example themes and should not be altered. 4*/ 5#colorbox, #cboxOverlay, #cboxWrapper{position:absolute; top:0; left:0; z-index:9999; overflow:hidden; -webkit-transform: translate3d(0,0,0);} 6#cboxWrapper {max-width:none;} 7#cboxOverlay{position:fixed; width:100%; height:100%;} 8#cboxMiddleLeft, #cboxBottomLeft{clear:left;} 9#cboxContent{position:relative;} 10#cboxLoadedContent{overflow:auto; -webkit-overflow-scrolling: touch;} 11#cboxTitle{margin:0;} 12#cboxLoadingOverlay, #cboxLoadingGraphic{position:absolute; top:0; left:0; width:100%; height:100%;} 13#cboxPrevious, #cboxNext, #cboxClose, #cboxSlideshow{cursor:pointer;} 14.cboxPhoto{float:left; margin:auto; border:0; display:block; max-width:none; -ms-interpolation-mode:bicubic;} 15.cboxIframe{width:100%; height:100%; display:block; border:0; padding:0; margin:0;} 16#colorbox, #cboxContent, #cboxLoadedContent{box-sizing:content-box; -moz-box-sizing:content-box; -webkit-box-sizing:content-box;} 17 18/* 19 User Style: 20 Change the following styles to modify the appearance of Colorbox. They are 21 ordered & tabbed in a way that represents the nesting of the generated HTML. 22*/ 23#cboxOverlay{background:#000; opacity: 0.9; filter: alpha(opacity = 90);} 24#colorbox{outline:0;} 25 #cboxContent{margin-top:20px;background:#000;} 26 .cboxIframe{background:#fff;} 27 #cboxError{padding:50px; border:1px solid #ccc;} 28 #cboxLoadedContent{border:5px solid #000; background:#fff;} 29 #cboxTitle{position:absolute; top:-20px; left:0; color:#ccc;} 30 #cboxCurrent{position:absolute; top:-20px; right:0px; color:#ccc;} 31 #cboxLoadingGraphic{background:url(images/loading.gif) no-repeat center center;} 32 33 /* these elements are buttons, and may need to have additional styles reset to avoid unwanted base styles */ 34 #cboxPrevious, #cboxNext, #cboxSlideshow, #cboxClose {border:0; padding:0; margin:0; overflow:visible; width:auto; background:none; } 35 36 /* avoid outlines on :active (mouseclick), but preserve outlines on :focus (tabbed navigating) */ 37 #cboxPrevious:active, #cboxNext:active, #cboxSlideshow:active, #cboxClose:active {outline:0;} 38 39 #cboxSlideshow{position:absolute; top:-20px; right:90px; color:#fff;} 40 #cboxPrevious{position:absolute; top:50%; left:5px; margin-top:-32px; background:url(images/controls.png) no-repeat top left; width:28px; height:65px; text-indent:-9999px;} 41 #cboxPrevious:hover{background-position:bottom left;} 42 #cboxNext{position:absolute; top:50%; right:5px; margin-top:-32px; background:url(images/controls.png) no-repeat top right; width:28px; height:65px; text-indent:-9999px;} 43 #cboxNext:hover{background-position:bottom right;} 44 #cboxClose{position:absolute; top:5px; right:5px; display:block; background:url(images/controls.png) no-repeat top center; width:38px; height:19px; text-indent:-9999px;} 45 #cboxClose:hover{background-position:bottom center;} 46
JQuery
1jQuery(document).ready(function($){ 2 $('.inline').on('click', function(){ 3 $(".inline").colorbox({ 4 inline: true, 5 rel:'group', 6 closeButton:'true', 7 onComplete: function(){ 8 var video = $("#video").get(0).play(); 9 } 10 }); 11 }); 12 });
また、ダウンロードしたCSSとJSを読み込むため、「function.php」に下記のコードを書いています。
// CSS呼び出し
function twpp_enqueue_styles() {
wp_enqueue_style( 'colorbox-style', 'http://localhost/wp-content/themes/underscores/colorbox.css' );
}
add_action( 'wp_enqueue_scripts', 'twpp_enqueue_styles' );
// Javascript呼び出し
if (!is_admin()) {
function register_script(){
wp_register_script( 'jqery', get_template_directory_uri() .'/js/jqery.js');
wp_register_script( 'colorbox', get_template_directory_uri() .'/js/jquery.colorbox-min.js');
}
function add_script() {
register_script();
wp_enqueue_script('jqery');
wp_enqueue_script('colorbox');
}
add_action('wp_enqueue_scripts', 'add_script');
}
試したこと
表示した画面で「ページのソースを表示」をして、CSSとJSが読み込めていることは確認済みです。
コードに間違いがあるのかと思い、何回も見直したりしていますが、わかりません。。
どなたかご教授お願いいたします。
試したこと(追記)
投稿の中にvideoタグを挿入して動画が再生されるかを試したところ、こちらは問題なく動画再生されました。
<video controls autoplay src="http://creatornote.nakweb.com/wp-content/themes/giraffe/images/test.mp4"/></video>
あなたの回答
tips
プレビュー