Id like to easy flip a text upside down, like this page is doing: http://papertiger.com/
I want to do this as easy as possible, using
-webkit-transition: 0s;
-webkit-transform rotateX(180deg)
This is my code:
HTML:
<script>
$(document).ready(function() {
Js.init();
});
</script>
<body>
<div id="flipbox">
<p id="text">Front</p>
</div>
</body>
JAVASCRIPT:
var Js = {
init: function() {
var up=true;
//do this every 5 seconds
var i = window.setInterval( function(){
up=!up;
Js.rotate(up);
//this will run just once after the timeout
var j = window.setTimeout( function(){
//Js.switchText(up);
}, 1000 );
}, 5000 );
},
rotate: function(up){
if(up){
$("#flipbox").removeClass('rotateDown');
$("#flipbox").addClass('rotateUp');
}else{
$("#flibox").removeClass('rotateUp');
$("#flipbox").addClass('rotateDown');
}
},
switchText: function(up) {
if(up){
$("#text").text('front');
}else{
$("#text").text('back');
}
}
}
CSS:
#flipbox{
color: green;
display: block;
font-size: 70px;
perspective: 800px;
perspective-origin: 50% 100px;
}
.rotateDown{
transition: 2s;
-webkit-transform: rotateX(180deg);
}
.rotateUp{
transition: 2s;
-webkit-transform: rotateX(0deg);
}
.upSideDown{
-webkit-transition: 0s;
-webkit-transform: rotate(180deg);
}
As it is now, I get the text "First" to rotate as I want to. To switch the text and rotate it in Y 180 degrees (to make it readable, since its now upside down), does not work, and is why the line is commented.
So, I think I need to have the text rotated by both X and Y 180 degress, how do I do that? rotate3d(1,1,0,180deg) does not work like that. If I write rotateX followed by rotateY, the first one gets overwritten.
...or is the jQuery plugin flippy somthing I should look for?
Ive tried to make a jsfiddle. It did not work, but can be found here: http://jsfiddle.net/EdNVw/
Thanks in advace for any advice!
Related
I have made a group of elements and set a hover for their class
.cards:hover {
transition: 0.2s;
transform: translate( 0px, -50px);
height: 180px;
width: 120px;
background-size: 120px 180px;
}
There are 10 elements in the class and i have a JS file that onclick singles out the element and i want it to spin so i write the JS and i tell it to add a transform after the click like
document.getElementById(idTag).style.transition = "3s ease";
document.getElementById(idTag).style.transform = "rotate(270deg)";
but it doesn't rotate it. Instead it goes directly to 270 degrees in the shortest possible path. If i remove the transform from the hover then it rotates like normal but if i have a transform on hover it doesn't work. Is there a conflict or something with the hover effect ?
In order to transition vertical displacement (translate) and rotation separately you should probably use a different way of moving the element upwards. Using the top property and a relative position. Then you could have the following css code...
.cards {
position: relative;
transition: top 0.2s, transform 3s ease;
height: 180px;
width: 120px;
background-size: 120px 180px;
top: 0px;
transform: rotate(0deg);
}
.cards:hover {
top: -50px;
}
.cards.rotate {
transform: rotate(270deg);
}
And the following code within your click listener to add the rotate class for rotation after the click.
document.getElementById(idTag).classList.add('rotate')
Now the cards should move up on hover, but rotate on click with separate speeds.
function drawCard() {
var Deck = document.getElementsByClassName("cards");
var idTag = this.id;
document.getElementById(idTag).classList.remove("cards");
document.getElementById(idTag).classList.add("draw-card");
for (i = 0; i < Deck.length; i++) {
Deck[i].style.display = "none";
}
document.getElementById(idTag).style.transition = "2s";
document.getElementById(idTag).style.left = "100px";
document.getElementById(idTag).style.animation = "rotate 2s linear forwards";
on click I separate the picked card by changing the class and then cycle through the rest of the class to make the rest of the cards dissapear. After that I add the rotate animation with the key frames in CSS
#keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
This is my first question on SO, I hope I'm doing it well.
My goal is to have a "magical rune effect", where each letter of a text "floats around" its original position, as if it was suspended in mid-air on a magic parchment of some sort.
To put it simply, it will be used for a game. I know how to make something "float at random" on a page, like a hot air balloon; but this isn't what I'm trying to do : I want the letters to move around their original position.
So far, I've tried something (you can check my fiddle at https://jsfiddle.net/3as4omj2/ ), but I'm running into problems.
(don't worry about the default font and the ugly aqua background, it's used for positionning)
function float(element, range, speed) {
var position = $(element).offset();
$(element).attr( 'original_x', position.left);
$(element).attr( 'original_y', position.top);
$(element).attr( 'range', range );
$(element).attr( 'speed', speed );
drift(element);
}
function drift(element) {
var max = Number.parseInt($(element).attr('range'));
var speed = Number.parseInt($(element).attr('speed'));
var pos_x = Number.parseInt($(element).attr('original_x'));
var pos_y = Number.parseInt($(element).attr('original_y'));
var drift_x = max/2 - Math.floor(Math.random()*max);
var drift_y = max/2 - Math.floor(Math.random()*max);
var final_x = pos_x + drift_x;
var final_y = pos_y + drift_y;
var total_wait = Math.sqrt(drift_x*drift_x+drift_y*drift_y)*speed;
$(element).animate({
left : final_x+"px",
top : final_y+"px"
}, total_wait, /*"linear",*/ function(){
setTimeout(function () {
drift(element);
}, Math.abs(total_wait-Math.floor(Math.random()*150)));
});
}
$( "#go" ).click(function() {
float($("#t"),50, 10);
float($("#e"),50, 10);
float($("#s"),50, 10);
float($("#s"),50, 10);
float($("#t2"),50, 10);
})
Here's my problems and questions so far :
I can't line up my letters to form a word (like, "TEST" is seen vertically, I'd love to see it as an horizontal "TEST"); ideally, using spans, so I can dynamically add a word or remove it without creating dozens of elements.
The text moves bananas... and I don't seem to be understanding why. :(
I'd love to be able to "move the original position" along, so that I can animate the letters further (moving the general text left to right, for example).
Ultimately, is there a way to optimize the size of the font to the user's display ?
Can you guys give me some advice ?
Thank you in advance.
You could try using CSS3 animation instead, setting custom animation delay for every letter using :nth-of-type() selector. To better understand all the animation properties, see this docs. All the rest is the matter of tweaking translate values.
If you are determined enough, creating custom #keyframes for each letter is also an option.
.runes span {
position: relative;
display: inline-block;
padding: 10px;
background: aqua;
animation-duration: 2s;
animation-name: float;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-timing-function: ease-in-out;
}
.runes span:nth-of-type(1) {
animation-delay: .3s;
}
.runes span:nth-of-type(2) {
animation-delay: .4s;
}
.runes span:nth-of-type(3) {
animation-delay: .5s;
}
.runes span:nth-of-type(4) {
animation-delay: .7s;
}
#keyframes float {
0% {
transform: translate(0, 0);
}
25% {
transform: translate(8px, 0);
}
50% {
transform: translate(0, 8px);
}
75% {
transform: translate(5px, 5px);
}
}
<section class="runes">
<span>T</span>
<span>E</span>
<span>S</span>
<span>T</span>
</section>
I am trying to make a zoom in effect on an image on hover and on click, the image should stay enlarged. I have a set of 5 images, and everytime I hover on each one of them, they zoom in properly and as I move my mouse, they zoom out just like I want them.
The problem is when I try to select an image. When I do this, the image should get slightly larger and stay as is and not decrease in size and go back to normal. This is where I think is the conflict between the hover and click function. Also, when I select another image, the other image which was previously zoomed in(selected) should get back to it's original size but this is also not working now due to this conflict.
I have done some searching around S.O but can't find anything that's helping me.
Below is my code:
//Item hover
$(".anItem").hover(function() {
$(this).addClass('transition');
}, function() {
$(this).removeClass('transition');
});
//Select Item
$(".anItem").click(function(){
$(".anItem").each(function() {
$(this).removeClass("selectedItem");
});
if($(this).hasClass("selectedItem")){
$(this).removeClass("selectedItem");
itemColor = "";
}else{
itemColor = $(this).data("color");
$(this).addClass("selectedItem");
$("#oneBtn").show();
}
});
CSS
.anItem{
width: 90%;
height: auto;
display: block;
margin: 0 auto;
cursor: pointer;
}
.transition {
-webkit-transform: scale(1.3);
-moz-transform: scale(1.3);
-o-transform: scale(1.3);
transform: scale(1.3);
}
.anItem{
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
-ms-transition: all .4s ease-in-out;
}
/*
.anItem:hover{
background-color: #ddd;
opacity: 0.7;
}
*/
.selectedItem{
width: 230px;
height: auto;
}
You wants something like this ?
See this fiddle
I've simplified your JS code like this :
$(function(){
//Select Item
$(".anItem").click(function(){
$('div').find('img').addClass('anItem');
$(this).removeClass("anItem");
});
});
The :hover part is done with CSS only in my code.
I want to apply a random animation on my slideshow image. First, I tried adding an animation such as scale but it didn't work as I wanted it to.
Things I want to fix:
Smoothness on fadein
Random animation (can be anything at this point, I just want to see how it's done)
Fiddle: http://jsfiddle.net/jzhang172/e7cLtsg9/1/
$(function() {
$('img').hide();
function anim() {
$("#wrap img").first().appendTo('#wrap').fadeOut(3500).addClass('transition').addClass('scaleme');
$("#wrap img").first().fadeIn(3500).removeClass('scaleme');
setTimeout(anim, 3700);
}
anim();
});
body,
html {
margin: 0;
padding: 0;
background: black;
}
#wrap img {
position: absolute;
top: 0;
display: none;
width: 100%;
height: 100%;
}
.transition {
transition: 10s;
}
.scaleme {
transition: 10s;
transform: scale(1.3);
}
.box {
height: 300px;
width: 500px;
position: relative;
overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
<div id="wrap">
<img src="http://elegantthemes.com/preview/InStyle/wp-content/uploads/2008/11/s-1.jpg" />
<img src="http://elegantthemes.com/preview/InStyle/wp-content/uploads/2008/11/s-5.jpg" />
<img src="http://elegantthemes.com/preview/InStyle/wp-content/uploads/2008/11/s-3.jpg" />
</div>
</div>
Here is a sample using CSS animations and jQuery (for achieving the randomness of animations). If you don't wish to use CSS animations and want to stick to transitions + jQuery effects (like fadeIn), you can still adapt this code to support it because the base idea will still remain the same. I am not too comfortable with jQuery effects and have hence stuck to using CSS animations.
Below is an overview of how it is being done (refer inline comments for more details):
Inside a wrapper there are a group of images that are part of the slide-show (like in your demo).
Using CSS #keyframes, a list of animations (one of which would be used randomly) is created in addition to the default fade-in-out animation. This list is also maintained in an array variable (in JS for picking up a random one from the list).
On load, the default fade-in-out animation and one random animation is added to the 1st element.
An animationend event handler is added to all of the images. This event handler will be triggered when the animation on an element ends. When this is triggered, animation on the current element is removed and the default fade-in-out + a random animation is added to the next element.
The animations are added using inline styles because if we add multiple CSS classes each with one different animation, then the animation in the latest class will override the others (that is, they will not happen together).
A loop effect is achieved by checking if the current element has any other img sibling elements. If there are none, the animation is added back to the 1st element.
$(window).load(function() {
$img = $('img'); // the images
var anim = ['zoom', 'shrink', 'move-down-up', 'move-right-left']; // the list of random animations
var rand = Math.floor(Math.random() * 4) + 1; // random number
$img.each(function() { // attach event handler for each image
$(this).on('animationend', function(e) { // when animation on one image has ended
if (e.originalEvent.animationName == 'fade-in-out') { // check the animation's name
rand = Math.floor(Math.random() * 4) + 1; // get a random number
$(this).css('animation-name', 'none'); // remove animation on current element
if ($(this).next('img').length > 0) // if there is a next sibling
$(this).next('img').css('animation-name', 'fade-in-out, ' + anim[rand - 1]); // add animation on next sibling
else
$img.eq(0).css('animation-name', 'fade-in-out, ' + anim[rand - 1]); // else add animation on first image (loop)
}
});
});
$img.eq(0).css('animation-name', 'fade-in-out, ' + anim[rand - 1]); //add animation to 1st element on load
})
#wrapper {
height: 250px;
width: 300px;
position: relative;
}
img {
position: absolute;
z-index: 1;
bottom: 20px;
left: 10px;
opacity: 0;
transform-origin: left top; /* to be on the safe side */
animation-duration: 3s; /* increase only if you want duration to be longer */
animation-fill-mode: backwards; /* fill mode - better to not change */
animation-iteration-count: 1; /* no. of iterations - don't change */
animation-timing-function: ease; /* better to leave as-is but can be changed */
}
#keyframes fade-in-out {
0%, 100% {
opacity: 0;
}
33.33%, 66.66% { /* duration is 3s, so fade-in at 1s, stay till 2s, fade-out from 2s */
opacity: 1;
}
}
#keyframes zoom {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
}
#keyframes shrink {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(.5);
}
}
#keyframes move-down-up {
0%, 100% {
transform: translateY(0px);
}
50% {
transform: translateY(50px);
}
}
#keyframes move-right-left {
0%, 100% {
transform: translateX(0px);
}
50% {
transform: translateX(50px);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<img src="https://placehold.it/200/000000/ffffff" />
<img src="https://placehold.it/200/ff0000/ffffff" />
<img src="https://placehold.it/200/00ff00/ffffff" />
<img src="https://placehold.it/200/0000ff/ffffff" />
</div>
Hoping for help in getting this javascript I found online to work. I am wanting to make the boxes rotate each time the button clicks. Currently it only rotates every second click.
<style>
.rotate {
animation-name: example;
animation-duration: 1s;
}
#-webkit-keyframes example {
from { transform: rotateZ(0deg);}
to { transform: rotateZ(360deg);}
}
</style>
<script type="text/javascript">
function rotateBox(){
var rotated = false;
var div = document.getElementById('box'),
deg = rotated ? 0 : 66;
document.getElementById('box').classList.toggle('rotate');
rotated = !rotated;
}
</script>
<body>
<div id="box" class="boxes" ></div>
<button class="button" onclick=" rotateBox()" >Rotate</button>
</body>
This will add the "rotate" class when clicked, and set a timeout to remove the class after the animation has finished.
function rotateBox() {
document.getElementById('box').classList.add('rotate');
window.setTimeout(function() {document.getElementById('box').classList.remove('rotate');}, 1000);
}
div {
width: 100px; height: 100px; border: 1px solid red;
}
.rotate {
animation-name: example;
animation-duration: 1s;
}
#-webkit-keyframes example {
from { transform: rotateZ(0deg);}
to { transform: rotateZ(360deg);}
}
<div id="box" class="boxes"></div>
<button class="button" onclick="rotateBox()">Rotate</button>
I think you want rotated to be outside of the function:
var rotated = false;
function rotateBox(){
var div = document.getElementById('box'),
deg = rotated ? 0 : 66;
document.getElementById('box').classList.toggle('rotate');
rotated = !rotated;
}