0
$('.greenBox').hover(function(){
    $(this).addClass('expanded');
    $(this).removeClass('contracted');
}, function(){
    $(this).removeClass('expanded');
    $(this).addClass('contracted');
}); // end of hover state for green box

Wondering if there is a way to implement a transition with the above jquery logic? Between the adding or removing of the class.

I have tried the below, but it did not work: (It is a simple div small height > large height toggle)

.expanded {
    height: auto;
    // to make the box move up add back the bottom 300px
    // bottom: 300px;
    background: linear-gradient(#812990, #9e248e);
    -webkit-transition-timing-function: linear; 
    transition-timing-function: linear; 
}

also:

.contracted {
    height: 100px;
}
7
  • That entirely depends on what styles those classes apply. Depending on that, you could theoretically do this in CSS alone. Commented Oct 20, 2015 at 16:14
  • Toggling of heights. Commented Oct 20, 2015 at 16:14
  • What does the .contracted class look like? Commented Oct 20, 2015 at 16:15
  • 1
    Headsup: you can NOT transition to/from auto values in CSS. Also you need both states for a transition to work and you haven't set a transition property. More info Commented Oct 20, 2015 at 16:16
  • You need to specify the transition setting in your CSS. Although, as @LuudJacobs says you cannot transition from/to height: auto so you would need to deal in concrete sizes. Here's a working CSS-only demo: jsfiddle.net/76n6Luc8 Commented Oct 20, 2015 at 16:18

2 Answers 2

2

Apply the transition to your .greenbox class, rather than either of the 2 you're adding & removing:

.greenbox{
    transition:height .5s linear;
}

Having said that, you cannot transition to or from an auto value in CSS so, the trick you would want to use here is to instead set the max-height of the expanded class to a value greater than the height its contents can ever be and transition that, instead.

.greenbox{
    transition:max-height .5s linear;
}
.contracted{
    max-height:100px;
}
.expanded{
    max-height:1000px;/* adjust to suit your content */
}

Depending on the value you set for the larger max-height, you may need to tweak the timing of the transition to improve how it looks.

Incidentally, you could achieve this entirely with CSS with absolutely no need for any JavaScript, like so:

.greenbox{
    max-height:100px;
    transition:max-height .5s linear;
}
.greenbox:hover{
    max-height:1000px;/* adjust to suit your content */
}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems like you can simplify the JavaScript and only have one class that you toggle. You can't use CSS to animate to auto, because it's not aware of what that means, but you can trick it with max-height... You'll have to just be arbitrary and get closest to the common size. Otherwise you'll have to use jQuery animate (or velocity) and query the height so you know what integer to animate to - and then leave the CSS transition out.

jQuery

$('.greenBox').hover( function() {
    $(this).addClass('expanded');
}, function() {
    $(this).removeClass('expanded');
});

CSS

.greenbox {
  height: 864572px;
  max-height: 100px;
  transition: .2s; 
}

.greenbox.expanded {
  max-height: 500px
}

jsFiddle

3 Comments

You have to have a height: Npx; as well. With width you can add a width 100%, but height is a little whacky - because the DOM doesn't usually know what 100% means... 100% of what? etc.
Where do I add height height: Npx; ?
That may have been confusing. I meant N to be a placeholder. In my example I have defined the height of the box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.