aboutsummaryrefslogtreecommitdiff
path: root/build/load-image-on-click
diff options
context:
space:
mode:
authorBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
committerBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
commit6b742c459266b18e2b375b35205ce8a6c02f0452 (patch)
treeb16fbb9a045e33dd6c97eb5ab72e6ff4d9237ea3 /build/load-image-on-click
Initial commit
Diffstat (limited to 'build/load-image-on-click')
-rw-r--r--build/load-image-on-click/index.html72
1 files changed, 72 insertions, 0 deletions
diff --git a/build/load-image-on-click/index.html b/build/load-image-on-click/index.html
new file mode 100644
index 0000000..a3b6f57
--- /dev/null
+++ b/build/load-image-on-click/index.html
@@ -0,0 +1,72 @@
+<!doctype html>
+<html lang="en" id="top">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <link rel="icon" href="data:,">
+ <title>Click to Load Website Images</title>
+ <link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
+ <style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
+</head>
+
+<nav>
+ <a href="#menu">Menu &darr;</a>
+</nav>
+
+<main>
+<h1>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>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>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><figure>
+ <div class="img-parent">
+ <img loading="lazy" src="/placeholder-image.webp" onclick="this.src='/public/images/aqua-ui-css-buttons.webp'" alt="Aqua UI buttons">
+ </div>
+ <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="/public/images/aqua-ui-css-buttons.webp">View full size image</a>.</figcaption>
+</figure></p>
+<p>Pretty neat, eh? Well let's get into the nitty gritty.</p>
+<h2>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>&lt;figure&gt;
+ &lt;img src=&quot;/placeholder-image.webp&quot; onclick=&quot;this.src='https://res.cloudinary.com/bradtaunt/image/fetch/q_auto:low/v1570124593/https://uglyduck.ca/public/images/aqua-ui-css-buttons.webp'&quot; alt=&quot;Aqua UI buttons&quot;&gt;
+ &lt;figcaption&gt;&lt;b&gt;Click the placeholder to load in the real image&lt;/b&gt;&lt;br&gt;
+ Example Dribbble shot for testing. Feel free to click the default image in order to load the correct Dribbble source.
+ &lt;a href=&quot;https://res.cloudinary.com/bradtaunt/image/fetch/q_auto:low/v1570124593/https://uglyduck.ca/public/images/aqua-ui-css-buttons.webp&quot;&gt;View full size image&lt;/a&gt;.
+ &lt;/figcaption&gt;
+&lt;/figure&gt;
+</code></pre>
+<h3>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>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>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">&uarr; 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> &amp; <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