Twitter Bootstrap 3:Spyscroll总是选择最后一个并在JSFIDDLE中工作而不是本地的相同代码

时间:2015-04-23 19:17:02

标签: javascript jquery html twitter-bootstrap twitter-bootstrap-3

同样的代码没有产生相同的行为,这有点好笑。

http://jsfiddle.net/5n3ygvhe/

这是jsfiddle代码。 这是spyscroll的一个很好的演示。

所以我将代码复制到我的本地html文件中。

这是我的本地html文件。我拿着appengine文件。我检查了jquery,bootstrap.min.css和bootstrap.min.js是否正确加载。 jquery的版本是2.1.3,bootstrap是3.0.0。

我已经尝试过spyscroll的其他jsfiddle,完全相同的事情发生了。不是在当地工作,而是在jsfiddle工作。 当我滚动页面时,导航系统没有反映出来并且始终坚持最后一项。

<html>
<script src="bower_components/jquery/dist/jquery.js"></script>
<!-- <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css"> -->
<!-- <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script> -->
<link rel="stylesheet" href="bower_components/bootstrap.min.css">
<script src="bower_components/bootstrap.min.js"></script>
<style>
.group {
    background: yellow;
    width: 200px;
    height: 500px;
}
.group .subgroup {
    background: orange;
    width: 150px;
    height: 200px;
}
#sidebar.affix {
    top: 20px;
}
/* sidebar */
.bs-docs-sidebar {
    padding-left: 20px;
    margin-top: 20px;
    margin-bottom: 20px;
}

/* all links */
.bs-docs-sidebar .nav>li>a {
    color: #999;
    border-left: 2px solid transparent;
    padding: 4px 20px;
    font-size: 13px;
    font-weight: 400;
}

/* nested links */
.bs-docs-sidebar .nav .nav>li>a {
    padding-top: 1px;
    padding-bottom: 1px;
    padding-left: 30px;
    font-size: 12px;
}

/* active & hover links */
.bs-docs-sidebar .nav>.active>a, 
.bs-docs-sidebar .nav>li>a:hover, 
.bs-docs-sidebar .nav>li>a:focus {
    color: #563d7c;                 
    text-decoration: none;          
    background-color: transparent;  
    border-left-color: #563d7c; 
}
/* all active links */
.bs-docs-sidebar .nav>.active>a, 
.bs-docs-sidebar .nav>.active:hover>a,
.bs-docs-sidebar .nav>.active:focus>a {
    font-weight: 700;
}
/* nested active links */
.bs-docs-sidebar .nav .nav>.active>a, 
.bs-docs-sidebar .nav .nav>.active:hover>a,
.bs-docs-sidebar .nav .nav>.active:focus>a {
    font-weight: 500;
}

/* hide inactive nested list */
.bs-docs-sidebar .nav ul.nav {
    display: none;           
}
/* show active nested list */
.bs-docs-sidebar .nav>.active>ul.nav {
    display: block;           
}

</style>
<!-- Top Navbar -->
<body>
<div class="navbar navbar-inverse">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle"
                    data-toggle="collapse"
                    data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">
                Bootstrap 3.0 Skeleton
            </a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div>
    </div>
</div>
<div class="row">
    <!--Nav Bar -->
    <nav class="col-xs-3 bs-docs-sidebar">
        <ul id="sidebar" class="nav nav-stacked">
            <li>
                <a href="#GroupA">Group A</a>
                <ul class="nav nav-stacked">
                    <li><a href="#GroupASub1">Sub-Group 1</a></li>
                    <li><a href="#GroupASub2">Sub-Group 2</a></li>
                </ul>
            </li>
            <li>
                <a href="#GroupB">Group B</a>
                <ul class="nav nav-stacked">
                    <li><a href="#GroupBSub1">Sub-Group 1</a></li>
                    <li><a href="#GroupBSub2">Sub-Group 2</a></li>
                </ul>
            </li>
            <li>
                <a href="#GroupC">Group C</a>
                <ul class="nav nav-stacked">
                    <li><a href="#GroupCSub1">Sub-Group 1</a></li>
                    <li><a href="#GroupCSub2">Sub-Group 2</a></li>
                </ul>
            </li>
        </ul>
    </nav>
    <!--Main Content -->
    <div class="col-xs-9">
        <section id="GroupA" class="group">
            <h3>Group A</h3>
            <div id="GroupASub1" class="subgroup">
                <h4>Group A Sub 1</h4>
            </div>
            <div id="GroupASub2" class="subgroup">
                <h4>Group A Sub 2</h4>
            </div>
        </section>
        <section id="GroupB" class="group">
            <h3>Group B</h3>
            <div id="GroupBSub1" class="subgroup">
                <h4>Group B Sub 1</h4>
            </div>
            <div id="GroupBSub2" class="subgroup">
                <h4>Group B Sub 2</h4>
            </div>
        </section>
        <section id="GroupC" class="group">
            <h3>Group C</h3>
            <div id="GroupCSub1" class="subgroup">
                <h4>Group C Sub 1</h4>
            </div>
            <div id="GroupCSub2" class="subgroup">
                <h4>Group C Sub 2</h4>
            </div>
        </section>    
    </div>
</div>
</body>
<script>
$('body').scrollspy({
    target: '.bs-docs-sidebar',
    offset: 40
});
$("#sidebar").affix({
    offset: {
      top: 60
    }
});
</script>

1 个答案:

答案 0 :(得分:2)

在页面顶部添加$(function() {}

使用jQuery的<html> <head> <style> body { background-image: url("http://i.imgur.com/SwKXk23.jpg"); background-repeat: no-repeat; background-attachment: fixed; background-position: center; } body { margin: 0; } .header { width: 100%; height: auto; background-color: none; padding-top: 24px; } .headerContent { width: 1024px; height: auto; margin: 0 auto; } .headerContent a img { width: 659px; height: 144px; margin: 0px auto; display: block; } .nav { width:750px; margin:0 auto; list-style:none; } .nav li { float:left; } .nav a { display:block; text-align:center; width:150px; /* fixed width */ text-decoration:none; } .nav ul li{ height: 40 px; background: #A14F53; } .nav ul li{ list-style-type: none; width: 150px; float: left; } .nav ul li a{ text-decoration: none; color: black; line-height: 40px; display: block; border-right: 1px solid #CCC; text-align:center; } .nav ul li a:hover{ background-color:#F9C1B5; } .headerBreak{ width: 100%; height: o%; border bottom: 2px solid #128e75; } </style> </head> <body> <div class="background"> <div class="header"> <div class="headerContent"> <a href="#"> <img style="border:0;width:900px;height:250px;" alt="" title="" src="http://i.imgur.com/5NhCbxu.png"> </a> </div> <div class="nav"> <ul id="nav"> <li><a href="Pagina principala.html">Pagina principala</a></li> <li><a href="Despre noi.html">Despre noi</a></li> <li><a href="Clienţii noştri.html">Clientii nostri</a></li> <li><a href="Produse.html">Produse</a></li> <li><a href="Contacte.html">Contacte</a></li> </ul> </div> </div> <div class="headerBreak"></div> </div> </body> </html> 包装器