Code Monkey home page Code Monkey logo

silverstripe-lazysizes's Introduction

Silverstripe Lazysizes module

Integrate https://github.com/aFarkas/lazysizes and https://github.com/aFarkas/respimage to Silverstripe.

Code heavily inspired by https://github.com/heyday/silverstripe-responsive-images

All-in-one solution for:

  • Lazy loading images
  • Responsive images
  • Placeholder images (using placehold.it)

Respimages is only loaded if the browser does not support native syntax.

Plugins

This modules has already pre built plugins into a consolidated js file.

Plugins available by default are:

Lasysizes:

  • unveilhooks
  • print
  • bgset
  • include

Respimages:

  • oldie
  • intrinsic
  • mutation

You can also define your own path to the libraries by adjusting the following path

lekoala\LazySizesImageExtension:
	js_path: 'lazysizes/javascript'

LazyLoading

If you simply want to lazy load your image, you can use any of three default methods which specify no additional sizes.

Example:

$Image.Lazy(720x250)
$Image.LazyCrop(720x250)
$Image.LazyCropFocus(720x250)

Responsive sets

You can define your own responsive sets. You can see the bundled configuration for the ResponsiveDefault set which set 4 breakpoints. You can define your own sets, change sizes, method and default size to be used.

All sets use lazysizes data-sizes="auto" by default, meaning that the width of the parent is used to determine which size needs to be loaded.

Retina is also supported.

Config example:

lekoala\LazySizesImageExtension:
  sets:
	  ResponsiveDefault:
		  sizes: [320x213 320w,640x426 640w,960x639 960w,1280x852 1280w]
	  ResponsiveDefaultSquare:
		  sizes: [320x320 320w,640x640 640w,960x960 960w,1280x1280 1280w]
	  ResponsiveDefaultRetina:
		  sizes: [400x300 1x, 800x600 2x]

Usage example in template:

$Image.ResponsiveDefault

Using placeholders

The controller extension provides a helper method to easily create placeholders using PlaceholdIt. You can define a base size and a src set.

Example:

$PlaceholdIt(175x75,'200x100,500x300,800x400')

SrcSet

Sometimes, you just need to output a list of srcset paths (for instance, to define a bgset). The last element of the set is never resized (always using the original image at maximum size). You can prevent this from happening by setting the second argument to false.

Example:

<body id="$ClassName" class="typography lazyload" data-bgset="$SiteConfig.RandomBackgroundImage.SrcSet('480,800')">

Compatibility

Tested with 4.4.4

Installation

composer require lekoala/silverstripe-lazysizes

Maintainer

LeKoala - [email protected]

silverstripe-lazysizes's People

Contributors

ollyradford avatar lekoala avatar

Watchers

James Cloos avatar

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.