Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift down / up position fixed elements in HTML

I'm working on a HTML framework that most of it's pages constructed from two section. first section (TopPanel) is a sliding panel that could slide down or up (with jQuery as well). second section is the Main part of page that could contain any sort of HTML document.

<!doctype html>
<html>
<head>
  <!--Meta scripts & more-->
</head>
<body>
<div class="TopPanel">
  <!--Panel's Contents-->
</div>
<div class="Main">
  <!--Some standard HTML docs here-->
</div>
</body>
</html>

When the TopPanel is sliding down, all elements of the Main section must move down. But it's possible to exist some position:fixed element in the Main section. so it's clear that they won't move unless we gave them some margin-top: [$('.TopPanel').height()] px;. But it's not what I'm looking after!

I'm looking for a way to shift down and shift up all content of the Main section with a smooth effect and without changing of all elements attributes.

like image 441
DarkMaze Avatar asked Nov 19 '25 21:11

DarkMaze


2 Answers

Have you thought about using a CSS transform:translateY(20px) on the body tag? If you are using fixed position on the other element, it shouldn't actually affect it although I haven't tested that.

You can then use transitions to get the smooth movement you are after.

body{
  padding:10px;
  overflow:hidden;
  background:#fff;
  height:100%;
  transition:all .2s;
}

body.active{
  transform: translateY(60px);
}

Example:

http://codepen.io/EightArmsHQ/pen/gpwPPo

Lookout for this kind of stuff though : Positions fixed doesn't work when using -webkit-transform

like image 85
Djave Avatar answered Nov 21 '25 11:11

Djave


JSFIDDEL version
(UPDATED) Try this:

$('.main').click(function(){
    $('.main').toggleClass('toggle');
})
.main{
    width:20%;
    height: 10%;
    background-color: rgba(100,100,100,0.7);
    text-align: center;
    position: fixed;
    top: 12%;
    transition:all 1.0s
}
.top{
    width: 20%;
    height: 10%;
    text-align: center;
    background-color: rgba(300,50,50,0.7);
    position: absolute;
}
.toggle{
    transform: translateY(100px);
    transition:all 1.0s
}
<div class="content">
    <div class="top">
        Top
    </div>
    <div class="main">
        Main
    </div>
</div>

It would need some tweaking but it still does what you are looking for.

like image 28
ThermalCube Avatar answered Nov 21 '25 10:11

ThermalCube