Most images on the web are superfluous. If I might be a jerk for a bit, 99% of them aren’t even that helpful at all (although there are rare exceptions). That’s because images don’t often complement the text they’re supposed to support and instead hurt users, taking forever to load and blowing up data caps like some sort of performance tax.
Thankfully, this is mostly a design problem today because making images performant and more user-friendly is so much easier than it once was. We have better image formats likeWebP(and soon, perhaps,JPEG XL). We have the magic ofresponsive imagesof course. And there are tons of great tools out there, like ImageOptim, as well as resources such as Addy Osmani’s new book.
Although perhaps my favorite way to improve image performance is withlazy loading:
<img src="/static/imghwm/default1.png" data-src="image.jpg" class="lazy" href="image.webp" alt="Image description" loading="lazy">
This image will only load when a user scrolls down the page so it’s visible to the user — which removes it from the initial page load and that’s just great! Making that initial load of a webpage lightningfast is a big deal.
But maybe there are images that should never load at all. Perhaps there are situations where it’d be better if a person could opt-into seeing it. Here’s one example: take the text-only version ofNPRand click around for a bit. Isn’t it… just so great?! It’s readable! There’s no junk all over the place, it respects me as a user and — sweet heavens — is itfast.
So! What if we could show images on a website but only once they are clicked or tapped? Wouldn’t it be neat if we could show a placeholder and swap it out for the real image on click? Something like this:
Well, I had two thoughts here as to how to build this chap (the golden rule is that there’s never one way to build anything on the web).
Method #1: Using
without a src attribute
We can remove the src attribute of an tag to hide an image. We could then put the image file in an attribute, like data-src or something, just like this:
<img src="/static/imghwm/default1.png" data-src="image.jpg" class="lazy" data- alt="Photograph of hot air balloons by Musab Al Rawahi. 144kb">
By default, most browsers will show a broken image icon that you’re probably familiar with:
Okay, so it’s sort of accessible. I guess? You can see the alt tag rendered on screen automatically, but with a light dash of JavaScript, we can then swap out the src with that attribute:
document.querySelectorAll("img").forEach((item) => { item.addEventListener("click", (event) => { const image = event.target.getAttribute("data-src"); event.target.setAttribute("src", image); }); });
Now we can add some styles and ugh, oh no:
Ugh. In some browsers there’ll be a tiny broken image icon in the bottom when the image hasn’t loaded. The problem here is that browsers don’t give you a way to remove the broken image icon with CSS (and we probably shouldn’t be allowed to anyway). It’s a bit annoying to style the alt text, too. But if we remove the alt attribute altogether, then the broken image icon is gone, although this makes the unusable without JavaScript. So removing that alt text is a no-go.
As I said: Ugh. I don’t think there’s a way to make this method work (although please prove me wrong!).
Method #2: Using links to create an image
The other option we have is to start with the humble hyperlink, like this:
<a href="image.jpg">Photograph of hot air balloons by Musab Al Rawahi. 144kb</a><a></a>
Which, yep, nothing smart happening yet — this will just render a link to an image:
That works accessibility-wise, right? If we don’t have any JavaScript, then all we have is just a link that folks can optionally click. Performance-wise, it can’t get much faster than plain text!
But from here, we can reach for JavaScript to stop the link from loading on click, grab the href attribute within that link, create an image element and, finally, throw that image on the page and remove the old link once we’re done:
document.querySelectorAll(".load-image").forEach((item) => { item.addEventListener("click", (event) => { const href = event.target.getAttribute("href"); const newImage = document.createElement("img"); event.preventDefault(); newImage.setAttribute("src", href); document.body.insertBefore(newImage, event.target); event.target.remove(); }); });
We could probably style this placeholder link to make it look a bit nicer than what I have below. But this is just an example. Go ahead and click the link to load the image again:
And there you have it! It isn’t groundbreaking or anything, and I’m sure someone’s done this before at some point or another. But if we wanted to be extremely radical about performance beyond the first paint and initial load, then I think this is an okay-ish solution. If we’re making a text-only website then I think this is definitely the way to go.
Perhaps we could also make a web component out of this, or even detect if someone hasprefers-reduced-dataand then only load images if someone has enough data or something. What do you think?
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

À quoi cela ressemble de dépanner l'un de ces problèmes impossibles qui se révèle être quelque chose d'autre à lequel vous n'avez jamais pensé.

@KeyFramesAndcsstransitionsDiffeRinComplexity: @ KeyframesAllowsfordEtailedanimationSequences, tandis que les StransitionShandlesImpLestateChanges.USECSSTRANSITIONSFORHOVEFFECTSLILLOBUTTONCOLORCHANGES, et @ KeyframesFransintricateAralienslikeTatationSpinners.

Je sais, je sais: il y a une tonne d'options de système de gestion de contenu disponibles, et bien que je les teste plusieurs, aucun n'a vraiment été celui, y & # 039; savoir? Des modèles de tarification étranges, une personnalisation difficile, certains finissent même par devenir un tout

La liaison des fichiers CSS à HTML peut être réalisée en utilisant des éléments dans une partie de HTML. 1) Utilisez des balises pour relier les fichiers CSS locaux. 2) Plusieurs fichiers CSS peuvent être implémentés en ajoutant plusieurs balises. 3) Les fichiers CSS externes utilisent des liens URL absolus, tels que. 4) Assurez-vous que l'utilisation correcte des chemins de fichier et de l'ordre de chargement du fichier CSS et optimiser les performances peuvent utiliser le préprocesseur CSS pour fusionner les fichiers.

Le choix de Flexbox ou de la grille dépend des exigences de mise en page: 1) Flexbox convient aux dispositions unidimensionnelles, telles que la barre de navigation; 2) La grille convient aux dispositions bidimensionnelles, telles que les dispositions de magazines. Les deux peuvent être utilisés dans le projet pour améliorer l'effet de mise en page.

La meilleure façon d'inclure des fichiers CSS est d'utiliser des balises pour introduire des fichiers CSS externes dans la pièce HTML. 1. Utilisez des balises pour introduire des fichiers CSS externes, tels que. 2. Pour les petits ajustements, le CSS en ligne peut être utilisé, mais doit être utilisé avec prudence. 3. Les grands projets peuvent utiliser des préprocesseurs CSS tels que SASS ou moins pour importer d'autres fichiers CSS via @Import. 4. Pour les performances, les fichiers CSS doivent être fusionnés et CDN doit être utilisé et compressé à l'aide d'outils tels que CSSNANO.

Oui, vous émeuble-chouchoudion-dimensionnal, flexiblelayAndavigationMenus.2)

À quoi cela ressemble-t-il de refactor votre propre code? John Rhea sépare une vieille animation CSS qu'il a écrite et traverse le processus de réflexion pour l'optimiser.


Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

Video Face Swap
Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Article chaud

Outils chauds

Listes Sec
SecLists est le compagnon ultime du testeur de sécurité. Il s'agit d'une collection de différents types de listes fréquemment utilisées lors des évaluations de sécurité, le tout en un seul endroit. SecLists contribue à rendre les tests de sécurité plus efficaces et productifs en fournissant facilement toutes les listes dont un testeur de sécurité pourrait avoir besoin. Les types de listes incluent les noms d'utilisateur, les mots de passe, les URL, les charges utiles floues, les modèles de données sensibles, les shells Web, etc. Le testeur peut simplement extraire ce référentiel sur une nouvelle machine de test et il aura accès à tous les types de listes dont il a besoin.

SublimeText3 version anglaise
Recommandé : version Win, prend en charge les invites de code !

SublimeText3 Linux nouvelle version
Dernière version de SublimeText3 Linux

VSCode Windows 64 bits Télécharger
Un éditeur IDE gratuit et puissant lancé par Microsoft

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)
