首页 > 解决方案 > Jquery滚动到偏移目标div总是

问题描述

我添加了一个新的粘性菜单,并且我希望辅助子菜单(在加载时具有偏移)在滚动时保持偏移。

我有点n00b,我不明白。

我使用这个链接作为参考: Jquery scroll to offset target div on page load

    // Goddamn scroll-to links

    // Get offset height depending on our sweet ass screen width
    if (window.innerWidth < 768) {
            var theOffset = 16;
        } else if ( $(this).is(':first')) {
           //var offset = $('.entry-header').height()  + 30;
        } else {
            var theOffset = $('.subheader').height() + 30;
    }

    $('a[href*=#]:not([href=#])').click(function() {

        //$('.subheader li').removeClass('active');
        //$(this).closest('li').addClass('active');
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
            || location.hostname == this.hostname) {

            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
               if (target.length) {
                 $('html,body').animate({
                     scrollTop: (target.offset().top - theOffset)
                }, 500);
                return false;
            }

        }

    });

and this:

if(window.location.hash) {
         $('html,body').animate({
          scrollTop: $(window.location.hash).offset().top-theOffset
      }, 900, 'swing');
    }

任何帮助,将不胜感激

标签: jqueryscrolloffset

解决方案


推荐阅读