search results:

    • Standard
    • React
    Pricing Learn Community
    • + D
    • Light
    • Dark
    • System
    logo TW Elements
    • Getting started
      • Quick start
      • Tutorials
      • Design system
      • Local installation
      • Optimization
      • Dark mode
      • Theming
      • Changelog
      • Migrating to v2
      • Internationalization guide
      • Class customization
      • Icons Integration
    • CommercialNew
      • Pricing
      • License
      • Installation
      • Git & repository
      • Premium Support
    • Integrations
      • Angular
      • ASP.NET
      • Django
      • Express
      • Laravel
      • Next
      • Nuxt
      • Qwik
      • React
      • Remix
      • Solid
      • Svelte
      • SvelteKit
      • Vue
    • Content & styles
      • Animations
      • Animations Extended
      • Colors
      • Dividers
      • Figures
      • Headings
      • Hover effects
      • Icons
      • Images
      • Mask
      • Shadows
      • Typography
    • Navigation
      • Breadcrumbs
      • Footer
      • Headers
      • Mega menu
      • Navbar
      • Offcanvas
      • Pagination
      • Pills
      • Scrollspy
      • Sidenav
      • Tabs
    • Components
      • Accordion
      • Alerts
      • Avatar
      • Badges
      • Button group
      • Buttons
      • Cards
      • Carousel
      • Chips
      • Collapse
      • Dropdown
      • Gallery
      • Jumbotron
      • Lightbox
      • Link
      • List group
      • Modal
      • Notifications
      • Paragraphs
      • Placeholders
      • Popconfirm
      • Popover
      • Progress
      • Rating
      • Scroll back to top button
      • Social buttons
      • Spinners
      • Stepper
      • Testimonials
      • Timeline
      • Toast
      • Tooltip
      • Video
      • Video carousel
    • Forms
      • Autocomplete
      • Checkbox
      • Datepicker
      • Datetimepicker
      • File input
      • Form templates
      • Input Group
      • Inputs
      • Login form
      • Multi range slider
      • Radio
      • Range
      • Registration form
      • Search
      • Select
      • Switch
      • Textarea
      • Timepicker
      • Validation
    • Data
      • Charts
      • Charts advanced
      • Datatables
      • Tables
    • Methods
      • Clipboard
      • Infinite scroll
      • Lazy loading
      • Loading management
      • Ripple
      • Scrollbar
      • Smooth scroll
      • Sticky
      • Touch
    • Design Blocks
      • Admin Charts
      • Admin Complex
      • Admin Forms
      • Admin Maps
      • Admin Navigation
      • Admin tables
      • Banners
      • Contact
      • Content
      • CTA
      • FAQ
      • Features
      • Headers
      • Hero / Intro sections
      • Logo clouds
      • Mega menu
      • News
      • Newsletter
      • Pricing
      • Projects
      • Stats
      • Stats admin
      • Team
      • Testimonials
    • Tools
      • Button generator
      • Card generator
      • Flexbox generator
      • Footer generator
      • Form builder
      • Grid generator
      • Icon generator
      • Instagram Filters generator
      • Logo generator
      • Table generator
      • Typography generator
    • Coming Soon
      • Angular
      • Builder
      • Templates
      • Vue
    • Resources
      • Playground
      • YouTube Channel
      • Private FB Group
      • Newsletter
      • UI Design course New
      • UI / UX tips
    • Overview
    • API

    Tailwind Lazy loading method

    Tailwind CSS Lazy loading

    Tailwind CSS Lazy Loading is a feature, that allows you to load images or videos only when they are visible on the screen.

    Required ES init: Lazy load *
    * UMD autoinits are enabled by default. This means that you don't need to initialize the component manually. However if you are using TW Elements ES format then you should pass the required components to the initTWE method.

    Basic example

    Lazy Loading will automatically initialize after adding data-twe-lazy-load-init attribute to your img or video element. It is important to add data-twe-lazy-src attribute - otherwise, Lazy Load will throw an error.

    Scroll down to see an image

    Small Apartment
    • HTML
    • javascript
            
                
          <div
            class="mt-5 h-[500px] w-full overflow-y-scroll"
            data-twe-lazy-load-init>
            <div class="grid h-[800px] place-items-center">
              <p>Scroll down to see an image</p>
              <p class="my-4">
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke-width="1.5"
                  stroke="currentColor"
                  class="h-16 w-16">
                  <path
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    d="M9 12.75l3 3m0 0l3-3m-3 3v-7.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
              </p>
    
              <img
                data-twe-lazy-load-init
                data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
                data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
                alt="Small Apartment"
                class="my-5 !mt-[50%] w-full" />
    
              <video
                class="w-full"
                data-twe-lazy-load-init
                data-twe-lazy-src="https://tecdn.b-cdn.net/img/video/Sail-Away.mp4"
                data-twe-lazy-delay="1000"
                data-twe-lazy-placeholder="https://place-hold.it/838x471?text=Loading"
                muted="muted"
                autoplay="autoplay"></video>
            </div>
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            LazyLoad,
            initTWE,
          } from "tw-elements";
          
          initTWE({ LazyLoad });
          
            
        

    Hey there 👋 we're excited about TW elements and want to see it grow! If you enjoy it, help the project grow by sharing it with your peers. Every share counts, thank you!

    Offset

    Use data-twe-lazy-offset attribute to define an additional amount of pixels after which image or video will show.

    Scroll more down to load a picture.

    Small Apartment
    • HTML
    • javascript
            
                
          <img
            data-twe-lazy-load-init
            data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
            data-twe-lazy-delay="1000"
            data-twe-lazy-offset="500"
            data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
            alt="Small Apartment"
            class="w-full" />
          
            
        
            
                
          // Initialization for ES Users
          import {
            LazyLoad,
            initTWE,
          } from "tw-elements";
          
          initTWE({ LazyLoad });
          
            
        

    Error

    Use data-twe-lazy-error to define a picture that will show if image or video doesn't load.

    Small Apartment
    • HTML
    • javascript
            
                
          <img
            data-twe-lazy-load-init
            data-twe-lazy-src="sample"
            data-twe-lazy-delay="1000"
            data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
            data-twe-lazy-error="https://place-hold.it/1321x583?text=Error"
            alt="Small Apartment"
            class="w-full" />
          
            
        
            
                
          // Initialization for ES Users
          import {
            LazyLoad,
            initTWE,
          } from "tw-elements";
          
          initTWE({ LazyLoad });
          
            
        

    Animations

    Use data-twe-lazy-animation to specify which animation you want to activate when element loads.

    Small Apartment
    Small Apartment
    • HTML
    • javascript
            
                
          <img
            data-twe-lazy-load-init
            data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
            data-twe-lazy-delay="1000"
            data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
            data-twe-lazy-animation="[zoom-in_1s_ease-in-out]"
            alt="Small Apartment"
            class="mb-3 w-full" />
    
          <img
            data-twe-lazy-load-init
            data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
            data-twe-lazy-delay="1000"
            data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
            data-twe-lazy-animation="[tada_1s_ease-in-out]"
            alt="Small Apartment"
            class="w-full" />
          
            
        
            
                
          // Initialization for ES Users
          import {
            LazyLoad,
            initTWE,
          } from "tw-elements";
          
          initTWE({ LazyLoad });
          
            
        

    Container

    Initialize a set of elements at once by adding data-twe-lazy-load-init attribute to their container. If you set data-twe-lazy attributes in the parent, all of img and video elements inside will inherit them. You can overwrite them by declaring these attributes in inner elements.

    Small Apartment Small Apartment
    • HTML
    • javascript
            
                
          <div
            data-twe-lazy-load-init
            data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
            data-twe-lazy-error="https://place-hold.it/1321x583?text=Error">
            <img
              data-twe-lazy-src="."
              data-twe-lazy-delay="1000"
              alt="Small Apartment"
              class="mb-3 w-full" />
            <img
              data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
              data-twe-lazy-delay="1000"
              data-twe-lazy-animation="[tada_1s_ease-in-out]"
              alt="Small Apartment"
              class="mb-3 w-full" />
            <video
              class="w-full"
              data-twe-lazy-src="https://tecdn.b-cdn.net/img/video/Sail-Away.mp4"
              data-twe-lazy-animation="[zoom-in_1s_ease-in-out]"
              muted="muted"
              autoplay="autoplay"></video>
          </div>
          
            
        
            
                
          // Initialization for ES Users
          import {
            LazyLoad,
            initTWE,
          } from "tw-elements";
          
          initTWE({ LazyLoad });
          
            
        

    Related resources

    Spinners Scrollbar Images Animations Icons Progress
    • Basic example
    • Offset
    • Error
    • Animations
    • Container
    • Related resources

    Tailwind Lazy loading method - API


    Import

    Importing components depends on how your application works. If you intend to use the TW elements ES format, you must first import the component and then initialize it with the initTWE method. If you are going to use the UMD format, just import the tw-elements package.

    • javascript
    • umd
            
                
            import { LazyLoad, initTWE } from "tw-elements";
            initTWE({ LazyLoad });
            
            
        
            
                
            import "tw-elements";
            
            
        

    Usage

    Via data attributes

    Simply add data-twe-lazy-load-init attribute to the image to initialize the lazy loading component. For ES format, you must first import and call the initTWE method.

    • HTML
            
                
            <img
              data-twe-lazy-load-init
              data-twe-lazy-src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(102).webp"
              data-twe-lazy-placeholder="https://place-hold.it/1321x583?text=Loading"
              alt="Small Apartment"
              class="my-5 w-full" />
            
            
        

    Via JavaScript

    • javascript
    • umd
            
                
            const myLazyLoading = new LazyLoad(document.getElementById('lazy-loading'), options);
            
            
        
            
                
            const myLazyLoading = new twe.LazyLoad(document.getElementById('lazy-loading'), options);
            
            
        

    Options

    Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-twe-, as in data-twe-lazy-delay="".

    Name Type Default Description
    lazyAnimation String '[fade-in_1s_ease-in-out]' Defines animation during element showing. Don't forget to include the animation in object safelist in theme of your tailwind.config.js file.
    lazyDelay Number 500 Defines delay after which element will show.
    lazyError String - Defines a picture that is shown if an error with showing element occurs.
    lazyOffset Number 0 Defines an additional offset after which element will show.
    lazyPlaceholder String - Defines a picture that is shown before loading a proper element.
    lazySrc String - Defines the source of an element.

    Methods

    Method Description Example
    dispose Removes an instance of the lazy element. lazyElement.dispose()
    getInstance Static method which allows you to get the lazy load instance associated to a DOM element. LazyLoad.getInstance(lazyLoadEl)
    getOrCreateInstance Static method which returns the lazy load instance associated to a DOM element or create a new one in case it wasn't initialized. LazyLoad.getOrCreateInstance(lazyLoadEl)
    • javascript
    • umd
            
                
            const myLazyLoadingEl = document.getElementById('myLazyLoading');
            const lazyLoading = new LazyLoad(myLazyLoadingEl);
            lazyLoading.dispose();
            
            
        
            
                
            const myLazyLoadingEl = document.getElementById('myLazyLoading');
            const lazyLoading = new twe.LazyLoad(myLazyLoadingEl);
            lazyLoading.dispose();
            
            
        

    Events

    Event type Description
    contentLoaded.twe.lazyLoad This event fires immediately when an image is loaded.
    loadingError.twe.lazyLoad This event fires immediately when an error with loading image occurs.
    • JavaScript
            
                
            const myLazyLoadingEl = document.getElementById('myLazyLoad')
            myLazyLoadingEl.addEventListener('contentLoaded.twe.lazyLoad', function (e) {
                // do something...
            })
            
            
        
    • Import
    • Usage
    • Options
    • Methods
    • Events
    Get useful tips & free resources directly to your inbox along with exclusive subscriber-only content.
    Join our mailing list now
    © 2024 Copyright: MDBootstrap.com

    Access restricted

    To view this section you must have an active PRO account

    Log in to your account or purchase an TWE subscription if you don't have one.

    Buy TWE PRO