{"id":17181515,"url":"https://github.com/dimitarchristoff/pre-loader","last_synced_at":"2025-04-13T16:21:09.654Z","repository":{"id":9144445,"uuid":"10936826","full_name":"DimitarChristoff/pre-loader","owner":"DimitarChristoff","description":"pre-load images from arrays or DOM lazy-loading.","archived":false,"fork":false,"pushed_at":"2017-01-04T15:54:37.000Z","size":357,"stargazers_count":79,"open_issues_count":1,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-23T03:32:43.174Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DimitarChristoff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-25T11:48:50.000Z","updated_at":"2024-11-27T10:59:15.000Z","dependencies_parsed_at":"2022-08-30T11:10:11.111Z","dependency_job_id":null,"html_url":"https://github.com/DimitarChristoff/pre-loader","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitarChristoff%2Fpre-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitarChristoff%2Fpre-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitarChristoff%2Fpre-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitarChristoff%2Fpre-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DimitarChristoff","download_url":"https://codeload.github.com/DimitarChristoff/pre-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741840,"owners_count":21154385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T00:34:33.118Z","updated_at":"2025-04-13T16:21:09.622Z","avatar_url":"https://github.com/DimitarChristoff.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"pre-loader\n==========\n\nEvent-driven sequential image preloading and lazyloading in vanilla js (no dependencies). Supports IE8 or higher, though the main lib should work even in IE6.\n\n## Installing\n\nEither [download](https://github.com/DimitarChristoff/pre-loader/releases) it\n\n...or, clone the repo:\n```sh\n$ git clone https://github.com/DimitarChristoff/pre-loader.git\n...\n$ cd pre-loader\n```\n\n...via npm:\n```sh\n$ npm i pre-loader\n```\n\n...or use bower:\n\n```sh\n$ bower install pre-loader --save\n```\n\n## Basic use via JavaScript\n\nLoad an array, get a functional callback when done:\n\n```javascript\n// under a normal browser\nnew preLoader(/*Array*/images, /*Object*/options/);\n\n// AMD with requireJS\nrequire(['../js/pre-loader'], function(preLoader){\n\tnew preLoader(/*Array*/images, /*Object*/options/);\n});\n\n// under CJS / webpack\nimport preLoader from 'pre-loader';\n// or just DOM lazy load\nimport { lazyLoad } from 'pre-loader';\n```\n\nTo have a play, you can clone the repo and:\n\n```sh\n$ npm install\n$ npm run start\n```\n\nOpen a browser and point to [http://localhost:8888](http://localhost:8888) - this runs a Webpack dev server and serves from the examples folder.\n\n_NOTE: if RequireJS or any other AMD-compatible loader is available, pre-loader will not go to the global object but define\nitself as an AMD module that can be required._\n\n\n## Options\n\nThe `options` object you can pass to the constructor supports 5 different keys.\n\n```\n{\n\t// use a single pipeline, default: false\n\tpipeline: false,\n\t// auto start loading when instantiated\n\tauto: true,\n\t// optional function to call on each image\n\tonProgress: function(src, imageEl, index){},\n\t// optional function to call when all done\n\tonComplete: function(loaded, errors){},\n\t// optional function to call when an error happens\n\tonError: function(src){}\n};\n```\n\n## Example\n\nThe most basic example to prime the cache with 2 images:\n\n```javascript\nnew preLoader(['image1.jpg', 'http://domain.com/image2.jpg'], {\n\tonComplete: function(loaded, errors){\n\t\tconsole.log('cache primed with:', loaded);\n\t\terrors \u0026\u0026 errors.length \u0026\u0026 console.log('these failed:', errors);\n\t}\n});\n```\n\n## Progress reporting\n\nLoading an array of images, firing a callback with each one and in the end:\n\n```javascript\nnew preLoader(['image1.jpg', 'http://domain.com/image2.jpg'], {\n\tonProgress: function(src, element, index){\n\t\tif (element){\n\t\t\tconsole.log|('loaded ' + src);\n\t\t\t// gets optional reference to element you can use:\n\t\t\t// document.appendChild(element);\n\t\t}\n\t\telse {\n\t\t\tconsole.log('failed ' + src);\n\t\t}\n\n\t\t// output some stats\n\t\tvar donePercent = Math.floor((100 / this.queue.length) * this.completed.length);\n\t\tconsole.log(donePercent + '% completed', this.completed.length + this.errors.length + ' / ' + this.queue.length + ' done');\n\t},\n\tonComplete: function(loaded, errors){\n\t\tconsole.log('cache primed with:', loaded);\n\t\terrors \u0026\u0026 errors.length \u0026\u0026 console.log('these failed:', errors);\n\t}\n});\n```\n\n## Pipeline vs parallel\n\nOptionally, you can control how the loading takes place. You can either let the browser resolve the resource handling or you could stick to using a single thread with pipelining (each image that completes calls the next one in the stack).\n\n```javascript\nnew preLoader(['image1.jpg', 'http://domain.com/image2.jpg'], {\n\t// enable a single pipeline\n\tpipeline: true,\n\tonComplete: function(list, errors){\n\t\t// this will take longer but won't waste resources.\n\t}\n});\n```\n\nWaterfall with pipeline enabled:\n![pipeline](example/images/pipeline-waterfall.jpg)\n\nWaterfall with parallel downloading enabled:\n![parallel](example/images/parallel-waterfall.jpg)\n\n\n\nFor more info, see the `example` folder or look at [http://jsfiddle.net/dimitar/mFQm6/](http://jsfiddle.net/dimitar/mFQm6/)\n\n## Lazy loading of images\n\nLazy loading works based upon existing DOM img elements with a `data-preload` attribute, pointing to the image to load.\n\neg.\n\n```html\n\u003cimg src=\"loading.gif\" data-preload=\"images/real-image.jpg\" /\u003e\n\u003cscript\u003e\n\t// go with defaults, we're good.\n\tvar instance = preLoader.lazyLoad();\n\t// or\n\tvar instance = preLoader.lazyLoad('#portfolio img.preload', { onComplete: function(){ });\n\u003c/script\u003e\n```\n\nThe `lazyLoad` method accepts arguments to the normal `preLoader` constructor and additionally, a `selector` property\nthat defaults to `img[data-preload]`. It will return an instance and all the callbacks will fire as expected. When\nan image matches the DOM element, it will set the `src` property and remove the `data-preload` attribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimitarchristoff%2Fpre-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimitarchristoff%2Fpre-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimitarchristoff%2Fpre-loader/lists"}