diff options
Diffstat (limited to 'build/load-image-on-click/index.html')
-rw-r--r-- | build/load-image-on-click/index.html | 24 |
1 files changed, 2 insertions, 22 deletions
diff --git a/build/load-image-on-click/index.html b/build/load-image-on-click/index.html index 2dae12c..95211bc 100644 --- a/build/load-image-on-click/index.html +++ b/build/load-image-on-click/index.html @@ -3,11 +3,12 @@ <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;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style> +<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> @@ -16,29 +17,17 @@ <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> @@ -47,23 +36,14 @@ </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> |