【问题标题】:Wordpress single page navigation and jQuery scrollspyWordpress 单页导航和 jQuery scrollspy
【发布时间】:2014-05-03 09:48:12
【问题描述】:

嗨!

如果我正在工作,我必须为公司创建一个网站:)

我的问题:

我用 Wordpress 创建了http://webl.eu/wordpress/,它是来自“二十三”的子主题。

当用户滚动到相应的元素时,我想使导航 li 或 a 发生变化。我找到了这个脚本:

(function($){

  var sections = [];
  var id = false;
  var $navbar = $('#navigation_desktop .navigation');
  var $navbara = $('a', $navbar);

  $navbara.click(function(e){
    e.preventDefault();
    $('html, body').animate({
      scrollTop: $($(this).attr('href')).offset().top - $navbar.height()
    });
    hash($(this).attr('href'));
  });

  $navbara.each(function(){
    sections.push($($(this).attr('href'))); // !!!erreur ici!!!
  });

    console.log(sections);
    
  $(window).scroll(function(e){
    var scrollTop = $(this).scrollTop() + ($(window).height() / 2)
    for(var i in sections){
      var section = sections[i];
      if (scrollTop > section.offset().top) {  // !!!erreur ici!!!
        scrolled_id = section.attr('id');
      }
    }
    if (scrolled_id !== id) {
      id = scrolled_id
      $navbara.removeClass('current');
      $('a[href="#' + id + '"]', $navbar).addClass('current');
    }
  });

})(jQuery);


hash = function(h) {
  if (history.pushState) {
    history.pushState(null, null, h);
  }else{
    location.hash = h;
  }
}

我认为它应该可以工作......但它没有:(

谢谢,

狮子座

【问题讨论】:

标签: jquery wordpress navigation singlepage scrollspy


【解决方案1】:

我解决了我的问题:

这很简单:如果菜单中有锚点以外的任何其他内容,则此脚本不起作用!

希望对你有用;)

再见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多