首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery ScrollTo问题

Jquery ScrollTo问题
EN

Stack Overflow用户
提问于 2012-05-02 18:51:25
回答 1查看 994关注 0票数 0

我使用Jquery ScrollTo,这样当我单击页面顶部的问题时,它会滚动到屏幕下方的问题和答案。此函数工作正常(在一定程度上)

现在,当我点击一个问题,也就是页面的一半,它会滚动,我的导航栏会重叠一半的问题(我使用的是twitter bootstrap修复的导航栏)

第二个问题是,当我单击顶部(应该滚动回页面顶部)时,导航栏会重叠前3个问题。

如果我从导航栏中删除已修复的,则一切正常,但我更希望保持导航栏已修复

我的代码如下

视图

代码语言:javascript
复制
<div class="links">

代码语言:javascript
复制
<ul class="top_links"> 

    <li><a href="#1a">Question 1</a></li>
    <li><a href="#1b"> Question 2</a></li>
    <li><a href="#1c"> Question 3</a></li>
    <li><a href="#1d">Question 4</a></li>
    <li><a href="#1e">Question 5</a></li>

</ul>

    </div>
    <ul class="faq">
<li><a name="1a"></a><span class="question">Q: Question 1</span><span        class="answer">Follow the link marked join which will take you to the relevant section.We review each application for membership and aim to let you know within qo working days.</span><div class="top"><a href="#top">Top ^</a></div></li>

<li><a name="1b"></a><span class="question">Q:Question 2</span><span class="answer">A:  Follow the link marked Forensic Odontologist list which will take you to the page where Odontologists are listed by region with full contact details..</span><div class="top"><a href="#top">Top ^</a></div></li>

<li><a name="1c"></a><span class="question">Q: Question 3</span><span class="answer">A: Unfortunately the subject is case dependent, which cannot be predicted. It is not a full time discipline. For this reason it is generally not possible to shadow an Odontologist - sorry.</span><div class="top"><a href="#top">Top ^</a></div></li>

<li><a name="1d"></a><span class="question">Q: Question 4</span><span class="answer">A: You should look at the available courses by following the link marked courses and then contact the particular institution directly and not through BAFO.</span><div class="top"><a href="#top">Top ^</a></div></li>

<li><a name="1e"></a><span class="question">Q:Question 5</span><span        class="answer">A: Nunc non orci eget odio suscipit rutrum. Nullam quam neque, tempus at, semper in, semper eu, mi. Nulla eu turpis vitae arcu sagittis iaculis. Fusce ut nunc vel ligula convallis vulputate. Aliquam feugiat dui in risus. Sed hendrerit. Praesent mollis, ligula imperdiet viverra faucibus, diam turpis ullamcorper ipsum, eget posuere velit tellus et turpis. Vivamus facilisis est nec libero. Phasellus at velit. Vivamus sed mauris.</span><div class="top"><a href="#top">Top ^</a></div></li>

Jquery

代码语言:javascript
复制
$(document).ready(function () {
$.localScroll();
$(".top_links > li > a").click(function () {
    $(".faq > li").removeClass('highlight');

    var str = $(this).attr("href");
    str = "a[name='" + str.substring(1) + "']";
    $(str).parent().addClass('highlight');


});

});

我希望这是足够的信息来继续下去。如果任何人能提出任何建议,我们将不胜感激。

示例是http://jsfiddle.net/richlewis14/YsK29/ (很抱歉使用了大量的css,但使用的是引导程序)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-03 03:50:19

您的站点正在正确滚动。问题是你的导航栏堆叠在顶部,覆盖了内容。我写的代码和你的略有不同,但它是有效的:

代码语言:javascript
复制
$(document).ready(function () {
    var navHeight = $('.navbar').height(); //get the navbar height

    $(".top_links > li > a").click(function (e) {
        $(".faq > li").removeClass('highlight');

        var str = $(this).attr("href");
        str = "a[name='" + str.substring(1) + "']";
        $(str).parent().addClass('highlight');
        e.preventDefault(); // prevent adding something to url
        var offset = $('.highlight').offset(); //get highlight position from top of page

        $('html, body').scrollTop(offset.top-navHeight); // scroll to correct position
    });

    $('.top').find('a').on('click', function(e){
       $('html, body').scrollTop(0);
       e.preventDefault();
    });
});​
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10412259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档