JavaScript Smooth Scrolling to anchors. Different offsets. Media Queries. Excluding some anchors smooth scrolling anchors,smooth scrolling to page anchors,smooth scrolling

Situation: I want to get smooth scrolling to anchor links, for every anchor link. Next i want to set an offset for specific anchor links (for example only the links of the navigation, but none of the anchor links on the site). And at last i want to add media queries.. so the offset position should only work at defined browser sizes (for example "max-width: 767px").

First problem: my smooth scrolling only works, if the other function (offset positioning) is disabled. Both together does not work. Any help? Second problem: I don't know how to reduce "offset positioning" to "navigation" anchor links only.

// 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();
});

I got the code by searching here and other sites, i didn't write it myself. I want to learn the basics of javascript and jquery soon. But it would be great to get help right now from you all. Thank you a lot!

boris

Answer:1

Ok, i found some other code here: Smooth scrolling when clicking an anchor link

And I've duplicated it once to add some media-query, offset and specific class (ul.nav a). I hope there are no problems - until now it works very fine for me. Hope this is a usefull solution! Even the code is smaller.

Only one "problem": The page scrolls two times. At first it scrolls to the anchor and a second time it scrolls 220px up again (the offset). It would be great if the page would only scroll one time to the offset position directly!

// 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;
  }
});
Answer:2

Suppose I able to get ($.ajax), process (process_*) and save (store_* =) data A and B independently and already have API: var store_A; function A() { $.ajax({url: "/getA"}).done(function(data) { ...

Suppose I able to get ($.ajax), process (process_*) and save (store_* =) data A and B independently and already have API: var store_A; function A() { $.ajax({url: "/getA"}).done(function(data) { ...

These three functions all work on Chrome, but not IE or Firefox. The first functions adds a cloned row. The second function removes a cloned row. The third function resets the entire field. All ...

These three functions all work on Chrome, but not IE or Firefox. The first functions adds a cloned row. The second function removes a cloned row. The third function resets the entire field. All ...

  1. adding and removing rows and columns in excel

I have been trying to draw a very basic rectangle using THREE.BufferGeometry(). Here is the code - var geometry = new THREE.BufferGeometry(); var material = new THREE.MeshBasicMaterial({color: 'rgb(...

I have been trying to draw a very basic rectangle using THREE.BufferGeometry(). Here is the code - var geometry = new THREE.BufferGeometry(); var material = new THREE.MeshBasicMaterial({color: 'rgb(...

I'm running into trouble trying to use Material Design Lite and AngularJS. After googling for a bit, I found another stack overflow post regarding a similar issue: Material Design Lite Integration ...

I'm running into trouble trying to use Material Design Lite and AngularJS. After googling for a bit, I found another stack overflow post regarding a similar issue: Material Design Lite Integration ...

  1. using material design icons
  2. using material design with react
  3. using material design with angular
  4. using material design in android
  5. using material design for web
  6. using material design in android studio
  7. using material design wpf
  8. using material design icons in react
  9. using material design in xamarin forms
  10. using material design in blazor
  11. using material design with django
  12. using material design with vue.js 2
  13. using material design
  14. using material design on ios
  15. using material design in angular 6
  16. using material design icons in angular
  17. using material design in sketch
  18. using material design for websites
  19. using material design in angular 4
  20. using material design css