原创

序列图片实现视频播放效果实例代码演示


移动端对video标签支持不好

  1. CSS代码:
  2. .container {
  3. width: 256px; height: 464px;
  4. margin: auto;
  5. background-color: #000;
  6. position: relative;
  7. }
  8. .container > img {
  9. position: absolute;
  10. width: 100%; height: 100%;
  11. }
  12. .loading {
  13. position: absolute;
  14. height: 8px; width: 150px;
  15. border: 1px solid #eee;
  16. background: linear-gradient(to top, #eee, #eee);
  17. background-size: 0 100%;
  18. transition: background-size .1s;
  19. left: 0; top: 0; right: 0; bottom: 0;
  20. margin: auto;
  21. }
  22. .loading::before {
  23. content: attr(data-percent)'%';
  24. position: absolute;
  25. left: 0; top: -1.5em;
  26. font-size: 12px;
  27. color: #eee;
  28. }
  29. HTML代码:
  30. <div id="container" class="container">
  31. <span id="loading" class="loading" data-percent="0"></span>
  32. </div>
  33. JS代码:
  34. var urlRoot = './thumbs/';
  35. var indexRange = [1, 47];
  36. var maxLength = indexRange[1] - indexRange[0] + 1;
  37. // loading
  38. var eleContainer = document.getElementById('container');
  39. var eleLoading = document.getElementById('loading');
  40. // 存储预加载的DOM对象和长度信息
  41. var store = {
  42. length: 0
  43. };
  44. // 图片序列预加载
  45. for ( var start = indexRange[0]; start <= indexRange[1]; start++) {
  46. (function (index) {
  47. var img = new Image();
  48. img.onload = function () {
  49. store.length++;
  50. // 存储预加载的图片对象
  51. store[index] = this;
  52. play();
  53. };
  54. img.onerror = function () {
  55. store.length++;
  56. play();
  57. };
  58. img.src = urlRoot + index + '.jpg';
  59. })(start);
  60. }
  61. var play = function () {
  62. // loading进度
  63. var percent = Math.round(100 * store.length / maxLength);
  64. eleLoading.setAttribute('data-percent', percent);
  65. eleLoading.style.backgroundSize = percent + '% 100%';
  66. // 全部加载完毕,无论成功还是失败
  67. if (percent == 100) {
  68. var index = indexRange[0];
  69. eleContainer.innerHTML = '';
  70. // 依次append图片对象
  71. var step = function () {
  72. if (store[index - 1]) {
  73. eleContainer.removeChild(store[index - 1]);
  74. }
  75. eleContainer.appendChild(store[index]);
  76. // 序列增加
  77. index++;
  78. // 如果超过最大限制
  79. if (index <= indexRange[1]) {
  80. setTimeout(step, 42);
  81. } else {
  82. // 本段播放结束回调
  83. // 我这里就放一个重新播放的按钮
  84. eleContainer.insertAdjacentHTML('beforeEnd', '<button onclick="play()">再看一遍英姿</button>');
  85. }
  86. };
  87. // 等100%动画结束后执行播放
  88. setTimeout(step, 100);
  89. }
  90. };
留言反馈
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈