Last active
December 1, 2017 14:20
-
-
Save isuke/381389166fc5b7f4590c251a4acc3e1c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 lang="pug"> | |
.flexible-image(:style="{width: width + unit, height: height + unit}") | |
img.image(:src="imageUrl" :style="{width: fixedWidth + unit, height: fixedHidth + unit}") | |
</template> | |
<script lang="coffee"> | |
export default | |
props: | |
imageUrl: | |
type: String | |
required: true | |
width: | |
type: Number | |
require: true | |
coerce: (val) -> parseFloat(val) | |
height: | |
type: Number | |
required: true | |
coerce: (val) -> parseFloat(val) | |
unit: | |
type: String | |
required: false | |
default: 'px' | |
computed: | |
aspectRatio: -> @width / @height | |
imageAspectRatio: -> @image.width / @image.height | |
image: -> | |
image = new Image() | |
image.src = @imageUrl | |
image | |
fixedWidth: -> | |
if @aspectRatio < @imageAspectRatio then null else @width | |
fixedHidth: -> | |
if @aspectRatio < @imageAspectRatio then @height else null | |
</script> | |
<style lang="scss" scoped> | |
.flexible-image { | |
position: relative; | |
overflow: hidden; | |
> .image { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
} | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment