I'm trying to do a transition from the center to left and reduce the height of an image. The height transition is working fine, but the margin just teleports to the left instead of animating.
this is my code:
#logo_img {
height: 55px;
width: 55px;
background-color: red;
margin-left: auto;
margin-right: auto;
display: block;
transition: all 1s ease-in-out;
}
#logo_img.tiny {
height:45px;
margin-left: 0;
}
JS:
$('#logo_img').addClass('tiny');
working example: http://jsfiddle.net/v0w6s3ms/1/
any help?
Specifies a fixed left margin in px, pt, cm, etc. Default value is 0px. Negative values are allowed. Read about length units Sets this property to its default value.
The transition effect will start when the specified CSS property (width) changes value. Now, let us specify a new value for the width property when a user mouses over the <div> element:
Special welcome offer: get $100 of free credit . The transition property is a shorthand property used to represent up to four transition-related longhand properties: transition properties allow elements to change values over a specified duration, animating the property changes, rather than having them occur immediately.
That is, when the styles are changed (e.g. on hover on), they properties will transition, and when the styles change back (e.g. on hover off) they will transition. For example, the following demo transitions on hover, but not on hover off: This browser support data is from Caniuse, which has more detail.
You can't animate auto
property instead try something like this
$(function() {
setTimeout(function() {
$('#logo_img').addClass('tiny');
}, 1000);
});
#logo_img {
height: 55px;
width: 55px;
background-color: red;
margin-left: calc(50% - 55px);
margin-right: auto;
display: block;
transition: all 1s ease-in-out;
}
#logo_img.tiny {
height: 45px;
margin-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="logo_img"></section>
You want to transition from "margin-left:auto" to "margin-left:0". Auto is not a defined value, thats why it can't be decreased to zero. Set margin-left: 50% instead "auto".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With