1 回答

TA貢獻1829條經驗 獲得超7個贊
代替 px,使用 vh 或 vw ov vmax 或 vmin 單位
這里有一些解釋https://web-design-weekly.com/2014-11-18-viewport-units-vw-vh-vmin-vmax/
vw:視口寬度的 1/100
vh: 1/100 視口高度
vmin:最小邊的 1/100
vmax:最大邊的 1/100
// Draw circles
const list = (length, callback) =>
Array.from(new Array(length), (hole, index) => callback(index))
const random = (min, max) => Math.random() * (max - min) + min
const viewportWidth = Math.max(
document.documentElement.clientWidth,
window.innerWidth || 0
)
const viewportHeight = Math.max(
document.documentElement.clientHeight,
window.innerHeight || 0
)
const elements = list(48, () => {
const circle = document.createElement("span")
const minSize = Math.round((viewportWidth + viewportHeight) / 150) // tune this to your needs
const maxSize = Math.round((viewportWidth + viewportHeight) / 80)// tune this to your needs
const size = random(minSize, maxSize)
Object.assign(circle.style, {
width: `${size}vmin`,// vmin used instead px , but vh,vw aand vmax is also avalaible
height: `${size}vmin`,// vmin used instead px , but vh,vw aand vmax is also avalaible
left: `${random(0, 100)}%`,
top: `${random(0, 100)}%`
})
return circle
})
document.body.append(...elements)
body { overflow: hidden }
span {
background: black;
position: absolute;
border-radius: 50%;
opacity: .5
}
添加回答
舉報