I'd like to make the height of a div animate depending on the dynamic ad that is served inside.
<div>
// Dynamic Content
</div>
The div has a minimum height of 90px, however if the ad served into the div has a height of 300px, I want the div to animate (css3 transition preferably) to the new height of 300px.
Is this possible?
At the moment the div just jumps to the new height, and the main content of the page jumps down with it. I'd just like this to be a smooth transition.
You should use max-height instead of min-height. Use max-height value t something your height will never reach, or if you want to strict hieght to some value.
And use transition with max-height and ease-in.
See JSFiddle provided in another answer by Chris Jordan.
<div class="imagediv">
// Dynamic Content
</div>
//CSS
#imageDiv {
max-height: 500px;
transition: max-height 0.25s ease-in;
}
If you can catch the "ad loaded" event and add a class to your div accordingly you can try setting the max-height of the div instead of height.
function addElement() {
var initialDiv = document.getElementsByClassName('yourDiv')[0];
var newdiv = document.createElement('div');
newdiv.setAttribute('style','height: 300px;');
initialDiv.classList.add('loaded');
newdiv.innerHTML = '<img src="http://placehold.it/300x300"/>';
initialDiv.appendChild(newdiv);
}
setTimeout(function(){
addElement();
}, 1000);
.yourDiv {
min-height: 90px;
width: 300px;
background-color: grey;
max-height: 90px;
transition: max-height 500ms ease-in;
overflow: hidden;
}
.yourDiv.loaded {
max-height: 300px;
transition: max-height 500ms ease-in;
}
<div class="yourDiv">
</div>
You can set the max-height to 300px or more, it doesn't really matter.
EDIT:
Based on your comment. There actually is an event you can listen to in GPT. You can simply add it when pushing the ads like this:
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
// slot has been rendered - do stuff
});
Now I'm not sure how you've done yours but I think you can implement this easily to your existing code.
A working example: https://jsfiddle.net/thepio/7tfdxw8f/
You try:
div{
height:auto
}
Create a new div dynamically with default display:none property. Append the ad to this new div and then append the new div to your original div. And you can show the new div with effect. Have a look below
$('button').click(function(){
$('.content').append('<div id="new" style="display:none;">1<br>1<br>3<br>4<br>1<br>3<br>4<br>1<br>3<br>4</div>');
$('#new').show(1000);
});
.content{
min-height:90px;
border:1px solid #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"></div>
<button type="button">Click</button>
Related
https://codepen.io/fuzzalicious/pen/YzYwozv
.faq-content {
transition: max-height 2s ease-in-out;
overflow: hidden;
}
.dropdown-off {
max-height: 0;
}
.dropdown-on {
max-height: 1000px;
}
I have an animated dropdown that uses max-height, but I can only get it to work with static height, I would like it work with any content so you don't have to know the height in advance.
Now I'm setting max-height to 1000px which means longer lists would be cut off and short ones take long to animate. Is there any way to use something like max-height: auto or calculate the content height?
I am building a single page website which shows a vector graphic of 80% screen width on the 'start screen'. Once the user scrolls down, the graphic transitions to a navigation bar at the top of the page and shall receive a hight of 50px. The transition from large to small dimensions should be animated with CSS3 transitions.
However, CSS transistions seem to not work when scaling an element from a percent or auto value to a fixed pixel value and vice versa. I've made a jsfiddle to demonstrate the effect. While the height of the div transitions well, the width is not animated at all.
Using pixel widths for the initial size of the image is not an option for responsive design reasons. The code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
html, body{
background-color: #010101;
height: 100%;
}
.navbar--logo-item{
background-color: #fff;
height: 10px;
width: 80%
-moz-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.navbar--logo-item.small{
height: 50px;
width: 200px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#toggle').click(function(){
$('.navbar--logo-item').toggleClass('small');
});
});
</script>
</head>
<body>
<div class="navbar--logo-item"></div>
<button id="toggle">
Toggle Logo
</button>
</body>
</html>
In modern browsers, that can be solved using calc.
You can transition a calc value as long as it is homogeneous.
Your case can be expressed in an homogeneous way as follows
.navbar--logo-item{
width: calc(80% + 0px);
}
.navbar--logo-item.small{
width: calc(0% + 200px);
}
Notice that the 2 calc are similar (they sum a percentage and a pixel value), but at the same time the result is the same that you already have
fiddle
Another usual way to get this is to set a max-width on the higher posible original value, something like
.navbar--logo-item{
width: auto;
max-width: 1000px; /* you don't need to set an accurate value */
}
.navbar--logo-item.small{
max-width: 200px; /* no need to set width */
}
If you set the width of your logo to that of its' immediate parent then everything is in pixels and the transition will take place as you want.
$(document).ready(function(){
var logo = $('.navbar--logo-item');
function setWidthtoParent(target) {
var parentWidth = target.parent().width();
target.css('width', parentWidth);
}
setWidthtoParent(logo);
$('#toggle').click(function(){
logo.toggleClass('small');
});
});
html, body{
background-color: #010101;
height: 100%;
}
.navbar--logo-item{
background-color: #fff;
height: 10px;
-moz-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
.navbar--logo-item.small{
height: 50px;
width: 200px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="navbar--logo-item"></div>
<button id="toggle">
Toggle Logo
</button>
</body>
This is not ideal as it necessitates adding an !important declaration to over-ride the applied inline style. You also run the risk of the browser window being resized before the transition takes place. A better solution would be to use a consistent unit (perhaps transitioning the width of a parent div whilst your SVG remains at 100%, like this).
I'm trying to set a transition-delay to the overflow property of body when a div is clicked by adding a class to the body as follows:
$("div").click(function(){
$("body").addClass("no_overflow");
});
div{
background:lime;
height:2000px;
}
.no_overflow{
overflow:hidden;
}
body{
overflow:auto;
transition: overflow 0 2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>I'm div</div>
However, this doesn't seem to work (there's no delay). Am I doing anything wrong here?
I know this can be achieved by using setTimeout function, but was wondering why can't this be achieved using css transitions? Are there any specific style properties to which css transitions can be applied?
There are many properties that can't be transitioned. overflow is among them; the render engine has no idea how to transition between "hidden" and "shown", because those are binary options, not intervals. This is the same reason why you can't transition between display: none; and display: block; (for example): there are no in-between phases to use as transitions.
You can see a list of properties you can animate here on Mozilla Developer Network.
You can simulate a delay with animation:
$("div").click(function() {
$("body").addClass("no_overflow");
});
div {
background: lime;
height: 2000px;
}
.no_overflow {
overflow: hidden;
/* persist overflow value from animation */
animation: 7s delay-overflow;
}
body {
overflow: auto;
}
#keyframes delay-overflow {
from { overflow: auto; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>I'm div</div>
You'll have to apply a separate animation to .body if you want a delay on removeClass, and also to take care that the two animations don't overlap or they'll cancel each other out.
overflow isn't CSS animatable property. You can see full list of animatable CSS properties there.
In case someone is looking at the answer, like I was, for a way to animate the cropping of an element which requires overflowing - here is the solution that worked for me: the clip-path css property which is animatable and very versatile.
Here is a cool tool to play around with, in order to get the proper start / end values for an animation: https://bennettfeely.com/clippy/.
Dmitry's answer should be the only accepted answer, as it is a pure CSS solution applying delay to "non-animatable" properties. However it's worth to mention, that the CSS rule applying animation should be "triggerable" each time when it is needed.
For instance, the following code does not work:
#keyframes show-overflow {
from { overflow: hidden; }
}
.hideable, .overlay {
font-size: 36px;
height: 50px;
}
.hideable {
transition: height 2s;
overflow: visible;
animation: show-overflow 2s; /* this line should be in separate "triggerable" CSS rule to work */
}
.hideable.hidden {
height: 0;
overflow: hidden;
}
<button onclick="document.getElementById('hideable').classList.toggle('hidden')">
Clik HERE to hide/show the text below
</button>
<div id='hideable' class='hideable'>
This is the text to hide and show.
</div>
<div class='overlay'>
This is overlaying text
</div>
But after moving the marked property to a separate CSS rule, everything works as expected:
#keyframes show-overflow {
from { overflow: hidden; }
}
.hideable, .overlay {
font-size: 36px;
height: 50px;
}
.hideable {
transition: height 2s;
overflow: visible;
}
.hideable:not(.hidden) {
animation: show-overflow 2s; /* now this works! */
}
.hideable.hidden {
height: 0;
overflow: hidden;
}
<button onclick="document.getElementById('hideable').classList.toggle('hidden')">
Clik HERE to hide/show the text below
</button>
<div id='hideable' class='hideable'>
This is the text to hide and show.
</div>
<div class='overlay'>
This is overlaying text
</div>
It makes sense that you can't transition between binary attributes for example overflow: hidden; and overflow: visible but it would have been really nice if instead of "transitioning" then it would be like (in js pseudo code:
setTimeout("applyOverflowVisible()", transitionTime);
But of course you can do this yourself in JavaScript but then you are splitting the code between places and it can make it difficult to understand by someone else. I guess using things like React helps but even there I would want to avoid mixing css into the js.
I have been experimenting and trying but have not been able to achieve the following. I'm sure the solution is simple, but I haven't hit it yet.
Let's say I want to animate an element (eg. div) when I apply a class (eg. active). And I want to reverse the animation when I remove the class (or toggle with another).
The properties I would like animate are scale (transform) and opacity.
Also, when entering the page, the element will not have any class, and should snap to its state, and not animate. It should only animate when explicitely adding or removing the class.
jsfiddle: http://jsfiddle.net/bertvan/9r98w/
HTML:
<div id="the-div"></div>
Trigger
JS:
$(function(){
$("a").click(function(){
$("#the-div").toggleClass("active");
});
});
CSS:
#the-div{
width: 200px;
height: 200px;
background-image: url("http://placeimg.com/200/200/any");
-webkit-transform: scale(0.7);
opacity: 0.5;
}
#the-div.active{
/* animate scale & opacity */
-webkit-transform: scale(1);
opacity: 1;
}
You are missing a transition property on the div selector:
running demo
code added:
#the-div{
transition: all 2s;
}
This is an example of a code to toggle class for changing size of a div with the transition-animation.
The HTML:
input-button with id="setRemoveClassBtn"
a div-tag with id="div1"
You will need a CSS-class-definition (notice that this is with the moz-prefix made to work in Firefox):
div{
width: 150px;
height: 150px;
background-color: rgb(250, 250, 150);
-moz-transition: width 5s, height 5s;
}
div.bigSizeDivs{
width: 250px;
height: 250px;
}
You will have to use javascript/jQuery to add/remove class. Here is an example with jQuery
$("#setRemoveClassBtn").click(function(){
$("#div1").toggleClass("bigSizeDivs");
});
I am trying to develop a small module (like the collapse component of twitter bootstrap).
I don't know how to treat the content of the div which is growing up/down, it could take me many descriptions, nothing's better than an example: Collapse deployment.
You can see a div.container which is the block destined to grow up/down.
Here i declare height: 50px to illustrate a state of the deployment.
Is there a way to hide the content (here a part of the text) that is out of the height of a parent div ?
I like the idea that we can access a content only by deploying another one, but i don't really don't understand how to make it happen properly in CSS.
Like this? JSFiddle example: http://jsfiddle.net/SW86B/1/
Updated CSS
.header {
background-color: green;
height:20%;
}
.container {
background-color: red;
height: 0;
overflow: hidden;
-webkit-transition: height 0.2s ease;
-moz-transition: height 0.2s ease;
transition: height 0.2s ease;
}
.container.open { height: 50px;}
p { margin: 0; }
Use jQuery to toggle states
$('button').on('click', function(event){
$('.container').toggleClass('open');
});
I am not sure that i understand what you are trying but you can use
overflow:hidden;
Demo here - http://jsfiddle.net/JjPcy/1/
Set the div.container's overflow css properoty to hidden.
div.container { overflow: hidden; }
Also make a class for instance called auto-width that has auto width:
div.auto-width { width: auto !important; }
Then use jQuery to toggle the class and reveal the data inside the container:
$('div.header button').click(function() {
$('div.container').toggleClass('auto-width');
});
Here's the demo: http://jsfiddle.net/VE9WR/3/
It could be done in so many ways. it depends on what you're looking for ;)