HTML Bootstrap 3没有向UL LI添加活动类

时间:2015-08-24 01:51:28

标签: jquery html css twitter-bootstrap

我试图使用scroll-spy在一页滚动和粘性导航中将活动类添加到ul li,由于某些未知原因,我无法在我的项目中使用它。 以下是我的菜单布局:

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<nav class="navbar navbar-default navbar-fixed-top my-nav" role="navigation">
    <div class="container_full height">
        <div class="navbar-header page-scroll height">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="logo-holder">
            <img class="" src="img/logo-p.png"/>
            <span>
                <a class="page-scroll" href="#home-page">xxxxxx</a>
                <i>xxxxxxx</i>
            </span>
            </div> 
        </div>  
        <div class="collapse navbar-collapse navbar-ex1-collapse height">
            <ul class="nav navbar-nav height">
                <li class="hidden">
                    <a class="page-scroll" href="#page-top"></a>
                </li>
                <li>
                    <a class="page-scroll" href="#home-page">item1</a>
                </li>
                <li>
                    <a class="page-scroll" href="#item2">item2</a>
                </li>                   
                <li>
                    <a class="page-scroll" href="#item3">item3</a>
                </li>

                <li>
                    <a class="page-scroll" href="#item4">item4</a>
                </li>
            </ul>
            </div>
        </div>
    </div>
</nav>
<div class="container_full" id="site">
<section class="parallax">
<!-- HOME PAGE DISPLAY -->  
    <div id="home-page" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            //many divs css parallax--

nav js文件位于:

 $('.parallax').scroll(function() {

if ($('.parallax').scrollTop() > 100) { 
    $(".navbar-fixed-top").addClass("top-nav-collapse");
    $(".my-nav").addClass("nav-resize");



} else {

    $(".navbar-fixed-top").removeClass("top-nav-collapse");
    $(".my-nav").removeClass("nav-resize");
    $(".moveLogo").removeClass("moveLogo-resize");
}
});


$(function() {
$('a.page-scroll').bind('click', function(event) {
    var $anchor = $(this);
    var position =  $($anchor.attr('href')).offset().top + $('.parallax').scrollTop() ;

    $('html,body,.parallax').stop().animate({
        scrollTop: position
    }, 1500, 'easeInOutExpo');

    event.preventDefault();
   });
});

如果我在没有视差div等的情况下使用上述方法,这将没有任何问题,但目前 li 类始终在最后一个项目向下滚动页面时。

enter image description here

green =父容器内的可滚动内容&#34;蓝色&#34;,绿色div用于定位id。

 .blue{
    height:100%;
    width:100%;
    overflow:hidden;

 }

 .green // Has many divs

我尝试了明显的目标绿色&gt; divs但它似乎不起作用。

1 个答案:

答案 0 :(得分:2)

你的代码没有给出足够的信息,所以我添加了一系列1000px高且具有不同背景颜色的div - 所有这些都对应于导航菜单中的ID - 滚动间谍正在工作。也许你还没有足够的内容来滚动或错误地实现ID?

请注意,我还使用了最新版本的Bootstrap和jQuery ......

Check the fiddle

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
        <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
        <!--[if lt IE 9]>
            <script src="//oss.maxcdn.com/libs/html5shiv/r29/html5.min.js"></script>
            <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>


<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<nav class="navbar navbar-default navbar-fixed-top my-nav" role="navigation">
    <div class="container_full height">
        <div class="navbar-header page-scroll height">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="logo-holder">

            <span>
                <a class="page-scroll" href="#home-page">xxxxxx</a>
                <i>xxxxxxx</i>
            </span>
            </div> 
        </div>  
        <div class="collapse navbar-collapse navbar-ex1-collapse height">
            <ul class="nav navbar-nav height">
                <li class="hidden">
                    <a class="page-scroll" href="#page-top"></a>
                </li>
                <li>
                    <a class="page-scroll" href="#home-page">item1</a>
                </li>
                <li>
                    <a class="page-scroll" href="#item2">item2</a>
                </li>                   
                <li>
                    <a class="page-scroll" href="#item3">item3</a>
                </li>

                <li>
                    <a class="page-scroll" href="#item4">item4</a>
                </li>
            </ul>
            </div>
        </div>
    </div>
</nav>
<div class="container_full" id="site">
<section class="parallax">
<!-- HOME PAGE DISPLAY -->  
    <div id="home-page" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:pink"></div>
        </div>
    </div>

    <div id="item2" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:red"></div>
        </div>
    </div>

    <div id="item3" class="home-g height-650">
        <div class="parallax-layer parallax-back height-650" >
            <div class="home">
            </div>
            <div style="height:1000px; background:green"></div>
        </div>
    </div>


        <script src="http://code.jquery.com/jquery.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>

</script>
    </body>
</html>