diff options
Diffstat (limited to 'build/posts/load-image-on-click')
-rw-r--r-- | build/posts/load-image-on-click/index.html | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/build/posts/load-image-on-click/index.html b/build/posts/load-image-on-click/index.html new file mode 100644 index 0000000..95211bc --- /dev/null +++ b/build/posts/load-image-on-click/index.html @@ -0,0 +1,68 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="color-scheme" content="dark light"> + <link rel="icon" href="data:,"> + <title>Click to Load Website Images</title> + <link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" /> + <link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" /> +<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}blockquote{background:rgba(0,0,0,0.1);border-left:4px solid;padding-left:5px;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style> +</head> + +<nav> + <a href="#menu">Menu ↓</a> +</nav> + +<main> +<h1 id="click-to-load-website-images">Click to Load Website Images</h1> +<p>2021-03-25</p> +<p>In my previous post about <a href="https://uglyduck.ca/#2021-03-22-89-posts-one-file">switching my Jekyll blog over to PHPetite</a>, I briefly mentioned how I only loaded in article images if the user <em>clicked or tapped</em> the empty file element.</p> +<p>In this post, I’m going to quickly breakdown the update I’ve done to my blog’s images since then and how you can easily implement the same thing in your own project.</p> +<h2 id="update">Update</h2> +<p>As pointed out by Gabriel <a href="https://github.com/bradleytaunt/phpetite/issues/1">in this Github issue</a>, this concept breaks things slightly for RSS users. Since then, I have just set the default images on this blog to <code>display: none</code> and render them as <code>block</code> elements when their specific <code>section</code> is loaded into the DOM visibly.</p> +<p>The example below is remaining the same as it was, to still provide context for this post.</p> +<h2 id="live-demo">Live Demo</h2> +<p>Before we jump head first into the details, let’s take a look at what we will be creating:</p> +<p>Pretty neat, eh? Well let’s get into the nitty gritty.</p> +<h2 id="the-code">The Code</h2> +<p>Personally, I place everything into a <code>figure</code> element to keep things contained and clean - but this isn’t required by any means. We then include our <code>img</code> and <code>figcaption</code> elements. That’s it.</p> +<pre><code><figure> + <img src="/placeholder-image.webp" onclick="this.src='https://res.cloudinary.com/bradtaunt/image/fetch/q_auto:low/v1570124593/https://uglyduck.ca/public/images/aqua-ui-css-buttons.webp'" alt="Aqua UI buttons"> + <figcaption><b>Click the placeholder to load in the real image</b><br> + Example Dribbble shot for testing. Feel free to click the default image in order to load the correct Dribbble source. + <a href="https://res.cloudinary.com/bradtaunt/image/fetch/q_auto:low/v1570124593/https://uglyduck.ca/public/images/aqua-ui-css-buttons.webp">View full size image</a>. + </figcaption> +</figure> +</code></pre> +<h3 id="the-image-element">The Image Element</h3> +<p>This is where the <em>magic</em> happens. By default all images will target the default placeholder image: <code>placeholder-image.webp</code>. This image is just 16KB in size and only needs to load in once.</p> +<p>Next we include an inline <code>onclick</code> attribute, which targets the current image’s <code>src</code> attribute and changes it based on the URL provided. (Note: I use Cloudinary for my blog’s image storage, but you could even host your images relative to your root directory if you wanted)</p> +<p>Now when a user clicks on the placeholder image, the inline <code>onclick</code> pulls in the correct image in it’s place.</p> +<h3 id="disabled-javascript">Disabled JavaScript</h3> +<p>For users who have JavaScript blocked or disabled we have a decent backup. By including a direct link to the image URL in the <code>figcaption</code> element, we give the user the ability to still view the image in a separate browser tab.</p> +<p>You could get extra fancy and include some <code>noscript</code> tags in your project that maybe render a different placeholder image mentioning they have JavaScript disabled etc, but for my needs that would be overkill.</p> +<h2 id="cool---but-why-do-this">Cool - But Why Do This?</h2> +<p>Bandwidth is a limited resource for a lot of users around the world. As designers and developers it’s best to respect this fact and only load in elements as the user <em>requires</em> them. Every little bit helps.</p> +<footer role="contentinfo"> + <h2>Menu Navigation</h2> + <ul id="menu"> + <li><a href="/">Home</a></li> + <li><a href="/projects">Projects</a></li> + <li><a href="/uses">Uses</a></li> + <li><a href="/wiki">Wiki</a></li> + <li><a href="/resume">Resume</a></li> + <li><a href="/colophon">Colophon</a></li> + <li><a href="/now">Now</a></li> + <li><a href="/donate">Donate</a></li> + <li><a href="/atom.xml">RSS</a></li> + <li><a href="#top">↑ Top of the page</a></li> + </ul> + <small> + Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br> + Maintained with ♥ for the web. <br> + Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> & <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br> + The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>. + </small> +</footer>
\ No newline at end of file |