Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Gabriel Ward
Gabriel Ward
20,222 Points

adding transition/animation to an overlay

I have dynamically added an overlay to the DOM of my page and have styled it in CSS. All the functionality of it works as I want it to and I have styled it in CSS.

The final thing I want to do is add some animation transition effects to the overlay. The effect I'm after can be seen here http://hugeinc.com/ If you click on the menu icon in the top right corner, and overlay eases onto the page.

I've tried adding some transition effects in CSS but haven't had any luck getting them to work. I'm not sure if I have to add anything in jQuery or not.

Here's my relevant code

css
#overlay {
  background:rgba(98,167,220,0.9);
  width:100%;
  height:100%;
  position:fixed;
  top:0;
  left:0;
  display:none;
  text-align:center;
}

#overlay ul {
    list-style: none;
    padding: 0;
    margin: 0 auto;
    display: inline-block;
    height: 100%;
    position: relative;
    text-align: center;
    position: relative;
    top: 15%;
    height: 60%;
    font-size: 54px;
}

#overlay ul li {
    display: block;
    height: 20%;
    height: calc(100% / 5);
    min-height: 54px;
}
#overlay ul li a {
    font-weight: 300;
    display: block;
    color: #fff;
}

#overlay ul li a:hover,
#overlay ul li a:focus {
    color: #e3fcb1;
}

#close-btn {
    position: absolute;
    top: 100px;
    right: 100px;
    cursor: pointer;
}
jQuery
var $overlay = $('<div id="overlay"><div id="close-btn"><img src="cross.png" height="25px" width="25px" alt="close" /></div></div>')
var $ul = $("<ul></ul>")
var $largeOverlay = $("<li id='largeOverlay'><a href='about.html'>About</a></li><")
var $largeOverlay2 = $("<li id='largeOverlay2'><a href='venue.html'>Venue</a></li>")
$($ul).append($largeOverlay).append($largeOverlay2);
$($overlay).append($ul);
$("body").append($overlay);
$("#imageGallery img").click(function(event) {
  event.preventDefault();
$overlay.show();
});

$('#close-btn').click(function() {
    $(overlay).hide();
  });

Thanks so much!

2 Answers

Raúl Barrera C.
Raúl Barrera C.
18,943 Points

Mmm, I use it with the default values. Here you can see more info: W3Schools http://matthewlein.com/experiments/easing.html

Gabriel Ward
Gabriel Ward
20,222 Points

Hmm I still can't get it to work.

Here's my jQuery

$("#imageGallery img").click(function(event) {
  event.preventDefault();
  // var href = $(this).attr("href");
  $overlay.fadeIn(1000, 'linear');
    // Animation complete
});

I'm not sure why it's not working.

Raúl Barrera C.
Raúl Barrera C.
18,943 Points

Have you tried with $overlay.fadeIn() / $overlay.fadeOut() instead?

Gabriel Ward
Gabriel Ward
20,222 Points

Ok cool. I've had a look at that right now. I can't quite get my head around the syntax exactly. Would you be able to help me out with an example? For instance, I need to put things like 'ease in' between in .fadeIn(parentheses) right?

Gabriel Ward
Gabriel Ward
20,222 Points

Rau,

Do you have any idea about running two different slideDown functions simultaneously?

I was trying to slideDown the overlay, and then slide the links in the overlay down at a slower rate. But it seems that if I have my code like this

$("#imageGallery img").click(function(event) {
  event.preventDefault();
  $overlay.slideDown(400, 'linear');
  $ul.slideDown(800, 'linear');
  //Animation complete
});

It only does one at a time. So the first time you click on the #imageGallery img,

$overlay.slideDown(400, 'linear');

is called.

Then when you click on #imageGallery img a second time,

$ul.slideDown(800, 'linear');

is called.