【发布时间】:2015-06-16 16:35:24
【问题描述】:
我有一个很长的包含内容的 HTML 页面。我想要实现的是当有人单击标题中的菜单项时,页面应该向下滚动到锚元素。我尝试了很多没有运气的事情。我不知道 JavaScript。这是我尝试过的:
_header.html.erb
<div class="header_pad">
<nav class="top-bar" data-topbar role="navigation">
<ul class="title-area">
<li class="name">
<h1><a href="/">The Investors' Club</a></h1>
</ul>
<section class="top-bar-section">
<ul class="left">
<li><a id="result" href="#contact">Contact</a></li>
</ul>
</section>
</nav>
</div>
应用程序.js
function scrollToAnchor(aid){
var aTag = $("a[name='"+ aid +"']");
$('html,body').animate({scrollTop: aTag.offset().top},'slow');
}
$("#result").click(function() {
scrollToAnchor('contact');
});
index.html.erb
<div id="contact">
<%= image_tag("contact_us.png", :alt => "Forex Investment Contact") %></br>
<p class="small-11 small-centered columns">
Skype is free and more convenient, give us a call or send us an email if you happen to have some questions. We will
be glad to hear from you.
</br>
<%= image_tag("skype.png", :alt => "skype") %> OR
<b>100forexinvestors@gmail.com</b>
</p>
</div>
直播页面:https://infinite-oasis-2303.herokuapp.com 所以我希望当我单击标题中的“联系人”时,它会一直平滑滚动到页面下方的联系人锚点。有什么帮助吗?
编辑: 我让它与我作为答案发布的 js 一起工作。然而。如果我单击后退按钮并单击另一个链接,则动画将不再起作用,直到我重新加载页面。看起来javascript只加载一次。我该如何消除它?
【问题讨论】:
-
在 href="#contact"
<a id="result" href="#contact">Contact</a>和联系部分使用<div id="contact">...</div> -
好的。我到了这个阶段,但向下滚动效果不起作用。它只是跳到锚点,没有效果。但平滑向下滚动是我所追求的。
标签: javascript jquery html css ruby-on-rails