Giter VIP home page Giter VIP logo

jquery-lazyloadscripts's Introduction

jquery-lazyloadscripts.js

jquery-lazyloadscripts.js is a lightweight jquery plugin for lazy loading scripts by appending the script tag to the dom when the element that is requiring the script is in viewport.

Why?

This script is primarily useful for stuff that doesn't need to be available on page load (such as google maps) to improve loading speed and make all the page speed tools shut up a bit more.

Usage

html

<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>

<section id="map" data-lazy-load-scripts="SCRIPT_URL"></section>

It is also possible to lazy load several scripts by passing the urls in array syntax.

<section id="map" data-lazy-load-scripts='["SCRIPT_URL_1", "SCRIPT_URL_2", "SCRIPT_URL_3"]'></section>

javascript

$('section').lazyLoadScripts()

Options

offset

Type: Int
Default: 0

The distance in px the lazy load should be triggered before it arrives at the actual container.

$('section').lazyLoadScripts({
	offset: 200 // starts loading 200px above the container
})

onWatch

Type: function

The distance in px the lazy load should be triggered before it arrives at the actual container.

$('section').lazyLoadScripts({
	// offset: 200,
	onWatch: function(elements)
	{
		console.log(elements)
		/* return object:
		{
			0: {
				elem: $(section),
				inDom: false || true,
				scriptSrc: SCRIPT_URL
			},
			...
		}
		*/
	}
})

onSuccess

Type: function

A callback function that is executed (for each script) if the request succeeds.

onAllDone

Type: function

A callback function that is executed when all scripts are loaded.

onFail

Type: function

A callback function that is executed when one or more scripts failed to load.

To-do

  • find a cool way to determine if the script has been loaded successfully and pass the status to the onWatch event

Author

Rico Dang

github/pgmlde

License

Copyright © 2018, Rico Dang. Released under the MIT License.

jquery-lazyloadscripts's People

Contributors

pgml avatar radon8472 avatar

Watchers

 avatar

jquery-lazyloadscripts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.