Javascript_30_22
15265 ワード
こんにちは!
デレクです😁
時間...流れが速いなぁ…!この
javascript 30
シリーズは近いうちに終わります早く終わるように頑張ります:)
今日はDay 22の計画を整理します
かわいいけどいつか使える機能
22. Follow along Link Highlighter
n/a.ターゲット
段落内のa
ラベルにマウスを置くと、そのラベルが強調されます.
マウスが文書中のa
タグの上を移動すると、このタグを強調表示することができる.
DerekとWes Bosの実装コード
const triggers = document.querySelectorAll("a");
const highlight = document.createElement("span");
highlight.classList.add("highlight");
document.body.append(highlight);
function highlightLink() {
const linkCoords = this.getBoundingClientRect();
const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
};
triggers.forEach(link => link.addEventListener("mouseenter", highlightLink));
まず、triggers
変数は、段落内のすべてのa
タグ配列を有する.
また、highlight
変数を作成し、まずdocument.body
でappend
を行います.
次に、a
タグを追跡するために、この2つの方法を使用します.
1.hightlightLink関数
この関数はmouseenter event
に登録されています.triggers.forEach(link => link.addEventListener("mouseenter", highlightLink));
a
タグアレイtriggers
にmouseenter
イベントhighlightLink
関数を登録した.function highlightLink() {
const linkCoords = this.getBoundingClientRect();
const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
}
初めて見たgetBoundingClientRect()
関数!MDN
によれば、
The Element.getBoundingClientRect()
method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.
すなわち,あるオブジェクトに対して,寸法と位置を教える関数と見なすことができる.
これは、オブジェクトの値がtop, bottom, left, right, width, height, x, y
であることを示します.
ここで確認できます.triggers
のイベント、this
のgetBoundingClientRect()
は、各a
タグの位置および属性を返します.その価格はlinkCoords
です.
これで、値段を確認できます!
これに基づいて、前述のspan
マークhighlight
を調整する.const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
各属性値がcoods
のオブジェクトを作成して保存します.highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
これにより、highlight
のstyle
のプロパティを変更して移動できます.
これは比較的簡単なテーマです!
初めて使用したgetBoundingClientRect()
関数を用いて簡単にした.
エラーや修正が必要な場合は、いつでもフィードバックしてください.
ありがとう!🤗
Reference
この問題について(Javascript_30_22), 我々は、より多くの情報をここで見つけました
https://velog.io/@ghdtjrrl94/Javascript3022
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
const triggers = document.querySelectorAll("a");
const highlight = document.createElement("span");
highlight.classList.add("highlight");
document.body.append(highlight);
function highlightLink() {
const linkCoords = this.getBoundingClientRect();
const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
};
triggers.forEach(link => link.addEventListener("mouseenter", highlightLink));
triggers.forEach(link => link.addEventListener("mouseenter", highlightLink));
function highlightLink() {
const linkCoords = this.getBoundingClientRect();
const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
}
const coords = {
width: linkCoords.width,
height: linkCoords.height,
top: linkCoords.top + window.scrollY,
left: linkCoords.left + window.scrollX
};
highlight.style.width = `${coords.width}px`;
highlight.style.height = `${coords.height}px`;
highlight.style.transform = `translate(${coords.left}px, ${coords.top}px)`;
Reference
この問題について(Javascript_30_22), 我々は、より多くの情報をここで見つけました https://velog.io/@ghdtjrrl94/Javascript3022テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol