图片预加载插件开发小记

深碍√TFBOYSˉ_ 2022-06-10 10:05 280阅读 0赞

面向对象组件开发前端进阶不可或缺的一步,复用思想也是前端模块化的重要推动力

图片预加载在大批量图片加载的同时保证用户体验方面作用明显,浅显尝试,以此为记

插件封装(preLoad.js)

  1. (function($) {
  2. function PreLoad(imgs, options) {
  3. this.imgs = imgs;
  4. this.opts = $.extend({}, PreLoad.defaults, options);
  5. this._loadnow();
  6. this._addcli(imgs);
  7. }
  8. PreLoad.defaults = {
  9. each: null,
  10. all: null
  11. }
  12. PreLoad.prototype._loadnow = function() {
  13. var imgs = this.imgs;
  14. var opts = this.opts;
  15. var count = 0;
  16. var l = imgs.length;
  17. $.each(imgs, function(i, src) {
  18. var imgObj = new Image();
  19. $(imgObj).on("load error", function() {
  20. ++count;
  21. opts.each(count);
  22. if(count >= l) {
  23. opts.all();
  24. }
  25. });
  26. imgObj.src = src;
  27. })
  28. }
  29. PreLoad.prototype._addcli = function(imgs) {
  30. var l = imgs.length;
  31. var index = 0;
  32. $("#prev").click(function() {
  33. if(index < 1) return;
  34. $(".box>img").attr('src', imgs[--index]);
  35. })
  36. $("#next").click(function() {
  37. if(index >= l - 1) return;
  38. $(".box>img").attr('src', imgs[++index]);
  39. })
  40. }
  41. $.extend({
  42. preload: function(imgs, options) {
  43. new PreLoad(imgs, options);
  44. }
  45. });
  46. })(jQuery)

插件使用

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. html,
  8. body {
  9. width: 100%;
  10. height: 100%;
  11. }
  12. .box {
  13. width: 1200px;
  14. margin: 0 auto;
  15. }
  16. .btn {
  17. width: 800px;
  18. margin: 0 auto;
  19. }
  20. .btn>span {
  21. width: 100px;
  22. height: 30px;
  23. line-height: 30px;
  24. text-align: center;
  25. background: #B3B3B3;
  26. color: white;
  27. cursor: pointer;
  28. }
  29. #prev {
  30. float: left;
  31. }
  32. #next {
  33. float: right;
  34. }
  35. .persent {
  36. position: fixed;
  37. width: 100%;
  38. height: 100%;
  39. left: 0;
  40. top: 0;
  41. background-color: #B3B3B3;
  42. }
  43. .persent>p {
  44. width: 30px;
  45. height: 30px;
  46. margin: 0 auto;
  47. color: #fff;
  48. font-size: 40px;
  49. margin-top: 400px;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="box">
  55. <img src="http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/08/0B/ChMkJlmILU-IGd5HABRM9L5MfHUAAfd_AJ9MToAFE0M019.jpg" width="1200px" />
  56. </div>
  57. <div class="btn">
  58. <span id="prev">上一页</span>
  59. <span id="next">下一页</span>
  60. </div>
  61. <div class="persent">
  62. <p><span id="num">0</span>%</p>
  63. </div>
  64. <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
  65. <script src="js/preLoad.js" type="text/javascript" charset="utf-8"></script>
  66. <script type="text/javascript">
  67. var imgs = [
  68. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/08/0B/ChMkJlmILU-IGd5HABRM9L5MfHUAAfd_AJ9MToAFE0M019.jpg',
  69. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/08/0B/ChMkJlmILU-IUKs0ABXhWXXbbckAAfd_AKRfkYAFeFx786.jpg',
  70. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/0B/0B/ChMkJ1iUT3SIG2zFANNdJFXu3zUAAZrPwMMhC4A0108912.jpg',
  71. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/00/06/ChMkJlgz_AGIcofsAAPk3-SsuUIAAX-ewKWFgIAA-T3540.jpg',
  72. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/09/0D/ChMkJ1e2_LCITKqJAAoJpzIbrR4AAUkhwH3LZEACgm_374.jpg',
  73. 'http://desk.fd.zol-img.com.cn/t_s960x600c5/g5/M00/0B/07/ChMkJlbQIJGIcSlhABDJ3oQM3lIAALspQEYV2wAEMn2807.jpg',
  74. ];
  75. $.preload(imgs, {
  76. each: function(count) {
  77. $("#num").text(Math.round((count) / imgs.length * 100));
  78. // alert("nihao");
  79. },
  80. all: function() {
  81. $(".persent").hide();
  82. }
  83. })
  84. </script>
  85. </body>
  86. </html>

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读

    相关 案例 - 图片

    0- 零 - 预加载 1.什么是预加载 资源预加载是另一个性能优化技术,我们可以使用该技术来预先告知浏览器某些资源可能在将来会被使用到。预加载简单来说就是将所有所需的

    相关 图片

    预加载图片 1. 在$(function())中,该封装函数是保证页面完成后进行执行js,该函数在ready时执行,在load后执行 2. 需要在$(function(

    相关 图片开发小记

    面向对象组件开发前端进阶不可或缺的一步,复用思想也是前端模块化的重要推动力 图片预加载在大批量图片加载的同时保证用户体验方面作用明显,浅显尝试,以此为记 插件封装(preL