Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move an element to just below a fixed header

I am trying to move each <li> to top of the screen on onClick(), and its working using scrollIntoView(); but because my header is fixed small part of the <li> goes below the header. How can I scroll the <li> to just below the header.

HTML

    <header></header>
<div id="wrapper">
   <ul>
       <li id='li1' onClick="moveup(1)"> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
         <li id='li2' onClick='moveUp(2)'> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
       <li id='li3' onClick='moveUp(3)'> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
        <li id='li4' onClick='moveUp(4)'> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
        <li id='li1' onClick='moveUp(1)'> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
       <li id='li5' onClick='moveUp(5)'> blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
           blah blah blah blah blah <br/>
       </li>
       </ul>   
</div> <!--end wrapper-->

css

header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    height: 50px;
    background-color: red;
}

#wrapper {
    width: 1000px;
    background-color: blue;
    height: 100%;
    margin: 0 auto;
}

#col1 {
 background-color: green;
    height: 100%;
    width: 80%;
    margin: 0 auto;
}

ul {                    
    margin: 0 auto;
    width: 500px;
    display: block;
}

li {
     display: block;
    background-color: yellow;
    height: 500px;
    width: 500px;  
    margin-bottom: 5px;
}

JAVASCRIPT

function moveup(x){
   document.getElementById('li'+x).scrollIntoView();
}

http://jsfiddle.net/Jn74M/2/

Thanks.

like image 760
user1846348 Avatar asked Nov 03 '22 19:11

user1846348


1 Answers

After scrollIntoView(), push it down by 50px, using something like this:

scrollBy(0, -50);
like image 105
Tushar Roy Avatar answered Nov 09 '22 09:11

Tushar Roy