如何跳转到div标签分隔的页面中的特定部分?

时间:2016-04-22 18:18:59

标签: javascript jquery ruby-on-rails

所以我想在这里实现的很简单,但我似乎无法找到答案。也许这是关键字的错误术语,但我在这里问。我的网站右侧有一个导航栏,其中列出了用户所在的每个特定页面中的所有内容。我想做的是"跳" /单击列表中的项目时自动滚动到页面中的部分。这是我到目前为止所拥有的。我不知道从哪里开始或开始,我对jquery的理解也是有限的。我不久前就开始学习rails了,我真的很感激一些指导。

我将所有内容分成div标签,并且我已经准备好了link_to按钮。我只是不太确定如何连接两者。

<div class="container">
  <div class="row">
    <div class="col-md-9 aquascaping-content">

      <div class="aquascape-about-intro">
        content//
      </div>

      <div class="aquascape-about-iwagumi">
        content//
      </div>

      <div class="aquascape-about-jungle">
        content//
      </div>

      <div class="aquascape-about-dutch">
        content//
      </div>

      <div class="aquascape-about-parting">
        content//
      </div>

    </div>

    <div class="col-md-3 side-nav">
      <h3 class="page-title", style="padding-left: 20px">Quick Navigation</h3>
      <div class="well well-sm">
        <ul class="side-nav-well">

          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Intro', "#" %>
            </b>
          </li>
          <hr>

          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Iwagumi Aquariums', "#" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Jungle Aquariums', "#" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Dutch Aquariums', "#" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Parting Words', "#" %>
            </b>
          </li>
        </ul>
    </div>
  </div><!-- end of quick navigation -->
  </div>
</div><!-- /.container-fluid -->

1 个答案:

答案 0 :(得分:2)

您需要将ID添加到这样的部门:

<div class="aquascape-about-intro" id="intro">

然后在链接中将此id添加为href,如下所示:

<%= link_to "Intro", "#intro" %>

这会转换为:

<a href="#intro">Intro</a>
<!-- Clicking on this would directly take you to the div with id intro -->

对所有div执行此操作,如下所示。

<div class="container">
  <div class="row">
    <div class="col-md-9 aquascaping-content">

      <div class="aquascape-about-intro" id="intro">
        content//
      </div>

      <div class="aquascape-about-iwagumi" id="about">
        content//
      </div>

      <div class="aquascape-about-jungle" id="jungle">
        content//
      </div>

      <div class="aquascape-about-dutch" id="dutch">
        content//
      </div>

      <div class="aquascape-about-parting" id="parting">
        content//
      </div>

    </div>

    <div class="col-md-3 side-nav">
      <h3 class="page-title", style="padding-left: 20px">Quick Navigation</h3>
      <div class="well well-sm">
        <ul class="side-nav-well">

          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Intro', "#intro" %>
            </b>
          </li>
          <hr>

          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Iwagumi Aquariums', "#about" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Jungle Aquariums', "#jungle" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Dutch Aquariums', "#dutch" %>
            </b>
          </li>
          <hr>
          <li class="side-nav-item">
            <b>
              <%= image_tag("arrow-icon.png", size: "30x30", alt: "Aquascaping image", class: "side-nav-img")%>
              <%= link_to 'Parting Words', "#parting" %>
            </b>
          </li>
        </ul>
    </div>
  </div><!-- end of quick navigation -->
  </div>
</div><!-- /.container-fluid -->