Created
April 11, 2019 19:03
-
-
Save lidocaine/e277f4f34b51c975f8b37df3511c0192 to your computer and use it in GitHub Desktop.
Vue component to replace <img/> designed to be used when pulling images from Contentful but can be adapted as needed for various image sources.
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
<template> | |
<img | |
:id="id" | |
:src="src" | |
:srcset="srcSet" | |
:sizes="rSizes" | |
:alt="alt" | |
:class="classes" | |
/> | |
</template> | |
<script> | |
export default { | |
name: 'ResponsiveImg', | |
props: { | |
src: { | |
default: '', | |
required: true, | |
type: String | |
}, | |
pxWidths: { | |
default: () => { | |
return [1920, 1600, 1366, 1024, 768, 640] | |
}, | |
required: false, | |
type: Array | |
}, | |
alt: { | |
default: '', | |
required: false, | |
type: String | |
}, | |
rSizes: { | |
default: '100vw', | |
required: false, | |
type: String | |
}, | |
id: { | |
default: '', | |
required: false, | |
type: String | |
}, | |
classes: { | |
default: '', | |
required: false, | |
type: String | |
} | |
}, | |
computed: { | |
srcSet() { | |
return this.pxWidths.reduce((setStr, width) => { | |
return setStr += this.src+'?w='+width+' '+width+'w, ' | |
}, '') | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment