平滑滚动到锚点。不同的偏移量。媒体查询。排除一些锚点

Smooth Scrolling to anchors. Different offsets. Media Queries. Excluding some anchors

情况: 我想要平滑滚动到锚点 links,对于每个锚点 link。接下来我想为特定的锚点 links 设置一个偏移量(例如只有导航的 links,但是网站上的锚点 links 的 none) .最后我想添加媒体查询.. 所以偏移位置只能在定义的浏览器大小下工作(例如 "max-width: 767px")。

第一个问题:我的平滑滚动只有在其他功能(偏移定位)被禁用时才有效。两者一起不起作用。有什么帮助吗? 第二个问题:我不知道如何将"offset positioning"减少到"navigation"锚点link。

// Smooth Scrolling
$(function () {
  'use strict';
  $('a[href*=#]').click(function () {
    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
        }, 300);  
        return false;
      }
    }
  });
});

// Offset Positioning
function offsetAnchor() {
  'use strict';
  if (location.hash.length !== 0) {
    window.scrollTo(window.scrollX, window.scrollY - 0); 
  }
}

// Offset Positioning with media query
function offsetAnchor() {
  'use strict';
  if (matchMedia('only screen and (max-width: 767px)').matches) {
    if (location.hash.length !== 0) {
      window.scrollTo(window.scrollX, window.scrollY - 220);
    }
  }
}

// This will capture hash changes while on the page
$(window).on("hashchange", function () {
  'use strict';
  offsetAnchor();
});

我通过在此处和其他网站上搜索获得了代码,不是我自己编写的。我想尽快学习 javascript 和 jquery 的基础知识。但如果现在能得到你们所有人的帮助,那就太好了。十分感谢!

鲍里斯

好的,我在这里找到了一些其他代码: Smooth scrolling when clicking an anchor link

我已经复制了一次以添加一些 media-query、offset 和特定的 class (ul.nav a). 我希望有没问题 - 直到现在它对我来说工作得很好。希望这是一个有用的解决方案!甚至代码更小。

只有一个 "problem": 页面滚动两次。起初它滚动到锚点,第二次它再次向上滚动 220px(偏移量)。 如果页面只滚动一次就直接到偏移位置就好了!

// Smooth Scrolling
var $root = $('html, body');
$('a').click(function () {
  'use strict';
  $root.animate({
    scrollTop: $($.attr(this, 'href')).offset().top
  }, 500);
  return false;
});

// Smooth Scrolling with offset and media-query
var $root = $('html, body');
$('ul.nav a').click(function () {
  'use strict';
  if (matchMedia('only screen and (max-width: 767px)').matches) {
    $root.animate({
      scrollTop: $($.attr(this, 'href')).offset().top - 220
    }, 500);
    return false;
  }
});

我的平滑滚动问题终于得到了优化。我认为媒体查询更加清晰易懂。此外,奇怪的 "scroll down and scroll some pixel up again"-效果现在消失了。

// smooth scrolling

function screenMin768() {
  'use strict';
  var mq = window.matchMedia("(min-width: 768px)");
  return mq.matches;
}

function screenMax767() {
  'use strict';
  var mq = window.matchMedia("(max-width: 767px)");
  return mq.matches;
}

console.log(screenMin768() + " " + screenMax767());

if (screenMin768()) {
  var $root = $('html, body');
  $('a').click(function () {
    'use strict';
    $root.animate({
      scrollTop: $($.attr(this, 'href')).offset().top - 55 // hier die variable aufrufen: "+ offset55"
    }, 500);
    return false;
  });
}

// offset for normal a-tags, excluding mobile nav: ul.nav a
if (screenMax767()) {
  var $root = $('html, body');
  $('a:not(ul.nav a)').click(function () {
    'use strict';
    $root.animate({
      scrollTop: $($.attr(this, 'href')).offset().top + 4
    }, 500);
    return false;
  });
}

// offset for mobile nav: ul.nav a
if (screenMax767()) {
  var $root = $('html, body');
  $('ul.nav a').click(function () {
    'use strict';
    $root.animate({
      scrollTop: $($.attr(this, 'href')).offset().top - 270
    }, 500);
    return false;
  });
}

// offset correction for go-to-top button on mobile screen width
if (screenMax767()) {
  var $root = $('html, body');
  $('a.go-to-top').click(function () {
    'use strict';
    $root.animate({
      scrollTop: $($.attr(this, 'href')).offset().top - 60
    }, 500);
    return false;
  });
}

我只是一个设计师,所以我通过反复试验得到了它。我不明白每一行,例如"console.log ..."。而且我敢打赌代码可以减少更多。

所以如果有人想优化/减少代码,那就太好了! :)