scrollspy导航到不同的页面并返回相同的位置

时间:2014-05-07 00:58:22

标签: javascript jquery html css twitter-bootstrap

网友爱好者,我希望你能解决我的问题。

我正在使用bootstrap创建一个网站,该网站使用scrollspy导航到导航栏的部分页面。滚动到页面的一部分时,我可以让URL无法显示"#section-2"的唯一方法是在我的<head>中调用我的javascript。

现在出现问题......

我试图添加一个FAQ部分,它完全是一个不同的页面,希望将它链接回原始页面的特定部分。

似乎FAQ <a> tag无法正常工作,除非重新调整浏览器的大小。

已经有两个带有卷轴的错误! bootstrap经历这么多错误是正常的吗?

http://dnwebdev.com

以下代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>
        Day &amp; Night | Responsive Web Design
    </title>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">


    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">

    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js">
    </script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js">
    </script>
</head>

<body data-spy="scroll" data-target="#myNavbar" data-offset="125">
    <nav id="myNavbar" class="navbar navbar-dn navbar-fixed-top" role="navigation">
        <!-- Brand and toggle get grouped for better mobile display -->
        <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" style="color:#2c3e50;">
          Day 
          <img src="../images/dnlogoc.png" height="35px"/>
          Night
        </a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="navbarCollapse">
            <ul class="nav navbar-nav" style="text-align:center;">
                <li class="active">
                    <a href="#section-1">
              About Us
            </a>
                </li>
                <li>
                    <a href="#section-2">
              What We Do
            </a>
                </li>

                <li>
                    <a href="#section-3">
              Our Process
            </a>
                </li>

                <li>
                    <a href="#section-4">
              Our Work
            </a>
                </li>


                <li>
                    <a href="#section-5">
              Get in touch
            </a>
                </li>
                <li>
                    <a href="/faq">
              FAQ
             </a>
                </li>



            </ul>
        </div>
    </nav>

    <div class="container day" style="padding-top:75px;">
        <hr>

        <h2 class="center" id="section-1">
        Web Solutions, LLC
      </h2>
        <hr>
        <div class="row">
            <div class="col-lg-4">
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla. Vivamus nisl leo, blandit at bibendum eu, tristique eget risus. Integer aliquet quam ut elit suscipit, id interdum neque porttitor. Integer faucibus ligula.
                </p>
            </div>
            <div class="col-lg-4" style="text-align:center;">
                <img src="../images/dnlogoc.png" height="200px" class="mainimg">

            </div>
            <div class="col-lg-4">

                <p>
                    Vestibulum quis quam ut magna consequat faucibus. Pellentesque eget nisi a mi suscipit tincidunt. Ut tempus dictum risus. Pellentesque viverra sagittis quam at mattis. Suspendisse potenti. Aliquam sit amet gravida nibh, facilisis gravida odio. Phasellus auctor velit at lacus blandit, commodo iaculis justo viverra. Etiam vitae est arcu. Mauris vel congue dolor. Aliquam eget mi mi. Fusce quam tortor, commodo ac dui quis, bibendum viverra erat. Maecenas mattis lectus enim, quis tincidunt dui molestie euismod. Curabitur et diam tristique, accumsan nunc eu, hendrerit tellus.
                </p>
            </div>
        </div>


        <p>
            Vestibulum quis quam ut magna consequat faucibus. Pellentesque eget nisi a mi suscipit tincidunt. Ut tempus dictum risus. Pellentesque viverra sagittis quam at mattis. Suspendisse potenti. Aliquam sit amet gravida nibh, facilisis gravida odio. Phasellus auctor velit at lacus blandit, commodo iaculis justo viverra. Etiam vitae est arcu. Mauris vel congue dolor. Aliquam eget mi mi. Fusce quam tortor, commodo ac dui quis, bibendum viverra erat. Maecenas mattis lectus enim, quis tincidunt dui molestie euismod. Curabitur et diam tristique, accumsan nunc eu, hendrerit tellus.
        </p>
    </div>

    <div class="container day">





        <hr>

        <h3 class="center" id="section-2">
        What We Do
      </h3>

        <hr>


        <div class="row">
            <!-- Boxes de Acoes -->
            <div class="col-xs-12 col-sm-4 col-lg-4 smallpad">
                <div class="box">
                    <div class="icon">
                        <div class="image">
                            <i class="fa fa-map-marker">
                </i>
                        </div>
                        <div class="info">
                            <h3 class="title">
                  Local Search Engine Optimization
                </h3>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in lobortis nisl, vitae iaculis sapien. Phasellus ultrices gravida massa luctus ornare. Suspendisse blandit quam elit, eu imperdiet neque semper.
                            </p>
                            <div class="more">
                                <a href="#" title="Title Link">
                    Read More 
                    <i class="fa fa-angle-double-right">
                    </i>
                  </a>
                            </div>
                        </div>
                    </div>
                    <div class="space">
                    </div>
                </div>
            </div>

            <div class="col-xs-12 col-sm-4 col-lg-4 smallpad">
                <div class="box">
                    <div class="icon">
                        <div class="image">
                            <i class="fa fa-laptop">
                </i>
                        </div>
                        <div class="info">
                            <h3 class="title">
                  Mobile Friendly Web Design
                </h3>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in lobortis nisl, vitae iaculis sapien. Phasellus ultrices gravida massa luctus ornare. Suspendisse blandit quam elit, eu imperdiet neque semper.
                            </p>
                            <div class="more">
                                <a href="#" class="hover" title="Title Link">
                    Read More 
                    <i class="fa fa-angle-double-right">
                    </i>
                  </a>
                            </div>
                        </div>
                    </div>
                    <div class="space">
                    </div>
                </div>
            </div>



            <div class="col-xs-12 col-sm-4 col-lg-4 smallpad">
                <div class="box">
                    <div class="icon">
                        <div class="image">
                            <i class="fa fa-mobile">
                </i>
                        </div>
                        <div class="info">
                            <h3 class="title">
                  Responsive - Mobile Friendly Web Design
                </h3>
                            <p>
                                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed in lobortis nisl, vitae iaculis sapien. Phasellus ultrices gravida massa luctus ornare. Suspendisse blandit quam elit, eu imperdiet neque semper.
                            </p>
                            <div class="more">
                                <a href="#" title="Title Link">
                    Read More 
                    <i class="fa fa-angle-double-right">
                    </i>
                  </a>
                            </div>
                        </div>
                    </div>
                    <div class="space">
                    </div>
                </div>
            </div>
            <!-- /Boxes de Acoes -->
        </div>
    </div>
    <div class="container night">

        <div class="page-header" style="text-align:center;" id="section-3">
            <h2>
          Our Process
        </h2>

            <h3>

        </h3>

        </div>
        <ul class="timeline">
            <li class="timeline">
                <div class="timeline-badge mybadge">
                    <i class="glyphicon glyphicon-search">
            </i>
                </div>
                <div class="timeline-panel">
                    <div class="timeline-heading">
                        <h4 class="timeline-title">
                Planning
              </h4>
                    </div>
                    <div class="timeline-body">
                        <p>
                            Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
                        </p>
                    </div>
                </div>
            </li>
            <li class="timeline-inverted">
                <div class="timeline-badge mybadge">
                    <i class="glyphicon glyphicon-th-list">
            </i>
                </div>
                <div class="timeline-panel">
                    <div class="timeline-heading">
                        <h4 class="timeline-title">
                Content
              </h4>
                    </div>
                    <div class="timeline-body">
                        <p>
                            Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
                        </p>
                        <p>
                            Suco de cevadiss, é um leite divinis, qui tem lupuliz, matis, aguis e fermentis. Interagi no mé, cursus quis, vehicula ac nisi. Aenean vel dui dui. Nullam leo erat, aliquet quis tempus a, posuere ut mi. Ut scelerisque neque et turpis posuere pulvinar pellentesque nibh ullamcorper. Pharetra in mattis molestie, volutpat elementum justo. Aenean ut ante turpis. Pellentesque laoreet mé vel lectus scelerisque interdum cursus velit auctor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac mauris lectus, non scelerisque augue. Aenean justo massa.
                        </p>
                    </div>
                </div>
            </li>
            <li class="timeline">
                <div class="timeline-badge mybadge">
                    <i class="glyphicon glyphicon-pencil">
            </i>
                </div>
                <div class="timeline-panel">
                    <div class="timeline-heading">
                        <h4 class="timeline-title">
                Design
              </h4>
                    </div>
                    <div class="timeline-body">
                        <p>
                            Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
                        </p>
                    </div>
                </div>
            </li>

            <li class="timeline-inverted">
                <div class="timeline-badge mybadge">
                    <i class="glyphicon glyphicon-wrench">
            </i>
                </div>
                <div class="timeline-panel">
                    <div class="timeline-heading">
                        <h4 class="timeline-title">
                Development
              </h4>
                    </div>
                    <div class="timeline-body">
                        <p>
                            Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
                        </p>
                        <hr>
                    </div>
                </div>
            </li>

            <li class="timeline">
                <div class="timeline-badge mybadge">
                    <i class="glyphicon glyphicon-cloud-upload">
            </i>
                </div>
                <div class="timeline-panel">
                    <div class="timeline-heading">
                        <h4 class="timeline-title">
                Test &amp; Launch
              </h4>
                    </div>
                    <div class="timeline-body">
                        <p>
                            Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
                        </p>
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="container day">

        <hr>
        <h3 class="center" id="section-4">
        Our Work
      </h3>
        <hr>

        <div class="row">


            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 myimg">

                <img src="../images/after-searok.jpg" class="img-responsive">
                <blockquote class="blockquote">
                    <footer>
                        <cite title="Source Title">
                Searok Charters
              </cite>
                    </footer>
                </blockquote>

            </div>

            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 myimg">

                <img src="../images/after-nicor.jpg" class="img-responsive">

                <blockquote class="blockquote">
                    <footer>
                        <cite title="Source Title">
                Nicor
              </cite>
                    </footer>
                </blockquote>



            </div>

            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 myimg">

                <img src="../images/after-cmt.jpg" class="img-responsive">

                <blockquote class="blockquote">
                    <footer>
                        <cite title="Source Title">
                Castle Mountain Team
              </cite>
                    </footer>
                </blockquote>


            </div>


            <hr>


        </div>

    </div>
    <hr>


    <div class="container" style="text-align:center;">
        <div class="col-xs-3">
            <i class="fa fa-5x fa-arrow-right">
        </i>
        </div>

        <div class="col-xs-6">
            <h2 class="center">
          <button type="button" class="btn btn-primary btn-lg btn-block">
            All my our work
          </button>
        </div>
        <div class="col-xs-3">
          <i class="fa fa-5x fa-arrow-left">
          </i>
        </div>
      </h2>
        </div>


        <footer style="text-align:center;">
            <div class="container night nopad">





                <hr>
                <h3 class="center" id="section-5">
          Let's talk about your project!
        </h3>
                <hr>



                <div class="col-lg-4 col-xs-12">
                    <hr>
                    <h2>

            <a href="tel:4073497036">
              <i class="fa fa-phone">
              </i>
              (407) 349-7036
            </a>
          </h2>
                    <hr>

                </div>
                <div class="col-lg-4 col-xs-12 nopad" style="text-align:center;">
                    <!-- Place this tag where you want the widget to render. -->
                    <div class="g-page" data-width="200" data-href="//plus.google.com/u/0/106333335696092351517" data-theme="dark" data-rel="publisher">
                    </div>

                    <!-- Place this tag after the last widget tag. -->
                    <script type="text/javascript">
                        (function () {
                            var po = document.createElement('script');
                            po.type = 'text/javascript';
                            po.async = true;
                            po.src = 'https://apis.google.com/js/platform.js';
                            var s = document.getElementsByTagName('script')[0];
                            s.parentNode.insertBefore(po, s);
                        })();
                    </script>
                </div>

                <div class="col-lg-4 col-xs-12">



                    <hr>

                    <h2>
            <a href="mailto:rob@dnwebdev.com">
              <i class="fa fa-envelope-o">
              </i>
              rob@dnwebdev.com
            </a>

          </h2>
                    <hr>

                </div>





                <div id="footer" class="day" style="text-align:center;">
                    <div class="col-lg-12 col-xs-12">
                        <p>
                            Day &amp; Night Web Solutions, LLC
                        </p>
                    </div>

                </div>

            </div>

        </footer>
        <script>
            function close_toggle() {
                if ($(window).width() <= 768) {
                    $('.nav a').on('click', function () {
                        $(".navbar-toggle").click();
                    });
                } else {
                    $('.nav a').off('click');
                }
            }
            close_toggle();




            $(window).resize(close_toggle);




             //function that offsets scoll

            if ($(window).width() <= 768) {

                var offset = 100;
            } else {

                var offset = 115;


            }
            $('.navbar li a').click(function (event) {
                event.preventDefault();
                $($(this).attr('href'))[0].scrollIntoView();
                scrollBy(0, -offset);
            });
        </script>


</body>

</html>

2 个答案:

答案 0 :(得分:1)

你有一个jQuery函数强制你的每个菜单链接滚动到该部分。你需要告诉函数对除 FAQ 之外的所有菜单链接执行此操作,并在单击 FAQ 时重定向,这样你就可以:

//trim the spaces and check if the text of the menu link is not equal to 'FAQ'
if ($.trim($(this).html()) != 'FAQ') {
    $($(this).attr('href'))[0].scrollIntoView();
    scrollBy(0, -offset);
} else {
    //if it is, redirect to the page
    window.location.href = $(this).attr('href');
}

Full Screen jsfiddle

编辑:

来自评论:

  

尝试链接时我正在使用“”;这是扔掉了   通过将#section-2添加到网址来滚动。

由于我们可以在页面重定向上使用的功能有限,因此解决这个问题非常棘手。但是,您可以这样做:

首先,完全删除此函数$('.navbar li a').click(function(event) {,并将此代码放在其他脚本之上:

$("document").ready(function() {
      $(document).on('click','.navbar li a',function(event) {
          alert($(this).html());
        event.preventDefault();
        if($.trim($(this).html())!='FAQ'){
            $($(this).attr('href'))[0].scrollIntoView();
            scrollBy(0, -offset);
          }
          else
          {
              window.location.href = $(this).attr('href');
          }

      });

   //document.referrer returns the url from which this page has been entered,
   //we will use this to check if we are redirected from FAQs page
   var previous_url = document.referrer;
   if(previous_url=='http://dnwebdev.com/dev/faq/'){
   //if we were redirected from FAQ page, we would have a #section-value in our url
   //hash here fetched that value
   var hash = document.URL.substr(document.URL.indexOf('#')+1);

   //this is the important part, we are gonna trigger that the
   //#section-value passed in url is _clicked_. And so the browser will
   //scroll down to that section
   $('.navbar li a#a-'+hash).trigger('click');
   //once it scrolls down, this deletes the #section-value from url
   history.pushState('', document.title, window.location.pathname);
   }

});

要使上述功能正常运行,您还需要为每个id标记添加<a>属性,例如:

<a href="#section-1" id="a-section-1">
<a href="#section-2" id="a-section-2">

关于trigger如何运作的一点演示:demo

答案 1 :(得分:0)

将id设为第一个div,例如dynaDiv并将<a href="/faq"> FAQ </a>修改为<a href="/faq" onclick='addFAQ();'> FAQ </a>将函数定义为

function addFAQ(){
  $("#dynaDiv").load("./faq.html");
}