how to change element CSS position with animation - html

I want to change an HTML element position from static to fixed at top 50% and left 50% of the browser window but the code just leads to background-color change!
div {
width: 100px;
height: 100px;
background-color: red;
position: static;
top: auto;
left: auto;
animation-name: example;
animation-duration: 4s;
}
#keyframes example {
from {
background-color: red;
position: static;
top: auto;
left: auto;
}
to {
background-color: yellow;
position: fixed;
top: 50%;
left: 50%;
}
}

Short answer: You can't animate position.
Instead of applying position: fixed through keyframes try to add a class fixed or something via javascript.
An example: https://jsfiddle.net/6Ldqhoce/
Alternative you could move the element with transform: translate but it won't be fixed.

You cannot change position using CSS-animation because position is NOT an animatable property.
List of animatable properties.
However, this fiddle might help you.

I found the answer via 'Vangel Tzo' help :
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
#keyframes example {
from {
background-color: red;
}
to {
background-color: yellow;
transform: translate(50vw, 50vh);
}
}

You can't change css position value with css animation.
You can do this with jQuery
example is with hover, you can do according to your choice.
Jquery
With the use of "Css"
$(".div_name").hover(function(){
$(this).css("position", "static");
}, function(){
$(this).css("position", "fixed");
});
With the use of "Animate"
$(".div_name").hover(function(){
$(this).animate({"position":"static"}, 1000);
}, function(){
$(this).animate({"position":"fixed"}, 1000);
});

Related

initiate css transitions on page load

Im trying to understand CSS Transition - but I cant make it happen. I want to make a line go from 0 to 100% width with a small delay on pageload. I understand that CSs transition needs a trigger, and tried to read up to a jQuery or Vanilla Js solution, but get stuck.
Why is this ot working? Jsfiddle: https://jsfiddle.net/1fzzgnwy/
<div class="container">
<span class="h-line"></span>
</div>
.h-line {
position: absolute;
z-index: -1;
width: 0%;
height: 10px;
right: 0;
background-color: #000;
top: 400px;
transition: right 1.5s linear;
transition-delay: 2s;
}
.h-line.ready {
width: 100%;
background-color: #000;
}
.container {
height:100%;
width:100%;
}
$(function(){
$('.h-line').addClass('ready');
});
In your transition line, you are telling it to transition right, but that doesn't change in the added class. Your width does. So you need to transition the width like transition: width 1.5s linear;: JS Fiddle
And if you want it to expand left to right, remove right: 0: JS Fiddle
In your jsfiddle link (https://jsfiddle.net/1fzzgnwy/) there are some errors.
Here a simplified example of your code that work https://jsfiddle.net/50t3qwmL/ .
.h-line {
position: absolute;
top: 0;
right: 0;
width: 20px;
height: 20px;
background-color: #000;
transition: .3s;
transition-delay: 2s;
}
.h-line.ready {
width: 100%;
}

CSS3 animate only on hover on and off

I have an animation of a box, where it's width increases on hover:
JSFiddle: https://jsfiddle.net/hbpncv34/
HTML:
<div></div>
CSS:
div {
background-color: red;
height: 10vh;
width: 10vw;
}
div:hover {
animation: change_width 0.7s forwards;
}
#keyframes change_width {
from {
width: 10vw;
}
to {
width: 15vw;
}
}
Issue:
I also want the box to smoothly move back when I hover off of it.
There are two ways I could do this:
Method 1:
JSFiddle: https://jsfiddle.net/hbpncv34/1/
HTML:
<div></div>
CSS:
div {
background-color: red;
height: 10vh;
width: 10vw;
animation: change_width_back 0.7s forwards;
}
div:hover {
animation: change_width 0.7s forwards;
}
#keyframes change_width {
from {
width: 10vw;
}
to {
width: 15vw;
}
}
#keyframes change_width_back {
from {
width: 15vw;
}
to {
width: 10vw:
}
}
Issue:
The change_width_back animation also runs on page load.
Also, mousing on / off rapidly is not smooth like it is with transition, so:
Method 2:
JSFiddle: https://jsfiddle.net/hbpncv34/2/
HTML:
<div></div>
CSS:
div {
background-color: red;
height: 10vh;
width: 10vw;
transition: width 0.7s;
}
div:hover {
width: 15vw;
}
Issue:
The animation also runs on zooming in and out.
I can fix this by using px or % instead of vw for my widths, but it's crucial in my actual issue that I keep it as viewport units.
So, is there any way to play an animation on hover on and off, and only hover on and off?
Since I don't know Javascript, please keep answers HTML / CSS only. If JS is absolutely needed, make it simple enough to just copy + paste with minimal editing.
Thanks for any help.
You need "ready" state flag in order to accomplish this:
$(function(){
$("div").attr("ready",true);
});
div {
background-color: red;
height: 10vh;
width: 10vw;
}
div[ready] {
transition: width 0.7s;
}
div[ready]:hover {
width: 15vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>.</div>
There may be a better way, but with very little js combined to your Method 1, you can do this:
<div onmouseover="this.classList.add('animate')"></div>
And in your css:
div {
background-color: red;
height: 10vh;
width: 10vw;
}
div.animate
{
animation: change_width_back 0.7s forwards;
}
https://jsfiddle.net/vhyfewnt/
EDIT
First solution is meant to work with animate, not with transition. There's a way to make transition work, but it needs a little more javascript.
<body onresize="document.getElementsByTagName('div')[0].classList.add('zoom'); setTimeout(function(e){document.getElementsByTagName('div')[0].classList.remove('zoom')}, 100)">
<div></div>
</body>
div {
background-color: red;
height: 10vh;
width: 10vw;
}
div:not(.zoom)
{
transition: width 0.7s;
}
div:hover {
width: 15vw;
}
https://jsfiddle.net/cdj7L5r3/
2nd EDIT:
To apply to more elements, you can add the class to body (or any ancestor) and change the rule. Like this:
<body onresize="document.body.classList.add('zoom'); setTimeout(function(e){document.body.classList.remove('zoom')}, 100)">
<div></div>
<div></div>
<div></div>
<div></div>
<section></section>
</body>
div, section{
background-color: red;
height: 10vh;
width: 10vw;
}
body:not(.zoom) div, body:not(.zoom) section
{
transition: width 0.7s;
}
div:hover, section:hover {
width: 15vw;
}
https://jsfiddle.net/cdj7L5r3/1/
RE: animate on zoom in/out, I don't think you're going to find a way around the animation issue with the CSS transition solution, because zooming changes the viewport size, and your units are in terms of the viewport. So when the viewport changes, those units have to be recalculated/repainted and that change is animated, as specified in the transition property.
If you could provide some background on the use case for this effect, we might be able to help think of another way to solve this problem.
If you are willing to add a litter js code maybe this help you, the key is that classes have the animation behavior, when the animation ends you remove the classes, so there is no transition when the page is zoom or loaded
var element = $("div");
element.bind("mouseover", function(){
$(this).removeClass('sh');
$(this).addClass('lg');
});
element.bind("mouseout", function(){
$(this).addClass('sh');
$(this).removeClass('lg');
});
/*this is for remove the class when animation ends*/
element.bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
$(this).removeClass('sh');
});
div {
background-color: red;
height: 10vh;
width: 10vw;
}
.lg{
width: 15vw;
-webkit-transition: .3s;
}
.sh{
width: 10vw;
-webkit-transition: .3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
Here a jsfiddle example to play with

CSS Transition and Auto position

I have a div .box with which has absolute position set at the bottom of the parent div. It does not have the top position set because the height differs for different .box divs. On hover over the parent div, I want to change its top position to 0 with the transition effect.
In the following code transition does not work if I do not define the top position by default. I have tried using top: auto and it still does not work. How can I use transition without defining the top position.
Here's my code:
HTML:
<div class="wrap">
<div class="box">
Box
</div>
</div>
CSS:
.wrap{
position: relative;
height: 200px;
width: 200px;
background: green;
}
.box{
background: yellow;
position: absolute;
bottom: 0;
left: 0;
transition: all 0.9s ease 0s;
top: 50%; /*== does not work without it ==*/
}
.wrap:hover .box{
top: 0;
bottom: 0;
}
Fiddle: http://jsfiddle.net/5hs09apn/
A slight different approach than using top: http://jsfiddle.net/5hs09apn/2/
set the height for the box, and on hover set it to 100%; let the bottom be zero, so you don't even need to use top
.box{
background: yellow;
position: absolute;
bottom: 0;
left: 0;
height:20px;
transition: all 1s ease 0s;
}
.wrap:hover .box{
height:100%;
bottom: 0;
}
.wrap {
position: relative;
height: 200px;
width: 200px;
background: green;
}
.box {
background: yellow;
position: absolute;
bottom: 0;
left: 0;
height: 20px;
transition: all 1s ease 0s;
}
.wrap:hover .box {
height: 100%;
bottom: 0;
}
<div class="wrap">
<div class="box">
Box
</div>
</div>
Add Top property in JQuery getting the current value of the Top of the .box using position().
This will be dynamic allocation of Top and will not effect your condition of varying .box height.
While doing this, you will have to add the hover effect in the JQuery part too, since the Top will be defined here and CSS wont know what to do with the hover.
Check the Fiddle here
This is the JQuery function added:
$(document).ready(function(){
var x = $('.box').position();
var myTop = x.top;
$('.box').css("top",myTop+"px");
$('.box').css("transition","all 0.9s ease 0s");
$('.wrap').bind('mouseover',function(){
$('.box').css("top","0px");
$('.box').css("bottom","0px");
});
$('.wrap').bind('mouseout',function(){
$('.box').css("top",myTop+"px");
$('.box').css("bottom","0px");
});
});
Hope this gives you what you need.
You can use:
.wrap:hover .box{
margin-bottom: 100%;
}
And try with different percentages until you get one you like. It's crude but I think it can work.

Keep child position the same after parent moved with CSS transform

Is it possible to take a child outside of the flow of its transformed parent? Simple example below:
HTML:
<div class="parent">
<div class="child"></div>
</div>
Move parent
CSS:
.parent {
background: red;
height: 200px;
position: relative;
transition: 0.5s ease-in-out;
width: 200px;
}
.child {
background: black;
height: 100px;
left: 0;
top: 0;
position: absolute;
width: 100px;
}
.moved {
transform: translateX(100px);
}
Javascript:
$('#button').on('click', function(e) {
e.preventDefault();
console.log('test');
$('.parent').toggleClass('moved');
});
JSFiddle: http://jsfiddle.net/ous3s873/1/
Very simple stuff. Basically, the parent (red box) is being moved 100px to the right using a CSS transform. My desired output is that the child (black box) would stay in the same place as the parent moves behind it.
One option is adding a negative translateX to the child element in order to make it stay at its place:
Updated Demo
.moved .child {
transform: translateX(-100px);
}

How to set original image for animate background image in css

css code:
body {
margin: 0;
padding: 0;
}
#slideshow {
position: relative;
overflow: hidden;
height: 100px;
}
#animate-area {
height: 100%;
width: 2538px;
position: absolute;
left: 0;
top: 0;
background-image: url('http://s30.postimg.org/qnju89rkx/banner.png');
-webkit-animation: animatedBackground 40s linear infinite;
}
/* Put your css in here */
#keyframes animatedBackground {
from { left: 0; }
to { left: -1269px; }
}
#-webkit-keyframes animatedBackground {
from { left: 0; }
to { left: -1269px; }
}
#-moz-keyframes animatedBackground {
from { left: 0; }
to { left: -1269px; }
}
Jsfiddle: http://jsfiddle.net/cz04c4nx/2/
Now animation is working smoothly, but image size not show as original.
Now set my original image for animate?..for that,when i adjust background-position or width and height, animation is not working properly, ..
can anybody help me with us, Thank you.
is that what u want ?
http://jsfiddle.net/cz04c4nx/7/
if so u have to add another parent to handle the slider
<div id="slideshow">
<div id="fixme">
<div id='animate-area'>
</div>
</div>
</div>
First, your image doesn't look bigger because the original file is only 100px tall. If you want to make it taller without losing quality, you'll need a larger source image for your script.
Here's a working fiddle for the method described below.
CSS Tricks has a good tutorial for getting good full-screen (or full-div) images, and you can use that method here.
First, set the height of your #animate-area
#slideshow {
position: relative;
overflow: hidden;
height: 400px; /* Or whatever height you want */
}
Then, add repeat-x to the background tag as well as adding the background-size attribute:
#animate-area {
background: url('http://s30.postimg.org/qnju89rkx/banner.png') repeat-x;
background-size:cover;
.... /* Other CSS */
}
But, like I said, until you have a higher-res image, this won't look so great.