【发布时间】:2016-06-23 08:45:39
【问题描述】:
我想要实现的目标:单击链接后(在选项卡内),切换到另一个选项卡并移动到页面的特定部分(如锚点)。
我的工作是在单击链接后切换到选项卡,但它不会向下滚动到锚点。
非常感谢您的帮助!
代码:
<div class="tab-content">
<div class="tab-pane active in" id="A">
<a data-tab-destination="tab-B" href="#bag-in-box">Bag-in-Box</a>
</div>
<div class="tab-pane fade" id="B">
...
<div id="bag-in-box"> <!-- This is where it needs to scroll to -->
<p>Bag-in-Box</p>
</div>
</div>
</div>
<script type="text/javascript">
$(function() {
$("a[data-tab-destination]").on('click', function(e) {
e.preventDefault();
// Works
var tab = $(this).attr('data-tab-destination');
$("#" + tab).click();
// Doesn't work
var hashtag = $(this.hash);
var target = hashtag.length ? hashtag : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({scrollTop: target.offset().top}, 1000);
return false;
}
});
});
</script>
【问题讨论】:
标签: javascript jquery html twitter-bootstrap