javascript - HTML 5's <picture> lazy load (without jquery) -


i've programmed small lightbox viewing images on website. keep loading process fast "lazy load" fullsize image. @ moment browsers load preview (test.jpg?size=520px) , fullsize image (test.jpg).

is there simple solution prevent browsers loading fullsize image until image clicked?

  • the website using minimal javascript - found no "no javascript" solution. additionally prefer solution doesn't require large html strings inside .js file added on mouse click.
  • most lazy load scripts change attribute key inside image tag. however, html 5 longer useful approach. maybe possible change <picture> tag (<picture> <<>> <picture-disabled>) , prefetch full size image?

html:

<div class="imagecc">         <picture onclick="lightboxshow('test004.jpg')">             <source type="image/webp" srcset="test004.webp?size=520px">             <img src="test.jpg?size=520px" alt="...">         </picture>         <p class="imgcaption">...</p> </div> <div id="test004.jpg" class="imageccbox" onclick="lightboxclose(this)">     <div class="imageccboxpicture">         <picture>             <source type="image/webp" srcset="test004.webp">             <img src="test.jpg">         </picture>     </div> </div>   

css:

.imageccbox {        display: none;        position: fixed;        z-index: 9999;        top: 0; left: 0;        width: 100%;        height: 100%;        background: linear-gradient(180deg, rgba(255,255,255,0.9), rgba(255,255,255,1));        text-align: center;} .imageccbox:target {         background: rgba(255,255,255,0.8);         display: block;         outline: none;} .imageccbox:target > .imageccboxpicture {opacity: 1.0;} .imageccboxpicture {         margin-top: 5%;         opacity: 0.4;         transition: opacity 500ms linear;         cursor: none;} 

javascript:

function lightboxshow(object) {         var imageccbox = document.getelementbyid(object);         imageccbox.style.display='block';         settimeout(function() {imageccbox.getelementsbyclassname("imageccboxpicture")[0].style.opacity = 1.0;}, 25);          window.location.hash = object;} 

consider using <template> element. it's part of web components.

essentially stuff content in <template> not want browser load, , move content when want loaded. make change onload, or onclick, or whatever other event like. either way, it's not lot of javascript , don't need libraries.

take @ tutorial: quick trick: using template delay loading of images.

older browsers don't support tag won't lazy load benefit, there nice progressive enhancement bonus there well.

another reference: http://webcomponents.org/articles/introduction-to-template-element/


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -