我怎么能有一个位置:在滚动事件后修复?

时间:2014-05-24 19:49:51

标签: javascript jquery html css css3

帮助我。我想在向下滚动500像素后将导航菜单移到顶部的固定位置。如果有人能给我我需要的java脚本,我将非常感激。在此先感谢:)

这是代码,如果有帮助

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="icon" type="image/png" href="img/favicon.png" />
    <title></title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<style>
body {
    margin: 0;
    font-family: Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif;
    padding: 0;
    width: 100%;
    font-size: 18px;

}

a {
    text-decoration: none;
}

h1 {
}

h2 {
}

hr {
    border: 0;
    height: 1px;
    background: #333;
    background-image: -webkit-linear-gradient(left, #ccc, #333, #ccc); 
    background-image:    -moz-linear-gradient(left, #ccc, #333, #ccc); 
    background-image:     -ms-linear-gradient(left, #ccc, #333, #ccc); 
    background-image:      -o-linear-gradient(left, #ccc, #333, #ccc); 
}

/* ==============================Header================= */

.cover {
    background: url(http://oi62.tinypic.com/a2ac8n.jpg) top center no-repeat;
    background-size: cover;
    width: 100%;
    height: 500px;
    margin-bottom: -400px;
    position:fixed;

}

.menu,
.menu ul,
.menu li,
.menu a {
    padding: 0;
    border: none;
    outline: none;  
}

.menu {
    text-shadow:0px 0px 10px #fff;
    font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
    margin: 400px 0px 0px 0px;
    position: absolute; 
    width: 100%;
    height: 100px;
    -webkit-box-shadow: 0px 0px 20px 1px #666;
    -moz-box-shadow: 0px 0px 20px 1px #666; 
    box-shadow: 0px 0px 20px 1px #666;
    background: #FFF;
}

.menuWrap {
    width: 1000px;
    margin: auto;
}

.menu li {
    list-style:none;
    float: left;
    width: 25%;
    text-align: center;
}

.menu li a {
    display: block;
    font-weight: bold;
    color: #000;
    font-size: 20px;
    line-height: 100px;
}

.menu li:hover > a,
.menu .active { 
    background-color:#09D5D5;
    color: #FFF;
    -webkit-transition: ease-in .2s;
    -moz-transition: ease-in .2s;
    -o-transition: ease-in .2s;
    -ms-transition: ease-in .2s;
    transition: ease-in .2s;
}

/* ========================Content======================== */

.mainContent{
    background: #F8F8F8;
    width: 100%;
    position: absolute;
    margin-top:500px;
}

.contentWrapper {
    margin: auto;
    width: 1000px;
    padding-top: 50px;
}

.content {
    margin-bottom: 50px;
    padding: 40px;
    border: #999 1px solid;
    line-height: 25px;
    color: #4D4D4D;
    border-radius: 10px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    background-color: #FFF;
    box-shadow: 0px 0px 1px 1px #e1e1e1 inset, 0px 23px 30px -33px #4D4D4D;
}

</style>
<body class="body">
    <div class="cover"></div>
    <header class="mainHeader">
    <nav>
            <ul class="menu"><div class="menuWrap">
                <li><a href="#" class="active">Page1</a></li>
                <li><a href="#">Page2</a></li>
                <li><a href="#">Page3</a></li>
                <li><a href="#">Page4</a></li>
            </div></ul>
    </nav>
    </header>

    <div class="mainContent">
        <div class="contentWrapper">
            <article class="content">

            </article>
            <article class="content">

            </article>
            <article class="content">
                <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
            </article>
        </div>
    <div>
</html>

1 个答案:

答案 0 :(得分:0)

使用其他课程:

.menu.fixed {
position:fixed;
top: 0;
margin: 0;
z-index: 3;
}

以及用于检测滚动的以下jQuery代码:

$(document).scroll(function()
{
    if($(document).scrollTop()>=500)
        $('.menu').addClass('fixed');
    else
        $('.menu').removeClass('fixed');
});

See working JSFiddle Demo