我正在尝试创建一个基于总数百分比的字体大小的word-cloud。每个单词都有一个"count“属性。
<h2>Cryptos</h2>
<div class="topics">
{#each cryptos as topic}
<a
href="/cryptos/{topic._id}"
style="font-size: {(topic.count / topics[0].count) * 100 + 100}%;">${topic._id}
({topic.count})</a>
{/each}
</div>这是不太正确的工作,我不确定为什么。
font-size: {(topic.count / topics[0].count) * 100 + 100}%;根据.count对主题进行降序排序
我基本上希望字体大小是100%在低端和200%在高端。
发布于 2020-12-24 14:24:07
可以通过将字体大小的范围与字数的范围进行比较来改变字体大小。
假设你有一个单词列表和它们的计数:
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现在我们可以计算字体大小了:
minFontSize + (deltaWordCount * sizeRatio)或者更具体地说:
{#each words as word}
<span style="font-size: {minFontSize + ((word.count - min) * (fontRange / countRange))}px">
{word.text}
</span>
{/each}你可以在这里找到一个可用的例子:https://svelte.dev/repl/770e4a35186449a182b8edecc4ed88ba?version=3.31.0
https://stackoverflow.com/questions/65419233
复制相似问题