我正在開發使用Twitter的引導3的ASP.NET MVC項目中的單頁網站。
問題:
即時通訊目前試圖實現「平滑滾動」應該滾動到相關的分度,從導航欄點擊下拉。在從導航欄下拉菜單中點擊某個區段時,它會跳轉到div而不是滾動。
代碼:
這下面是我的「_Layout.cshtml」標記與提供導航欄和頁腳。 JavaScript的滾動顯示的腳本標記之間:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<script>
$('#nav .navbar-nav li>a').click(function() {
var link = $(this).attr('href');
var offSet = 30;
var targetOffset = $(link).offset().top - offSet;
$('html,body').animate({ scrollTop: targetOffset }, 7000);
});
</script>
</head>
<body>
<div class="navbar navbar-fixed-top">
<nav class="navbar navbar-default" role="navigation" id="nav">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/"><img src="images/logo.png" alt="yourLogo"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#myCarousel">Projects</a></li>
<li><a href="#Welcome">Welcome</a></li>
<li><a href="#features">Thing</a></li>
<li><a href="#About">About Me</a></li>
<li><a href="#Location">Location</a></li>
<li><a href="#Contact">Contact Me</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
<div class="container body-content">
@RenderBody()
</div>
<footer>
<!-- This HTML is outside the other container above i.e its separate. You need a container as this allows you to use bootstrap classes -->
<div class="container">
<div class="row">
<div class="col-sm-2">
<h6>Copyright © 2014</h6>
</div>
<div class="col-sm-4">
<h6>About Us</h6>
<p>Chia dreamcatcher artisan vinyl Intelligentsia Banksy. Art party whatever cred wolf hella distillery Portland wayfarers Kickstarter, kale chips pug artisan +1 crucifix.</p>
</div>
<div class="col-sm-2">
<h6>Navigation</h6>
<ul class="unstyled">
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Links</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<div class="col-sm-2">
<h6>Follow Us</h6>
<ul class="unstyled">
<li><a href="#">Twitter</a></li>
<li><a href="#">Facebook</a></li>
<li><a href="#">Google Plus</a></li>
</ul>
</div><!-- end col-sm-2 -->
<div class="col-sm-2">
<h6>Coded with<span class="glyphicon glyphicon-heart"> by Me</span></h6>
</div>
</div>
</div>
</footer>
</body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
截圖:
下面的截圖顯示,導航欄選項,當單擊頁轉到正確的DIV,但它不會滾動。任何幫助解決這個問題都會很好。
您需要阻止錨點的默認行爲,否則瀏覽器將在您的事件處理程序運行之前獲取'#whatever'哈希值並跳轉頁面。 – Mathletics 2014-10-16 20:26:31