正直なところ、1 ページに 50 個の動画が埋め込まれていることは、目に見えるかどうかに関係なく、あまり良くないと思います。表示されていなくてもブラウザによって処理されるためです。(私が間違っている場合はお気軽に修正してください。ただし、ブラウザーは DOM 全体を表示して処理し、「隠し」ビットにスタイルを適用するだけです。)
Gustavo Straube は、ブラウザーと帯域幅の両方に影響を与える可能性があるにもかかわらず、DOM に 50 個の要素を配置したい場合に、これを行う方法について非常に良い答えを出しています。
おそらく、XML を解析し、すべてのデータを JSON として保存し、Mustache.js などのテンプレート フレームワークで提供される HTML から jQuery を使用して DOM を動的に更新するという行に沿って、さらに何かを行うでしょう。
/* Generate JSON */
$url = "http://www.theURLofmyXML.blah";
$xml = simplexml_load_file($url);
$i = 0;
$json = array();
while ($i < 49) {
$arr['title'] = (string) $xml->query->results->item[$i]->title;
$arr['videoid'] = (string) $xml->query->results->item[$i]->id;
$arr['explanation'] = (string) $xml->query->results->item[$i]->explanation;
$json[] = $arr;
}
echo json_encode($json);
次に、マークアップに以下のようなものを用意して、最初の x ビデオを初期化します - この例では 10..
$(document).ready(function(){
var template = '{{$title}}<br /><iframe width="400" height="225"'
+ 'src="http://www.youtube.com/embed/{{$videoid}}?rel=0&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe><br/>'
+ '{{explanation}}<br /><br />';
var html = '';
var i=0;
for(; i<10; i++){
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html); //where #videos is a div to contain your videos
次に、次の 10 個のビデオを取得するためのアンカー (この例では #next) を用意します。
$('#next').click(function(){
/* template, i and json are still in scope! */
var j = i+10;
for(; i<j; i++){
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html); //where #videos is a div to contain your videos
});
これの利点は、以前のアンカーも簡単に実行できることです...
$('#prev').click(function(){
/* template, i and json are still in scope! */
var j = i -10;
i -= 20; //10 for the current page, 10 for the start of the previous page
for(; i<j; i++){ //rebuild div content of previous page
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html);
});
繰り返しになりますが、これは別の解決策です。JSON を使用すると、XML よりも軽量で柔軟性があり、使用されていない 50 個の DOM 要素を使用する必要がなくなります。一度。あなたが持っている実装を選択したのには理由があるかもしれませんが、この問題が発生した場合に私が採用する実装ではありません!