Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 animate slide/hide navigation to influence other elements

Animated navigation slide to display/hide needs to push content down the page

I would like to have a smooth animated transition of 1 & 2, when 1 is triggered to display. I have accomplished this here: http://codepen.io/jacksonbeale/pen/epjcs using a negative top margin, but the problem is that the height of 1 will vary depending on the number of items in the navigation, so this will not work for me.

nav {
    margin-top:-95px;
    width:100%;
    box-sizing:border-box;
    z-index:5;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
    &.showmenu {
      margin-top:0;
    }
  }

My current project does not allow me to use jQuery, and any js needs to be written by another developer. I only have control over html & css. I seem to be running in to this issue a bit so it would be great if someone could offer a suggestion on a pure css method to accomplish what I need.

like image 741
Jackson Avatar asked Oct 16 '14 08:10

Jackson


People also ask

How do you transition display property?

If you even toggle the display property from none to block , your transition on other elements will not occur. To work around this, always allow the element to be display: block , but hide the element by adjusting any of these means: Set the height to 0 . Set the opacity to 0 .

Can you animate the display property?

One of the properties that cannot be animated is the display property.

How to store the animation info in HTML?

The animation info can be stored directly in HTML using classes and data attributes. Let’s take a look at the first animated element on my page.

How to style toggle button in CSS slide down menu?

To style the toggle button, first, we need to apply some basic styles like set to float right, some background color, width, and height. Then we will style the buttons by using :before property of CSS. Here is a complete set of CSS for the CSS Slide Down Menu. You can see we used CSS keyframes for a better result.

Should you use animated page elements in web design?

There’s a recurring trend of using animated page elements in web design at the moment — as you scroll down the page, items will naturally animate into view. These animations only happen one time, and they only begin once the element is within the browser viewport.

How to create a navigation menu in HTML?

First of all, We need to define the main div that holds all the other HTML code. Here we define SlideDownMenu The next step to define our menus and we follow the standard process by defining ul, li, and anchor link elements. We keep our navigation menu separately by define nav HTML element.


Video Answer


1 Answers

You could do this :

  • Add a container around the menu and the content
  • Position your content absolutely
  • Then, as the container only has the height of the menu (content is out of flow with position:absolute;) you can use translateY to togggle the menu in and out with translateY(-100%)/translateY(0)

DEMO

HTML :

<div class="test">
  <div><span class="menulink">Menu</span></div>
  <div class="content">
    <nav>
      <ul>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Three</li>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Three</li>
      </ul>
    </nav>
    <div>content</div>
  </div>
</div>

CSS : (sorry I'm not used to SCSS)

.test {
  width:400px;
  position:relative;
  > div, nav {
    border:3px solid #CCC;
  }
  > div:first-child {
    height:30px;
    z-index:10;
    background:teal;
    position:relative;
    z-index:1;
    span {
      float:right;
      display:table;
      height:100%;
      &:hover {
        cursor:pointer;
      }
    }
  }
  nav {
    width:100%;
    box-sizing:border-box;
    z-index:5;
    &.showmenu {
      margin-top:0;
    }
  }
}
.content > div{
  min-height:200px;
    position:absolute;
    left:-3px; top:100%;
    width:100%;
  border:3px solid #ccc;
}
.content{
  z-index:0;
  transform: translateY(-100%);
  -webkit-transform: translateY(-100%);
  -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}
.content.showmenu{
  transform: translateY(0);
  -webkit-transform: translateY(0);
}

jQuery :

$( ".menulink" ).click(function() {
  $( ".content" ).toggleClass( "showmenu" );
});
like image 193
web-tiki Avatar answered Oct 02 '22 23:10

web-tiki