-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use the Image component so we avoid content redraw
- Loading branch information
1 parent
15bae95
commit 0a6d49f
Showing
67 changed files
with
279 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
<script setup> | ||
import { computed } from 'vue' | ||
import { ref, onMounted, computed } from 'vue' | ||
const props = defineProps({ | ||
width: Number, | ||
height: Number, | ||
width: String, | ||
height: String, | ||
alt: String, | ||
src: String, | ||
size: String, // WIDTHxHEIGHT as a string | ||
}) | ||
let widthSize, heightSize | ||
const imageIsSmallerThanParent = ref(false) | ||
let widthSize = ref(null) | ||
let heightSize = ref(null) | ||
if (props.size) { | ||
const sizes = props.size.toString().split('x') | ||
widthSize = sizes[0] | ||
heightSize = sizes[1] | ||
widthSize.value = sizes[0] | ||
heightSize.value = sizes[1] | ||
} | ||
const width = computed(() => parseInt(widthSize) || props.width || 0) | ||
const height = computed(() => parseInt(heightSize) || props.height || 0) | ||
const width = computed(() => parseInt(widthSize.value) || parseInt(props.width) || 0) | ||
const height = computed(() => parseInt(heightSize.value) || parseInt(props.height) || 0) | ||
const alt = computed(() => props.alt || 'Avo') | ||
const src = computed(() => props.src || '') | ||
const style = computed(() => `padding-bottom: calc(${height.value}/${width.value} * 100%);`) | ||
const style = computed(() => { | ||
if (imageIsSmallerThanParent.value) { | ||
return `width: ${width.value}px; height: ${height.value}px;` | ||
} | ||
return `padding-bottom: calc(${height.value}/${width.value} * 100%);` | ||
}) | ||
const parent = ref(null) | ||
onMounted(() => { | ||
checkParentWidth() | ||
}) | ||
const checkParentWidth = () => { | ||
const parentWidth = parent.value.parentNode.getBoundingClientRect().width | ||
const imageWidth = width | ||
imageIsSmallerThanParent.value = parentWidth > imageWidth.value | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="aspect-ratio-box" :style="style"> | ||
<div class="aspect-ratio-box" :style="style" ref="parent"> | ||
<img :src="src" :alt="alt" loading="lazy" class="aspect-ratio-box-inside"> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.