1 回答

TA貢獻1821條經驗 獲得超6個贊
通過將字體大小范圍與字數范圍進行比較,可以改變字體大小。
想象一下您有一個單詞列表及其計數:
const words = [
? { text: "apples", count: 10 },
? { text: "pears", count: 30 },
? // ...
]
的范圍font-size可以計算:
const minFontSize = 10
const maxFontSize = 30
// delta between sizes
const fontRange = maxFontSize - minFontSize
然后計算最小字數、最大字數和字數范圍。響應式語句對此很有效:
// list of counts
$: counts = words.map(record => record.count)
// minimum word count
$: min = Math.min(...counts)
// maximum word count
$: max = Math.max(...counts)
// delta between smallest and largest word count
$: countRange = max - min
現在我們可以計算字體大?。?/p>
minFontSize + (deltaWordCount * sizeRatio)
或者更具體地說:
{#each words as word}
? <span style="font-size: {minFontSize + ((word.count - min) * (fontRange / countRange))}px">
? ? {word.text}
? </span>
{/each}
添加回答
舉報