gpt4 book ai didi

javascript - Scrollspy 选择了错误的元素

转载 作者:行者123 更新时间:2023-11-28 05:32:12 27 4
gpt4 key购买 nike

如您在此示例中所见,Bootstrap Scrollspy 会跳过导航,但实际上并没有像预期的那样工作。错误的元素是获取 .active 类。

$('.spycontent').scrollspy({ target: 'nav' })
html, body {
height:100%;
}

body > .container, body > .container > .row, .col-xs-6 {
height:100%;
}

.col-xs-6 {
overflow-y:auto;
}

.item {
height:500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

<div class="container">
<div class="row">
<nav class="col-xs-6">
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#group1">group1</a>
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#question1">question1</a>
</li>
<li>
<a href="#question2">question2</a>
</li>
</ul>
</li>
<li>
<a href="#group2">group2</a>
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#question3">question3</a>
</li>
<li>
<a href="#question5">question5</a>
</li>
</ul>
</li>
</ul>
</nav>
<div class="col-xs-6 spycontent">
<div class="col-xs-12" id="group1">
group1
<div class="row">
<div class="col-xs-12 item" id="question1">
question1
</div>
<div class="col-xs-12 item" id="question2">
question2
</div>
</div>
</div>
<div class="col-xs-12" id="group2">
group2
<div class="row">
<div class="col-xs-12 item" id="question3">
question3
</div>
<div class="col-xs-12 item" id="question5">
question5
</div>
</div>
</div>
</div>
</div>
</div>

我觉得菜单和内容都很完美,为什么突出显示了错误的元素?

最佳答案

我已经知道发生了什么:

当我告诉浏览器有一列嵌套在另一列中时,浏览器不同意我的看法。 TWBS CSS 以某种方式弄乱了位置。移除巢解决了我的问题:

$('.spycontent').scrollspy({ target: 'nav' })
html, body {
height:100%;
}

body > .container, body > .container > .row, .col-xs-6 {
height:100%;
}

.col-xs-6 {
overflow-y:auto;
}

.item {
height:500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

<div class="container">
<div class="row">
<nav class="col-xs-6">
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#group1">group1</a>
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#question1">question1</a>
</li>
<li>
<a href="#question2">question2</a>
</li>
</ul>
</li>
<li>
<a href="#group2">group2</a>
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#question3">question3</a>
</li>
<li>
<a href="#question5">question5</a>
</li>
</ul>
</li>
</ul>
</nav>
<div class="col-xs-6 spycontent">
<div id="group1">
group1
<div class="row">
<div class="col-xs-12 item" id="question1">
question1
</div>
<div class="col-xs-12 item" id="question2">
question2
</div>
</div>
</div>
<div id="group2">
group2
<div class="row">
<div class="col-xs-12 item" id="question3">
question3
</div>
<div class="col-xs-12 item" id="question5">
question5
</div>
</div>
</div>
</div>
</div>
</div>

关于javascript - Scrollspy 选择了错误的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com