我知道我的方法有點像jQuery和JavaScript,但對於優化,我有點虛弱,但願意學習新東西。Vimeo API - 加載視頻
我當前正在從Vimeo加載視頻到我們的網站上,當用戶單擊適當的圖像區域時。這工作正常,但我覺得這不是一個完整的表現方式去做。
有沒有人看到我寫下面的代碼有哪些問題可以做得更好?
JS
var videoData = [
{
'player_id':'video1',
'videoURL':'<?php the_field('two_vimeo_video_url'); ?>',
'width':'1000',
},
{
'player_id':'video2',
'videoURL':'<?php the_field('six_vimeo_video_url'); ?>',
'width':'1000',
}];
function loadVideo(target, videoid) {
$.getJSON('http://www.vimeo.com/api/oembed.json?url=' + encodeURIComponent(videoData[videoid]['videoURL']) + '&api=1&player_id='+ videoData[videoid]['player_id'] +'&width='+videoData[videoid]['width']+'&byline=0&color=ffffff&title=0'+'&callback=?', function(data){
$(target).find('.video-container').html(data.html); // puts an iframe embed from vimeo's json
$(target).closest('iframe').load(function(){
$f(player).addEvent('ready', function(id){
var vimeoVideo = $f(videoid);
});
});
});
}
$(function(){
// Create array to store values
var vimeoArray = [];
var videoContainer = $('.video-container');
// loop through st
$(videoContainer).each(function(index, value) {
// get the image with the data attr on each loop
var dataAttr = $(this).attr('data-video');
// if dataAttr is not false
if (dataAttr !== undefined) {
// push data attribute value into array
vimeoArray.push(dataAttr);
// Store last element of array on iteration
var videoid = vimeoArray[vimeoArray.length-1];
// attach click handler to the parent of the scoped element
$(this).parent().click(function(){
// load the video
loadVideo(this, videoid);
$(this).unbind('click');
$(this).find('.b-studio__image').hide();
});
}
});
});
我創建了一個jsfiddle以及所有的代碼和一些虛擬數據,任何建議將是真正的幫助:)
StackExchange具有良好的代碼改進請求的網站。它的所謂的代碼審查:http://codereview.stackexchange.com/這種類型的問題將在那裏得到更多的播放。 – 2014-12-05 15:21:00