jquery - JavaScript 'smooth scroll' will not scroll -



jquery - JavaScript 'smooth scroll' will not scroll -

question background:

i'm developing single page site using twitter bootstrap 3 within asp.net mvc project.

the problem:

im attempting implement 'smooth scrolling' should scroll to relevant div clicked navbar drop down. @ moment when section clicked navbar drop downwards 'jumps' div rather scroll.

the code:

this next '_layout.cshtml' markup provides navbar , footer. javascript scroll shown between script tags:

<!doctype html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@viewbag.title - 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 , toggle grouped improve 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 nav links, forms, , other content 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> <!-- html outside other container above i.e separate. need container allows utilize bootstrap classes --> <div class="container"> <div class="row"> <div class="col-sm-2"> <h6>copyright &copy; 2014</h6> </div> <div class="col-sm-4"> <h6>about us</h6> <p>chia dreamcatcher artisan vinyl intelligentsia banksy. fine 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"> me</span></h6> </div> </div> </div> </footer> </body> @scripts.render("~/bundles/jquery") @scripts.render("~/bundles/bootstrap") @rendersection("scripts", required: false)

screenshots:

the next screenshots shown when navbar alternative clicked page go right div, will not scroll. help resolving great.

after bit more research , using great example:http://alijafarian.com/responsive-page-scrolling-with-jquery-and-bootstrap/ managed produce solution below works perfectly.

<script> $(document).ready(function() { $('#nav .navbar-nav li>a').on('click', function(event){ event.preventdefault(); var sectionid = $(this).attr("href"); scrolltoid(sectionid, 750); }); function scrolltoid(id, speed) { var offset = 70; var targetoffset = $(id).offset().top - offset; $('html,body').animate({scrolltop:targetoffset}, speed); } }); </script>

javascript jquery

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -