aboutsummaryrefslogtreecommitdiff
path: root/build/load-image-on-click/index.html
blob: 2dae12c94be7dd774b905d84745ca448e111e5e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!doctype html>
<html lang="en">
<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="/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>
</head>

<nav>
	<a href="#menu">Menu &darr;</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&#8217;m going to quickly breakdown the update I&#8217;ve done to my blog&#8217;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&#8217;s take a look at what we will be creating:</p>

<p>Pretty neat, eh? Well let&#8217;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&#8217;t required by any means. We then include our <code>img</code> and <code>figcaption</code> elements. That&#8217;s it.</p>

<pre><code>&#60;figure&#62;
    &#60;img src="&#47;placeholder-image.webp" onclick="this.src=&#39;https:&#47;&#47;res.cloudinary.com&#47;bradtaunt&#47;image&#47;fetch&#47;q_auto:low&#47;v1570124593&#47;https:&#47;&#47;uglyduck.ca&#47;public&#47;images&#47;aqua-ui-css-buttons.webp&#39;" alt="Aqua UI buttons"&#62;
    &#60;figcaption&#62;&#60;b&#62;Click the placeholder to load in the real image&#60;&#47;b&#62;&#60;br&#62;
        Example Dribbble shot for testing. Feel free to click the default image in order to load the correct Dribbble source.
        &#60;a href="https:&#47;&#47;res.cloudinary.com&#47;bradtaunt&#47;image&#47;fetch&#47;q_auto:low&#47;v1570124593&#47;https:&#47;&#47;uglyduck.ca&#47;public&#47;images&#47;aqua-ui-css-buttons.webp"&#62;View full size image&#60;&#47;a&#62;.
    &#60;&#47;figcaption&#62;
&#60;&#47;figure&#62;
</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&#8217;s <code>src</code> attribute and changes it based on the URL provided. (Note: I use Cloudinary for my blog&#8217;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&#8217;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&#8217;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>