Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created May 13, 2019 13:58
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 nfreear/9155ffd08e21faaba3e05d72e2011da7 to your computer and use it in GitHub Desktop.
Save nfreear/9155ffd08e21faaba3e05d72e2011da7 to your computer and use it in GitHub Desktop.
Horizontal flip animation with Vue.js and CSS
<!doctype html> <title> *CSS / Vue Flip </title>
<h1> CSS / Vue horizontal flip </h1>
<style>
/* entire container, keeps perspective */
.flip-container {
perspective: 1000px;
}
/* flip the pane when hovered */
.flip-container.do-flip .flipper,
.X--flip-container:hover .flipper,
.X--flip-container.hover .flipper {
transform: rotateY( 180deg );
}
.flip-container,
.front,
.back {
width: 320px;
height: 480px;
}
/* flip speed goes here */
.flipper {
transition: 0.6s;
transform-style: preserve-3d;
position: relative;
}
/* hide back of pane during swap */
.front,
.back {
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
padding: .7rem;
border: 1px solid #ccc;
}
/* front pane, placed above back */
.front {
z-index: 2;
/* for firefox 31 */
transform: rotateY(0deg);
}
/* back, initially hidden pane */
.back {
transform: rotateY(180deg);
}
</style>
<div id="app">
<horiz-flip v-bind:transition-sec="2" />
</div>
<template id="horiz-flip-template">
<div class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper" v-bind:style="'transition: ' + transitionSec + 's;'">
<div class="front">
<!-- front content -->
<slot name="front">
<button class="btn-flip"> Flip to back </button>
<h2> Front </h2>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</slot>
</div>
<div class="back">
<!-- back content -->
<slot name="back">
<button class="btn-flip"> Flip to front </button>
<h2> Back </h2>
<p>Prow scuttle parrel provost Sail ho shrouds spirits boom mizzenmast yardarm. Pinnace holystone mizzenmast quarter crow's nest nipperkin grog yardarm hempen halter furl. Swab barque interloper chantey doubloon starboard grog black jack gangway rutters.
<p>Deadlights jack lad schooner scallywag dance the hempen jig carouser broadside cable strike colors. Bring a spring upon her cable holystone blow the man down spanker Shiver me timbers to go on account lookout wherry doubloon chase. Belay yo-ho-ho keelhaul squiffy black spot yardarm spyglass sheet transom heave to.
</slot>
</div>
</div>
</div>
</template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script id="HorizFlip.vue">
Vue.component('HorizFlip', {
template: '#horiz-flip-template',
props: {
transitionSec: {
type: Number,
default: 0.6, // Seconds.
}
},
mounted () {
const FLIP_CTR = document.querySelector('.flip-container');
FLIP_CTR.querySelectorAll('.btn-flip').forEach(elem => {
elem.addEventListener('click', event => {
console.warn(elem.innerText, event)
FLIP_CTR.classList.toggle('do-flip')
})
})
console.warn('HorizFlip.mounted', FLIP_CTR)
},
})
</script>
<script>
new Vue({ el: '#app', })
</script>
<pre>
NDF, 13-May-2019.
* https://davidwalsh.name/css-flip
* https://en.wikipedia.org/wiki/Lorem_ipsum
* https://shopify.co.uk/partners/blog/79940998-15-funny-lorem-ipsum-generators-to-shake-up-your-design-mockups
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment