简体   繁体   English

Bootstrap滚动导航栏问题

[英]Bootstrap scrolling navbar issues

The website I'm working on: zarwanhashem.com 我正在努力的网站:zarwanhashem.com

You can find my previous question (which includes my code) here: Bootstrap one page website themev formatting problems 你可以在这里找到我之前的问题(包括我的代码): 引导一页网站themev格式化问题

The selected answer solved my issues but I have another problem because of the jQuery adjustment with the -50. 选定的答案解决了我的问题,但由于使用-50进行jQuery调整,我还有另一个问题。 Now the navbar incorrectly indicates the page I am on. 现在,导航栏错误地指示了我所在的页面。 ie The navbar is supposed to darken the section that you are currently in. So if you click "about" it will take you to the about page and darken the about link in the navbar. 即导航条应该使您当前所在的部分变暗。因此,如果您单击“关于”,它将带您进入about页面并使导航栏中的about链接变暗。 But the link BEFORE the page you are on is highlighted because the -50 makes the navbar think that it is on the previous section. 但是在您所在的页面之前的链接会突出显示,因为-50使导航栏认为它在上一部分中。 You can easily try this to see what I mean. 你可以轻松尝试这个来看看我的意思。

How can I fix this? 我怎样才能解决这个问题? Thanks. 谢谢。 The reason I didn't add this onto my old question is because the person stopped looking at it. 我之所以没有将这个问题添加到我的旧问题上,是因为该人停止了查看它。

Also please keep your explanations simple/dumb them down a little for me. 另外,请保持你的解释简单/愚蠢对我来说。 I know very basic HTML and CSS, and I don't know any Javascript. 我知道非常基本的HTML和CSS,我不知道任何Javascript。

scrolling js: 滚动js:

//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
    if ($(".navbar").offset().top > 50) {
        $(".navbar-fixed-top").addClass("top-nav-collapse");
    } else {
        $(".navbar-fixed-top").removeClass("top-nav-collapse");
    }
});

//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top -50
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});

js added at end of document as suggested by poster in previous question: js在上一个问题中的海报建议的文档末尾添加:

$(window).ready(function(){
    $('div[class^="content-section"]').css('min-height', $(window).height());
    })

You are putting the .active class on the wrong element somehow. 你是以某种方式将.active类放在错误的元素上。 You need to put the .active class on the clicked element. 您需要将.active类放在单击的元素上。 You should handle the active state with js. 您应该使用js处理活动状态。 This is my solution based on your HTML structure but I'm sure there are different solutions as well. 这是我的解决方案,基于您的HTML结构,但我确信也有不同的解决方案。

$(document).on('click', '.page-scroll', function(event) {
   var clicked = event.target; //get the clicked element
   if($(clicked).closest('ul').hasClass('dropdown-menu')){ //check if clicked element is inside dropdown
      $(clicked).closest('ul').parent().siblings().removeClass('active'); //remove active class from all
      $(clicked).closest('ul').parent().addClass('active'); add active class on clicked element parent - in your case <li> tag.
   }else{
      $(clicked).parent().siblings().removeClass('active');
      $(clicked).parent().addClass('active');
   }
}

Let me know if this works for you. 如果这对您有用,请告诉我。

EDIT after you posted your code 发布代码后编辑

Try replacing your function with this: 尝试用以下方法替换您的功能:

$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top -50
        }, 1500, 'easeInOutExpo');

        if($($anchor).closest('ul').hasClass('dropdown-menu')){
           $($anchor).closest('ul').parent().siblings().removeClass('active'); 
           $($anchor).closest('ul').parent().addClass('active'); 
        }else{
           $($anchor).parent().siblings().removeClass('active');
           $($anchor).parent().addClass('active');
        }
        event.preventDefault();
    });
});

here is a work around this problem. 这是一个解决这个问题的方法。 just change the contents of your scrolling-nav.js to the following: 只需将scrolling-nav.js的内容更改为以下内容:

//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
if ($(".navbar").offset().top > 50) {
    $(".navbar-fixed-top").addClass("top-nav-collapse");
} else {
    $(".navbar-fixed-top").removeClass("top-nav-collapse");
}
});

//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
$('a.page-scroll').bind('click', function(event) {
    var $anchor = $(this);
    $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset().top -50
    }, 1500, 'easeInOutExpo', function(){
        $('ul.navbar-nav li, ul.dropdown-menu li').removeClass('active');
        $($anchor).parent('li').addClass('active');
    });
    event.preventDefault();
}); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM