javascript对象将内容加载到DOM中

时间:2015-07-21 05:59:39

标签: javascript jquery html ajax

我试图弄清楚我的代码是否会永远加载我的页面,或者它是否通过我的javascript对象不断迭代。我有一个对象有46个条目,其中46个图像,也充当模态触发器,应该在页面加载后自动加载。该页面正在加载一半图像,并且不会加载其余图像。图像的文件大小是最小的,最多97kb。在对象中链接的视频确实高达9mb,但我假设在点击事件被触发之前它们没有被加载。我是对的?我应该使用ajax加载视频内容吗?或者我的javascript代码是罪魁祸首?我尽可能多地学习,所以任何想法/建议都会受到欢迎并非常感激!

live site | fiddle with same code

的JavaScript

var dataCollection = {
  'videoData': [
    {
      'id'      : 0,
      'title'   : 'Badminton',
      'company' : 'Pepto Bismol',
      'gifLink' : 'http://reneinla.com/kris/gifs/BadmintonPeptoBismolCommercial.gif',
      'srcMp4'  : 'http://reneinla.com/kris/videos/BadmintonPeptoBismolCommercial.mp4',
      'srcWebm' : 'http://reneinla.com/kris/videos/BadmintonPeptoBismolCommercial.webm',
      'srcOgv'  : 'http://reneinla.com/kris/videos/BadmintonPeptoBismolCommercial.ogv',
      'poster'  : 'http://reneinla.com/kris/videos/BadmintonPeptoBismolCommercial.jpg'
      },
      { ...
     ]
    };

var videos = $('#video');
var modalContent = $('#video-modal');

var appendVideo = function(videoObj) {
  var poster = videoObj.poster;
  var srcMp4 = videoObj.srcMp4;
  var srcWebm = videoObj.srcWebm;
  var srcOgv = videoObj.srcOgv;
  var video = $('<div class="video col-sm-12 col-md-3"><img src="' + poster + '" data-title="' + videoObj.title + '" data-toggle="modal" data-target="#myModal"/></div>');

  var videoPlayer = $('<video preload="auto" controls><source src="' + srcMp4 + '" type="video/mp4;"><source src="' + srcWebm + '" type="video/webm;"><source src="' + srcOgv + '" type="video/ogv;"></video>');
  videos.append(video);

video.find('img').click(function(e) {
  modalContent.append(videoPlayer);
  // debug
  // alert(videoObj.srcMp4);
  // console.log(videoObj.srcMp4);
});

$('#myModal, .close').on('click', function(){
  modalContent.find('video').remove();
});

$('.myHTMLvideo').click(function() {
    $(this).get(0).paused ? $(this).get(0).play() : $(this).get(0).pause();
  });
};

for (var i = 0; i < dataCollection.videoData.length; i++) {
  var obj = dataCollection.videoData[i];
    appendVideo(obj);
}

HTML

<div class="container row">
  <div id="video"></div>
</div>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <div class="modal-body" id="video-modal"></div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

尝试这个

var videoPlayer = $('<video preload="none" controls><source src="' + srcMp4 + '" type="video/mp4;"><source src="' + srcWebm + '" type="video/webm;"><source src="' + srcOgv + '" type="video/ogv;"></video>');