Advertisement
csako

Mobilarena anchor fix

Apr 14th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Fototrend Anchor Scroll
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0
  5. // @description  try to take over the world!
  6. // @author       levihu
  7. // @match        https://fototrend.hu/*
  8. // @match        https://mobilarena.hu/*
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     'use strict';
  14.  
  15. (function(document, history, location) {
  16.   var HISTORY_SUPPORT = !!(history && history.pushState);
  17.  
  18.   var anchorScrolls = {
  19.     ANCHOR_REGEX: /^#[^ ]+$/,
  20.     OFFSET_HEIGHT_PX: 50,
  21.  
  22.     /**
  23.      * Establish events, and fix initial scroll position if a hash is provided.
  24.      */
  25.     init: function() {
  26.       this.scrollToCurrent();
  27.       $(window).on('hashchange', $.proxy(this, 'scrollToCurrent'));
  28.       $('body').on('click', 'a', $.proxy(this, 'delegateAnchors'));
  29.     },
  30.  
  31.     /**
  32.      * Return the offset amount to deduct from the normal scroll position.
  33.      * Modify as appropriate to allow for dynamic calculations
  34.      */
  35.     getFixedOffset: function() {
  36.       return this.OFFSET_HEIGHT_PX;
  37.     },
  38.  
  39.     /**
  40.      * If the provided href is an anchor which resolves to an element on the
  41.      * page, scroll to it.
  42.      * @param  {String} href
  43.      * @return {Boolean} - Was the href an anchor.
  44.      */
  45.     scrollIfAnchor: function(href, pushToHistory) {
  46.       var match, anchorOffset;
  47.  
  48.       if(!this.ANCHOR_REGEX.test(href)) {
  49.         return false;
  50.       }
  51.  
  52.       match = document.getElementById(href.slice(1));
  53.  
  54.       if(match) {
  55.         anchorOffset = $(match).offset().top - this.getFixedOffset();
  56.         $('html, body').animate({ scrollTop: anchorOffset});
  57.  
  58.         // Add the state to history as-per normal anchor links
  59.         if(HISTORY_SUPPORT && pushToHistory) {
  60.           history.pushState({}, document.title, location.pathname + href);
  61.         }
  62.       }
  63.  
  64.       return !!match;
  65.     },
  66.  
  67.     /**
  68.      * Attempt to scroll to the current location's hash.
  69.      */
  70.     scrollToCurrent: function(e) {
  71.       if(this.scrollIfAnchor(window.location.hash) && e) {
  72.         e.preventDefault();
  73.       }
  74.     },
  75.  
  76.     /**
  77.      * If the click event's target was an anchor, fix the scroll position.
  78.      */
  79.     delegateAnchors: function(e) {
  80.       var elem = e.target;
  81.  
  82.       if(this.scrollIfAnchor(elem.getAttribute('href'), true)) {
  83.         e.preventDefault();
  84.       }
  85.     }
  86.   };
  87.  
  88.     $(document).ready($.proxy(anchorScrolls, 'init'));
  89. })(window.document, window.history, window.location);
  90. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement