JSナレッジポイントのまとめ
1.ドキュメントフローとは何ですか?
ドキュメントフローとは、レイアウトプロセス中に要素が左から右、上から下の順に配置されることを意味します。
ブロックレベルの要素が1つの行を占め、その行の要素がコンテンツサイズの順にレンダリングされます。行がいっぱいになると、レンダリングは次の行から開始されます。
Reduceを実装する
function myReduce(arr, callback, initialValue) {
if (!(arr instanceof Array)) {
throw TypeError(`${
arr} is not an array!`);
}
if (typeof callback !== "function") {
throw TypeError(`${
callback} is not a function!`);
}
// 1.遍历arr调用callback,若initialValue为undefined则赋值为第一个值
const n = arr.length;
if (n < 1) {
return null;
}
// 2.维护一个累加值接受callback的返回值
let accumulator;
let startIndex = 0;
// 若有初始值
if (initialValue !== undefined) {
accumulator = initialValue;
} else {
accumulator = arr[0];
startIndex = 1;
}
for (let i = startIndex; i < n; i++) {
accumulator = callback(accumulator, arr[i], i, arr);
}
// 3.返回累加值
return accumulator;
}
2つ目は、offsetTopとscrollTopの違いです。
offsetTop:親要素の最上部と最上部までの現在の要素ここでの距離は親要素を指します:offsetParent offsetParent:要素配置要素または最も近いテーブルtd、th、を含む最新(最新の平均階層を含む)ボディ要素。scrollTop:ウィンドウ距離の上部および上部までの現在の要素