Skip to content

Commit a9bb43b

Browse files
committed
refactor: improve image scale calculation and event handling for better performance and clarity in step 1
1 parent 3937d43 commit a9bb43b

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/pages/steps/1-single.astro

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ import Layout from '@/layouts/Layout.astro'
6565
* 480px - 1600px: Linear scale from 0.5 to 0.33
6666
* >= 1600px: 0.33
6767
*/
68-
function calculateImageScale() {
69-
const width = window.innerWidth
70-
const scale = Math.max(0.33, Math.min(0.5, 0.5 - (width - 480) * 0.17 / 1120))
71-
return scale.toString()
68+
function calculateImageScale(width: number) {
69+
return Math.max(0.33, Math.min(0.5, 0.5 - (width - 480) * 0.17 / 1120))
7270
}
7371

74-
function updateImageScale() {
72+
function setupImageScale() {
73+
const width = window.innerWidth
74+
const scale = calculateImageScale(width)
7575
const imageStack = document.querySelector('.image-stack') as HTMLElement
76-
imageStack?.style.setProperty('--image-scale', calculateImageScale())
76+
77+
imageStack?.style.setProperty('--image-scale', scale.toString())
7778
}
7879

7980
function handleClick(event: MouseEvent) {
@@ -83,7 +84,8 @@ function handleClick(event: MouseEvent) {
8384
target.closest('.image-item')?.classList.toggle('zoomed')
8485
}
8586

86-
updateImageScale()
87-
window.addEventListener('resize', updateImageScale)
87+
document.addEventListener('astro:page-load', () => setupImageScale())
88+
window.addEventListener('resize', setupImageScale)
8889
document.addEventListener('click', handleClick)
90+
setupImageScale()
8991
</script>

0 commit comments

Comments
 (0)