Skip to content

Instantly share code, notes, and snippets.

@spiderneo
Last active November 13, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spiderneo/e5d95e36a00a40ef985af5887238539c to your computer and use it in GitHub Desktop.
Save spiderneo/e5d95e36a00a40ef985af5887238539c to your computer and use it in GitHub Desktop.
--- a/js/single-page-site-scroll.js
+++ b/js/single-page-site-scroll.js
@@ -9,10 +9,11 @@
// On page load, smooth scroll.
/* global destination */
var hash = window.location.hash;
- var heightDifference = $(document).height() - $(window).height();
+
if (hash) {
- Drupal.behaviors.scrollToAnchor.scrollToDestination($(hash).offset().top, heightDifference);
+ setTimeout(Drupal.behaviors.scrollToAnchor.scrollToDestination($(hash).offset().top,300),100);
}
+
// On click, smooth scroll this baby!
$('a[href^="#"], a[href^="/#"]').click(function (event) {
@@ -20,17 +21,18 @@
// Only do this on the single page.
event.preventDefault();
}
+
var hrefValue = $(this).attr('href').replace('/', '');
var strippedHref = hrefValue.replace('#', '').replace('/', '');
if (Drupal.behaviors.scrollToAnchor.validateSelector(hrefValue)) {
if ($(hrefValue).length > 0) {
var linkOffset = $(this.hash).offset().top;
- Drupal.behaviors.scrollToAnchor.scrollToDestination(linkOffset, heightDifference);
+ Drupal.behaviors.scrollToAnchor.scrollToDestination(linkOffset);
}
else if ($('a[name=' + strippedHref + ']').length > 0) {
var linkOffset = $('a[name=' + strippedHref + ']').offset().top;
- Drupal.behaviors.scrollToAnchor.scrollToDestination(linkOffset, heightDifference);
+ Drupal.behaviors.scrollToAnchor.scrollToDestination(linkOffset);
}
}
});
@@ -38,25 +40,37 @@
validateSelector: function (a) {
return /^#[a-z]{1}[a-z0-9_-]*$/i.test(a);
},
- scrollToDestination: function (a, b) {
+ scrollToDestination: function (a, b,time) {
+ if(time == undefined)
+ time = 500;
+ $('header').addClass('pagescroll');
+ b = $(document).height() - $(window).height();
+
if (a > b) {
var destination = b;
}
else {
var destination = a;
}
+
if (drupalSettings.singlePage.offsetSelector) {
// Take offset selector height into account.
if ($(document).find(drupalSettings.singlePage.offsetSelector).length) {
destination -= $(drupalSettings.singlePage.offsetSelector).height();
}
}
- $('html,body').animate({scrollTop: destination}, 800, 'swing');
+ $('html,body').animate({scrollTop: destination}, {
+ duration: time,
+ easing: "easeInQuad"
+ });
}
};
$(function () {
// Init scroll behaviour.
- Drupal.behaviors.scrollToAnchor.init();
+
+ window.addEventListener('load', function(event){
+ Drupal.behaviors.scrollToAnchor.init();
+ });
});
}(jQuery));
\ No newline at end of file
diff --git a/js/single-page-site-scrollspy.js b/js/single-page-site-scrollspy.js
index e6c5bdf..6a30914 100644
--- a/js/single-page-site-scrollspy.js
+++ b/js/single-page-site-scrollspy.js
@@ -6,13 +6,15 @@
Drupal.behaviors.Scrollspy = {
attach: function (context, settings) {
// Set first menu item active on page load.
- $(drupalSettings.singlePage.menuClass + ' li:nth-child(1) a').addClass('active');
-
+ //$(drupalSettings.singlePage.menuClass + ' li:nth-child(1) a').addClass('active');
+
$('.single-page-wrapper').each(function (i) {
var position = $(this).position();
+
+ var page = $(this);
$(this).scrollspy({
- min: position.top,
- max: position.top + $(this).height(),
+ min: function() { return page.position().top - 100},
+ max: function() { return page.position().top -100 + page.height() },
onEnter: function (element, position) {
var id = element.id;
$(drupalSettings.singlePage.menuClass + ' li a[data-active-item="' + id + '"]').addClass('active');
@@ -34,4 +36,4 @@
});
}
};
-})(jQuery);
+})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment