I have a simple card flip animation from this example: https://desandro.github.io/3dtransforms/docs/card-flip.html
I would like to turn this (or something similar to this) into a slideshow mechanic, where I don't just flip front and back, but multiple elements, together with a back and forth button.
How would this be possible without asynchronous loading the next or previous element?
http://codepen.io/anon/pen/qbvvyp
See the example HTML element:
<section class="container">
<div id="card">
<figure class="front">1</figure>
<figure class="back">2</figure>
<!--
How To Make this a Slideshow?
<figure class="back">3</figure>
<figure class="back">4</figure>
-->
</div>
</section>
I did it by using 3 separate CSS classes:
turnedLeft (turned left, -180 degrees on the Y axis)
turnedRight (turned right, 180 degrees on the Y axis)
active (facing the user, 0 degrees on the Y axis)
and 2 functions:
prevSlide: adds turnedRight to the active slide, and makes the previous element active.
nextSlide: adds turnedLeft to the active slide, and makes the next element active.
This is dynamic, and will work for any number of elements inside the "card" div!
https://jsfiddle.net/gmsitter/vuob9bnx/
HTML:
<section class="container">
<div id="card">
<figure class="active">1</figure>
<figure>2</figure>
<figure>3</figure>
<figure>4</figure>
<figure>5</figure>
</div>
</section>
<button id="previous">Previous</button>
<button id="next">Next</button>
CSS:
figure {
margin: 0px;
font-family: sans-serif;
}
.container {
width: 200px;
height: 260px;
position: relative;
margin: 0 auto 40px;
border: 1px solid #CCC;
-webkit-perspective: 800px;
-moz-perspective: 800px;
-o-perspective: 800px;
perspective: 800px;
}
#card {
width: 100%;
height: 100%;
position: absolute;
}
#card figure {
display: block;
height: 100%;
width: 100%;
line-height: 260px;
color: white;
text-align: center;
font-weight: bold;
font-size: 140px;
position: absolute;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-o-backface-visibility: hidden;
backface-visibility: hidden;
background: blue;
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg );
-webkit-transition: -webkit-transform 1s;
-moz-transition: -moz-transform 1s;
-o-transition: -o-transform 1s;
transition: transform 1s;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
-o-transform-style: preserve-3d;
transform-style: preserve-3d;
}
#card .turnedLeft {
-webkit-transform: rotateY( -180deg );
-moz-transform: rotateY( -180deg );
-o-transform: rotateY( -180deg );
transform: rotateY( -180deg );
}
#card .turnedRight {
-webkit-transform: rotateY( 180deg );
-moz-transform: rotateY( 180deg );
-o-transform: rotateY( 180deg );
transform: rotateY( 180deg );
}
#card .active {
-webkit-transform: rotateY( 0deg );
-moz-transform: rotateY( 0deg );
-o-transform: rotateY( 0deg );
transform: rotateY( 0deg );
}
JS:
var init = function() {
var r = 1;
var slides = $("#card").children();
$('#next').click(function(){
nextSlide();
});
$('#previous').click(function(){
prevSlide();
});
function nextSlide(){
if(r<slides.length){
var activeSlide = $(".active");
activeSlide.addClass("turnedLeft");
activeSlide.removeClass("active");
activeSlide.next().addClass("active");
r++
}
}
function prevSlide(){
if(r>1){
var activeSlide = $(".active");
activeSlide.addClass("turnedRight");
activeSlide.removeClass("active");
activeSlide.prev().addClass("active");
r--
}
}
};
window.addEventListener('DOMContentLoaded', init, false);
Related
I am very new at this. I have three boxes with information on both sides. They currently rotate on hover but I would like them to only rotate on click. How do I accomplish this?
.flip3D {
width: 240px;
height: 200px;
margin: 10px;
float: left;
}
.flip3D>.front {
position: absolute;
-webkit-transform: perspective( 600px) rotateY( 0deg);
transform: perspective( 600px) rotateY( 0deg);
background: #FC0;
width: 240px;
height: 200px;
border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D>.back {
position: absolute;
-webkit-transform: perspective( 600px) rotateY( 180deg);
transform: perspective( 600px) rotateY( 180deg);
background: #80BFFF;
width: 240px;
height: 200px;
border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D:hover>.front {
-webkit-transform: perspective( 600px) rotateY( -180deg);
transform: perspective( 600px) rotateY( -180deg);
}
.flip3D:hover>.back {
-webkit-transform: perspective( 600px) rotateY( 0deg);
transform: perspective( 600px) rotateY( 0deg);
}
<div class="flip3D">
<div class="back">Box 1 - Back</div>
<div class="front">Box 1 - Front</div>
</div>
This would require a javascript click event listener. You will also need to update your CSS to change with the newly created class (.active) instead of on :hover.
var box = document.querySelector('.flip3D');
box.addEventListener("click", function() {
if (box.classList.contains('active')) {
box.classList.remove('active');
} else {
box.classList.add('active');
}
});
.flip3D {
width:240px;
height:200px;
margin:10px;
float:left;
}
.flip3D .front{
position:absolute;
-webkit-transform: perspective( 600px ) rotateY( 0deg );
transform: perspective( 600px ) rotateY( 0deg );
background:#FC0; width:240px; height:200px; border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D .back{
position:absolute;
-webkit-transform: perspective( 600px ) rotateY( 180deg );
transform: perspective( 600px ) rotateY( 180deg );
background: #80BFFF; width:240px; height:200px; border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D.active .front{
-webkit-transform: perspective( 600px ) rotateY( -180deg );
transform: perspective( 600px ) rotateY( -180deg );
}
.flip3D.active .back{
-webkit-transform: perspective( 600px ) rotateY( 0deg );
transform: perspective( 600px ) rotateY( 0deg );
}
<div class="flip3D">
<div class="back">Box 1 - Back</div>
<div class="front">Box 1 - Front</div>
</div>
You can use :active pseudo class. However, active is triggered on mousedown and untriggered on mouseup. Not sure this fits your requirements. If not, you may need to resort to a js solution with event handlers.
.flip3D {
width: 240px;
height: 200px;
margin: 10px;
float: left;
}
.flip3D>.front {
position: absolute;
-webkit-transform: perspective( 600px) rotateY( 0deg);
transform: perspective( 600px) rotateY( 0deg);
background: #FC0;
width: 240px;
height: 200px;
border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D>.back {
position: absolute;
-webkit-transform: perspective( 600px) rotateY( 180deg);
transform: perspective( 600px) rotateY( 180deg);
background: #80BFFF;
width: 240px;
height: 200px;
border-radius: 7px;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: -webkit-transform .5s linear 0s;
transition: transform .5s linear 0s;
}
.flip3D:active>.front {
-webkit-transform: perspective( 600px) rotateY( -180deg);
transform: perspective( 600px) rotateY( -180deg);
}
.flip3D:active>.back {
-webkit-transform: perspective( 600px) rotateY( 0deg);
transform: perspective( 600px) rotateY( 0deg);
}
<div class="flip3D">
<div class="back">Box 1 - Back</div>
<div class="front">Box 1 - Front</div>
</div>
This question already has answers here:
How to use jQuery to wait for the end of CSS3 transitions?
(6 answers)
Closed 6 years ago.
I've got an element that starts out with no transforms applied to it.
After an event, the following transform is applied.
transform: rotateY( -180deg );
After another event, another transform is applied.
transform: rotateZ( -15deg ) rotateY( -180deg ) translateX( 1000px ) translateY( -600px );
Everything works fine as long as the 1st transform finishes before the 2nd. However, when it doesn't the 2nd transform will make the element do a horizontal and vertical 360.
How can I prevent the card from doing any 360 ever? and/or wait until the 1st transition is completely finished before continuing.
Full code:
HTML
<div class="studyCard">
<div class="card flip">
<input class="currentCardKey" type="hidden" value="">
<input class="currentCardPlacement" type="hidden" value="">
<div class="cardFront">
<div class="cardSub">
<p style="max-height:100px;">
<span class="frontText">FrontText</span>
</p>
</div>
</div>
<div class="cardBack">
<div class="cardSub">
<p style="max-height:100px;">
<span class="backText">BackText</span>
</p>
</div>
</div>
</div>
</div>
CSS
.studyCardContainer{
position: absolute;
width: 100%;
height: 100%;
padding: 70px 0px 90px;
z-index: 10;
}
.studyCard{
margin:0 5px;
position: relative;
height: 100%;
cursor: pointer;
perspective: 2000px;
}
.card{
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
transition-duration: .40s;
/*-o-transition-duration: .70s;
-webkit-transition-duration: .70s;
-moz-transition-duration: .70s;*/
}
.card .cardFront,.card .cardBack{
border: 1px solid #888;
background-color: #FFF;
box-shadow: 0px 2px 12px 0px rgba(0,0,0,.2);
margin: 0;
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
display: table;
table-layout: fixed;
overflow: auto;
}
/*.card .cardFront {}*/
.card.flip{
transform: rotateY( 0deg );
-o-transform: rotateY( 0deg );
-webkit-transform: rotateY( 0deg );
-moz-transform: rotateY( 0deg );
}
.card.flipped, .card .cardBack {
transform: rotateY( -180deg );
-o-transform: rotateY( -180deg );
-webkit-transform: rotateY( -180deg );
-moz-transform: rotateY( -180deg );
;
}
.card.flip,.card.flipped{
}
.card.flip.sling{
transform: rotateZ( -15deg ) rotateY( 0deg ) translateX( -1000px ) translateY( -600px );
/* -o-transform: rotateZ( -15deg ) rotateY( 0deg ) translateX( -1000px ) translateY( -600px );
-webkit-transform: rotateZ( -15deg ) rotateY( 0deg ) translateX( -1000px ) translateY( -600px );
-moz-transform: rotateZ( -15deg ) rotateY( 0deg ) translateX( -1000px ) translateY( -600px );*/
}
.card.flipped.sling{
transform: rotateZ( -15deg ) rotateY( -180deg ) translateX( 1000px ) translateY( -600px );
/* -o-transform: rotateZ( -15deg ) rotateY( -180deg ) translateX( 1000px ) translateY( -600px );
-webkit-transform: rotateZ( -15deg ) rotateY( -180deg ) translateX( 1000px ) translateY( -600px );
-moz-transform: rotateZ( -15deg ) rotateY( -180deg ) translateX( 1000px ) translateY( -600px );*/
}
.card.sling{
/*opacity: 0;*/
/*display: none;*/
transition-duration: .4s;
/* -o-transition-duration: .70s;
-webkit-transition-duration: .70s;
-moz-transition-duration: .70s;*/
}
Here was the solution I went with:
function flipCard(sideToSwitchTo){
if(sideToSwitchTo != "front" && sideToSwitchTo != "back"){
//decide for self
if($('.revealAnswerButton').is(":visible")){
sideToSwitchTo = "back";
}else{
sideToSwitchTo = "front";
}
}
if(sideToSwitchTo == "back"){
$('.card:first').removeClass('flip').addClass("flipped");
}else{
$('.card:first').removeClass("flipped").addClass('flip');
}
$('.card:first').addClass('flipTransition');
$('.card:first').on('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd',function() {
$(this).removeClass('flipTransition');
});
}
function slingCardAway(){
if($('.card:first').hasClass('flipTransition')){
$('.card:first').on('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd',function() {
$(this).addClass('sling');
$(this).on('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd',function() {
$(this).remove();
});
});
}else{
$('.card:first').addClass('sling');
$('.card.sling').on('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd',function() {
$(this).remove();
});
}
}
As noted here, you could achieve it using the following jQuery function:
$("#someSelector").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });
This, in this provided case, will wait for the '#someSelector' CSS animation to finish and then execute whatever piece of code you wish.
This is a possible duplicate to this and this.
I've created a cube in css with perspevtive and rotateX/Y/X and with some translate I put the screen/camera into this cube (the goal is to create a skybox).
While it's ok in Chrome, Firefox seems to have some problem displaying background-image on some faces when thoses faces are to inclined.
Here is a codepen for example : http://codepen.io/poolboy/full/MaNgVK/
Html
<section class="cube-container">
<div id="cube">
<figure class="front"></figure>
<figure class="back"></figure>
<figure class="right"></figure>
<figure class="left"></figure>
<figure class="top"></figure>
<figure class="bottom"></figure>
</div>
</section>
Css
body {
overflow: hidden;
}
.cube-container {
width: 1024px;
height: 1024px;
position: relative;
perspective: 1000px;
margin: auto;
}
#cube {
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
/* animation: anim 20s linear infinite; */
}
#cube figure {
margin: 0;
width: 1024px;
height: 1024px;
display: block;
position: absolute;
/* border: 1px solid rgba(0,0,0,0); */
backface-visibility: hidden;
}
#cube .front { background: url(http://www.poolboy.fr/images/front.jpg) ; transform: translateZ( 511px
) rotateY( 180deg ); }
#cube .back { background: url(http://www.poolboy.fr/images/back.jpg) ; transform: rotateX( 180deg ) translateZ( 511px ) rotateZ(180deg) rotateY( 180deg ); }
#cube .right { background: url(http://www.poolboy.fr/images/right.jpg) ; transform: rotateY( 90deg ) translateZ( 511px ) rotateY( 180deg ); }
#cube .left { background: url(http://www.poolboy.fr/images/left.jpg) ; transform: rotateY( -90deg ) translateZ( 511px ) rotateY( 180deg ); }
#cube .top { background: url(http://www.poolboy.fr/images/top.jpg) ; transform: rotateX( 90deg ) translateZ( 511px ) rotateY( 180deg ); }
#cube .bottom { background: url(http://www.poolboy.fr/images/bottom.jpg) ; transform: rotateX( -90deg ) translateZ( 511px ) rotateY( 180deg ); }
#keyframes anim {
from {
transform: translateZ(511px) rotateY(0deg);
}
to {
transform: translateZ(511px) rotateY(360deg);
}
}
#cube {
/* transition: transform 1s; */
transform: translateZ(512px) rotateY(10deg);
}
Is there a way to ensure that image are correctly displayed in Firefox ?
Thanks
Firefox doesn't use transform-style, it uses -moz-transform-style. Try to replace that in your CSS code. Make sure transform-style: preserve-3d; becomes -moz-transform-style: preserve-3d;
Here is a list of Firefox extensions for CSS, use it well.
From Comment:
I took a deeper look at this, and found no issues with Hardware acceleration and that the -moz- prefix doesn't do anything, really. However, I did find that increasing the perspective did have an impact (A quite massive one on my part) on how the cube rendered. For my display (1920 x 1080), the sweet-spot of not losing quality and making it actually work was to set perspective: 1000px; to perspective: 1250px; I hope this helps, cause it certainly fixed it for me.
i have try to make flip effect through css and html only.its working fine on Firefox but not working on chrome.I have also try webkit prefix but not working can anybody help me.its urgent.here the code
.flip3D{
width: 240px;
height: 200px;
margin: 10 px;
float: left;
}
.flip3D > .front{
position: absolute;
-webkit-transform: perspective( 600px ) rotateY( 0deg);
transform: perspective( 600px ) rotateY( 0deg);
background-color: #D8D2D2;
width: 180px;
height: 200px;
border-radius: 7px;
color: black;
-webkit-backface-visibility:hidden;
backface-visibility:hidden;
transition: -webkit-transform 0.5s ;linear 0s;
transition: transform 0.5s ;linear 0s;
}
.flip3D > .back{
position: absolute;
-webkit-transform: perspective( 600px ) rotateY( 180deg);
transform:perspective( 600px ) rotateY( 180deg);
background-color:#30D2FF;
width: 180px;
height: 200px;
border-radius: 7px;
color: white;
-webkit-backface-visibility:hidden;
backface-visibility:hidden;
transition: -webkit-transform 0.5s ;linear 0s;
transition: transform 0.5s ;linear 0s;
}
.flip3D:hover > .front{
transform:perspective( 600px ) rotateY( -180deg);
}
.flip3D:hover > .back{
transform:perspective( 600px ) rotateY( 0deg);
}
html code:
<div class="flip3D">
<div class="back">box 1 back</div>
<div class="front">box 1 front</div>``
</div>
You need to add the -webkit- prefix to everything. After adding it to the below code it seems to work fine,
.flip3D:hover > .front{
transform:perspective( 600px ) rotateY( -180deg);
-webkit-transform:perspective( 600px ) rotateY( -180deg);
}
.flip3D:hover > .back{
transform:perspective( 600px ) rotateY( 0deg);
-webkit-transform:perspective( 600px ) rotateY( 0deg);
}
JSFIDDLE
Note: I messed up one of the styles it is now fixed.
Here's your answer man. Add -webkit prefix to transform property. You missed it twice at the end for those hover styles. And check the fiddle for your reference. JS FIDDLE DEMO
.flip3D:hover > .front{
-webkit-transform:perspective( 600px ) rotateY( -180deg);
}
.flip3D:hover > .back{
-webkit-transform:perspective( 600px ) rotateY( 0deg);
}
I have a weird bug with safari...
I have 3 divs with captions, which are hidden with rotateX(-91deg). On :hover the caption comes down via rotateX(0deg). It works perfectly in Chrome etc. When i hover in safari it works only on the first div. When i hover on the 2. or the 3. the animation doesn't work.
Check out my demo: http://jsbin.com/aseced/28/edit
HTML:
<article class="tile2x1">
<section class="caption"></section>
</article>
<article class="tile2x1">
<section class="caption"></section>
</article>
<article class="tile2x1">
<section class="caption"></section>
</article>
CSS:
[class*="tile"] > section.caption {
padding-top: 20px;
width: 100%;
background-color: blue;
height: 100px;
transform-origin: top;
-o-transform-origin: top;
-moz-transform-origin: top;
-webkit-transform-origin: top;
transform: perspective( 600px ) rotateX( -91deg );
-o-transform: perspective( 600px ) rotateX( -91deg );
-moz-transform: perspective( 600px ) rotateX( -91deg );
-webkit-transform: perspective( 600px ) rotateX( -91deg );
transition: all .25s linear;
-o-transition: all .25s linear;
-moz-transition: all .25s linear;
-webkit-transition: all .25s linear;
backface-visibility: hidden;
-webkit-backface-visibility: hidden; /* Chrome and Safari */
-moz-backface-visibility: hidden; /* Firefox */
-ms-backface-visibility: hidden; /* Internet Explorer */
}
[class*="tile"]:hover > section.caption {
transform: perspective( 600px ) rotateX( 0deg );
-o-transform: perspective( 600px ) rotateX( 0deg );
-moz-transform: perspective( 600px ) rotateX( 0deg );
-webkit-transform: perspective( 600px ) rotateX( 0deg );
}
Thanks for any help.
TJL