diff --git a/.gitignore b/.gitignore
index 3d3c9a9..b69c673 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@
[Dd]esktop.ini
# Project
+node_modules/
package-lock.json
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..4c54f93
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,66 @@
+# Changelog
+
+## [1.1.0] - 2024-03-19
+
+### Added
+- Event system implementation
+ - `on` method for adding event listeners
+ - `off` method for removing event listeners
+ - `once` method for one-time event listeners
+ - `emit` method for triggering events
+- IntersectionObserver API integration
+- Support for multiple media types
+ - Images
+ - Videos
+ - Iframes
+ - Audio
+ - Embed
+ - Object
+- State information for loading process
+ - `waiting`: Element not yet visible
+ - `loading`: Element is visible and loading
+ - `loaded`: Element loaded successfully
+ - `error`: Loading error occurred
+
+### Changed
+- Switched from Parcel to Rollup build system
+- Added ES Modules support
+- Improved code organization
+ - Separated utils and config
+ - Better file structure
+- Updated build configuration
+ - UMD format support
+ - Terser plugin for minification
+- Enhanced documentation
+ - Complete README rewrite
+ - Added usage examples
+ - Added API documentation
+ - Added CSS examples
+- Updated package.json
+ - Added dev dependencies
+ - Updated scripts
+ - Added module type
+ - Updated keywords
+ - Updated description
+
+### Removed
+- Old demo HTML file
+- Old lazy.js implementation
+- Old build system
+- Unnecessary dependencies
+
+### Fixed
+- Event listener management
+- Performance optimizations
+- Error handling improvements
+
+### Security
+- Updated license year to 2025
+
+## [1.0.5] - 2023-03-19
+
+### Initial Release
+- Basic lazy loading functionality
+- Simple implementation
+- Limited media support
+- Basic documentation
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
index 93c427c..3f00b61 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) 2022 - 2023 drementer
+Copyright (c) 2022 - 2025 drementer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index baf3eeb..86e97bf 100644
--- a/README.md
+++ b/README.md
@@ -1,40 +1,140 @@
-# Lazy Load Images
+# Lazy-load.js
-Lazy Load Images is a JavaScript utility that allows you to lazy load visual content when it approaches the visible area of the screen. This technique helps improve page loading speed by deferring the loading of images until they are actually needed.
+A lightweight lazy loading library for modern browsers.
-## Usage
+## Features
-To use Lazy Load Images, include the `lazyLoadImages` function in your JavaScript code. The function takes two optional parameters:
+- Simple and lightweight structure
+- Configurable options
+- Uses IntersectionObserver API
+- Support for various media types (img, video, iframe, etc.)
+- Event system for better control
-- `selector` (string, default: '[lazy]'): CSS selector for lazy load items.
-- `options` (object): IntersectionObserver options.
+## Installation
-If no selector is provided, the default selector '[lazy]' will be used.
-If no options are provided, default options will be used.
+```bash
+# with npm
+npm install lazy-load.js
+# or with yarn
+yarn add lazy-load.js
+```
+
+## Basic Usage
+
+HTML:
```html
-
-
+
+
+
```
+JavaScript:
```javascript
-lazyLoad();
+import lazyLoad from 'lazy-load.js';
+
+// Start with default settings
+const loader = lazyLoad();
+
+// Listen to events
+loader.on('loaded', (element) => {
+ console.log('Element loaded:', element);
+});
+
+loader.on('error', (element, error) => {
+ console.error('Loading error:', error, element);
+});
+
+// or with custom options
+const customLoader = lazyLoad('[lazy]', {
+ observer: {
+ rootMargin: '200px 0px',
+ threshold: 0.1
+ }
+});
-// Or
+// You can also add event listeners this way
+customLoader.on('waiting', (element) => {
+ console.log('Element waiting to be visible:', element);
+});
-lazyLoad('[lazy]', {
- root: null,
- threshold: 1,
- rootMargin: '300px 0px',
+// One-time event listener
+customLoader.once('loaded', (element) => {
+ console.log('This will only be called once for the first loaded element');
});
+
+// Remove specific event listener
+const myHandler = (element) => console.log('Element loaded:', element);
+customLoader.on('loaded', myHandler);
+customLoader.off('loaded', myHandler);
+```
+
+## Custom Attributes
+
+- `lazy`: Main lazy loading attribute for src
+- `lazy-srcset`: For srcset in img elements
+- `lazy-poster`: For poster in video elements
+
+## State Information
+
+The library adds a `lazy-state` attribute to elements at each stage of the loading process:
+
+- `waiting`: Element is not yet visible
+- `loading`: Element is visible and loading
+- `loaded`: Element has loaded successfully
+- `error`: An error occurred during loading
+
+You can apply CSS styles based on this attribute:
+
+```css
+[lazy-state="loading"] {
+ filter: blur(5px);
+ transition: filter 0.3s;
+}
+
+[lazy-state="loaded"] {
+ filter: blur(0);
+}
+
+[lazy-state="error"] {
+ opacity: 0.5;
+ filter: grayscale(100%);
+}
```
-The lazy load functionality will be applied to all elements that match the given selector. When an element approaches the visible area of the screen, its 'lazy' attribute will be used as the source for the 'src' attribute, and the element will be marked as loaded by adding the '-loaded' class.
+## All Settings
+
+```javascript
+{
+ attrs: {
+ src: 'lazy',
+ srcset: 'lazy-srcset',
+ poster: 'lazy-poster',
+ },
+ observer: {
+ root: null,
+ threshold: 1,
+ rootMargin: '100% 0px',
+ }
+}
+```
+
+## Events
+
+The library provides an event system for better control over the loading process:
+
+- `waiting`: Fired when element is not yet visible
+- `loading`: Fired when element starts loading
+- `loaded`: Fired when element is loaded successfully
+- `error`: Fired when loading fails
-## Developer
+## Browser Support
-[@drementer](https://github.com/drementer)
+- Chrome 51+
+- Firefox 55+
+- Safari 12.1+
+- Edge 79+
## License
-[MIT](https://choosealicense.com/licenses/mit/)
+MIT
diff --git a/dist/lazy-load.js b/dist/lazy-load.js
new file mode 100644
index 0000000..a470c03
--- /dev/null
+++ b/dist/lazy-load.js
@@ -0,0 +1,7 @@
+/**
+ * lazy-load.js
+ * @version 0.0.8
+ * @license MIT
+ * @link https://github.com/drementer/lazy-load.js
+ */
+!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).lazyLoad=t()}(this,(function(){"use strict";var e={attrs:{src:"lazy",srcset:"lazy-srcset",poster:"lazy-poster"},observer:{root:null,threshold:1,rootMargin:"100% 0px"}};class t{#e=new Map;on(e,t){return this.#e.has(e)||this.#e.set(e,[]),this.#e.get(e).push(t),this}off(e,t){if(!this.#e.has(e))return;const s=this.#e.get(e);return this.#e.set(e,((e,t)=>e.filter((e=>e!==t)))(s,t)),this}once(e,t){const s=(...r)=>{t(...r),this.off(e,s)};return this.on(e,s)}emit(e,...t){const s=this.#e.get(e)||[];return!!s.length&&(s.forEach((e=>e(...t))),!0)}get events(){return this.#e}}const s=["img","video","embed","object","iframe","audio"];class r extends t{#t;#s;constructor(t,s){super(),this.#t=t,this.#s={...e,...s},this.init()}#r(e){Object.entries(this.#s.attrs).forEach((([t,s])=>e.removeAttribute(s)))}#n(e){try{(e=>{const t=e.tagName.toLowerCase();if(!s.includes(t))throw new Error(`${t} Element is not supported!`)})(e),this.emit("waiting",e),((e,t,s)=>{new IntersectionObserver(((e,s)=>{e.forEach((e=>{e.isIntersecting&&(t(e.target),s.unobserve(e.target))}))}),s).observe(e)})(e,this.#o,this.#s.observer)}catch(t){console.warn("Lazy-load error:",e)}}#o(e){var t,s;this.emit("loading",e),t=e,s=this.#s,Object.entries(s.attrs).forEach((([e,s])=>{const r=t.getAttribute(s);r&&t.setAttribute(e,r)})),e.addEventListener("load",(()=>{this.#r(e),this.emit("loaded",e)}),{once:!0}),e.addEventListener("error",(()=>{this.emit("error",e,"Loading media failed"),console.warn("Lazy-load error:",e)}),{once:!0})}init(){return(e=>{if(e instanceof Element)return[e];if(e instanceof NodeList)return e;if(e instanceof Array)return e;const t=document.querySelectorAll(e);if(!t.length)throw new Error("No lazy loadable element found!");return t})(this.#t).forEach((e=>this.#n(e))),this}}return(e="[lazy]",t={})=>new r(e,t)}));
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..31248f8
--- /dev/null
+++ b/index.html
@@ -0,0 +1,304 @@
+
+
+