国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
Method #2: Using links to create an image
ホームページ ウェブフロントエンド CSSチュートリアル クリック時(shí)に畫(huà)像を表示する方法

クリック時(shí)に畫(huà)像を表示する方法

Mar 23, 2025 am 09:15 AM

How to Show Images on Click

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 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 data-src="image.jpg" src="" 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!).

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>

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?

以上がクリック時(shí)に畫(huà)像を表示する方法の詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見(jiàn)つけた場(chǎng)合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫(huà)像を無(wú)料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫(xiě)真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫(xiě)真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類(lèi)リムーバー

Video Face Swap

Video Face Swap

完全無(wú)料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無(wú)料のコードエディター

SublimeText3 中國(guó)語(yǔ)版

SublimeText3 中國(guó)語(yǔ)版

中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開(kāi)発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開(kāi)発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

「レンダリングブロッキングCSS」とは何ですか? 「レンダリングブロッキングCSS」とは何ですか? Jun 24, 2025 am 12:42 AM

ブラウザは、特にインポートされたスタイルシート、ヘッダーのインラインCSS、および最適化されていないメディアクエリスタイルを使用して、ブラウザがインラインおよび外部CSSをデフォルトで主要なリソースとして表示するため、ページレンダリングをブロックします。 1.重要なCSSを抽出し、HTMLに埋め込みます。 2。JavaScriptを介して非クリティカルなCSSの読み込みを遅らせる。 3.メディア屬性を使用して、印刷スタイルなどのロードを最適化します。 4.リクエストを減らすためにCSSを圧縮およびマージします。ツールを使用してキーCSSを抽出し、REL = "Preload"非同期負(fù)荷を組み合わせ、過(guò)度の分割と複雑なスクリプト制御を避けるためにメディア遅延荷重を合理的に使用することをお?jiǎng)幛幛筏蓼埂?/p>

外部対內(nèi)部CSS:最良のアプローチは何ですか? 外部対內(nèi)部CSS:最良のアプローチは何ですか? Jun 20, 2025 am 12:45 AM

TheBestAppRoachforCSDependsonTheProject'sSpecificNeeds.forLargerProjects、externalCssissisbetterduetoMaintainasiladability; forsmallerProjectsOrsingLe-PageApplications、internalcsSmightBemoresuitable.it

私のCSSは小文字でなければなりませんか? 私のCSSは小文字でなければなりませんか? Jun 19, 2025 am 12:29 AM

いいえ、CSSDOESNOTHAVETOBEINLOWERCASE。

CSSケース感度:重要なことを理解する CSSケース感度:重要なことを理解する Jun 20, 2025 am 12:09 AM

cssismostlycase-inssensitive、buturlsandfontfamilynamesarecase-sensitive.1)propertiesandvalueslikecolor:red; areotcase-sensitive.2)urlsmustmatchtheserver'scase、例えば、/畫(huà)像/logo.png.3)

Autoprefixerとは何ですか?それはどのように機(jī)能しますか? Autoprefixerとは何ですか?それはどのように機(jī)能しますか? Jul 02, 2025 am 01:15 AM

Autoprefixerは、ターゲットブラウザスコープに基づいてCSS屬性にベンダープレフィックスを自動(dòng)的に追加するツールです。 1.エラーで接頭辭を手動(dòng)で維持する問(wèn)題を解決します。 2. PostCSSプラグインフォーム、CSSを解析し、プレフィックスする必要がある屬性を分析し、構(gòu)成に従ってコードを生成する屬性を分析します。 3.使用手順には、プラグインのインストール、ブラウザーリストの設(shè)定、ビルドプロセスでそれらを有効にすることが含まれます。 4。メモには、接頭辭を手動(dòng)で追加しない、構(gòu)成の更新を保持すること、すべての屬性ではなくプレフィックスを維持することが含まれ、プリ??プロセッサでそれらを使用することをお?jiǎng)幛幛筏蓼埂?/p>

CSSカウンターとは何ですか? CSSカウンターとは何ですか? Jun 19, 2025 am 12:34 AM

csScountersCantAnationally-bersectionSandLists.1)usecounter-resettoinitialize、counter-incrementtoincrease、andcounter()orcounters()todisplayvalues.2)を組み合わせたjavascriptfordynamiccontenttoensureaCurateupdatesと組み合わせます。

CSS:ケースはいつ重要ですか(いつそうではありませんか)? CSS:ケースはいつ重要ですか(いつそうではありませんか)? Jun 19, 2025 am 12:27 AM

CSSでは、セレクターと屬性名はケースに敏感ですが、値、名前の色、URL、およびカスタム屬性はケースに敏感です。 1.バックグラウンドカラーや背景色など、セレクターと屬性名はケース非感受性です。 2。値の16進(jìn)數(shù)色は大文字と小文字を區(qū)別しますが、赤と赤などの名前の色は無(wú)効です。 3. URLは癥例に敏感であり、ファイルロードの問(wèn)題を引き起こす可能性があります。 4.カスタムプロパティ(変數(shù))はケースに敏感であり、使用する場(chǎng)合はケースの一貫性に注意を払う必要があります。

CSSの癥例感度:説明されたセレクター、プロパティ、および値 CSSの癥例感度:説明されたセレクター、プロパティ、および値 Jun 19, 2025 am 12:38 AM

cssselectors andpropertynamesarecase-inssensitive、whilevaluescanbecase-sensitivedingoncontext.1)selectorslike'div'andiv'areequivalent.2)propertiessuchas'background-color'and'background-color'arecase-sensens

See all articles