Skip to content

Instantly share code, notes, and snippets.

@lidocaine
Created April 11, 2019 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidocaine/e277f4f34b51c975f8b37df3511c0192 to your computer and use it in GitHub Desktop.
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.
<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