Skip to content

Instantly share code, notes, and snippets.

@mantismamita
Created February 16, 2018 15:15
Show Gist options
  • Save mantismamita/f93285d7ac409cbc590da316f2987d63 to your computer and use it in GitHub Desktop.
Save mantismamita/f93285d7ac409cbc590da316f2987d63 to your computer and use it in GitHub Desktop.
function stickSharebar () {
if ($W.scrollTop() < parseInt($offset)) {
$el.addClass('in-article')
$menuBtn.css('bottom', '20px')
} else {
$el.removeClass('in-article')
$menuBtn.css('bottom', '60px')
}
if (($W.scrollTop() > parseInt($offset + 1)) && (window.matchMedia('(min-width: 620px)').matches)) {
$el.addClass('atop-article')
} else {
$el.removeClass('atop-article')
}
if (($W.scrollTop() > parseInt($offset + 1)) && (window.matchMedia('(min-width: 1200px)').matches)) {
$prime.addClass('container')
} else {
$prime.removeClass('container')
}
}
/**
* The sharebar is onscreen only when the $scrollTarget is within the viewport
*/
function sharebarOnScroll () {
if ($W.scrollTop() < $scrollTarget.offset().top ||
$W.scrollTop() > $scrollTarget.offset().top + $scrollTarget.outerHeight()) {
$el.addClass('in-article')
} else { $el.removeClass('in-article') }
}
/**
* Called when the window is resized
*/
function sharebarOnResize () {
// use a CSS prop as breakpoint detection
if ($list.css('position') === 'fixed') {
$list.attr('aria-hidden', $list.attr('aria-hidden') || 'true')
} else {
$list.removeAttr('aria-hidden')
}
sharebarOnScroll()
onToggleShareBar()
}
function onToggleShareBar () {
// Prevent the page content from scrolling when the sharebar is expanded
if ($list.attr('aria-hidden') === 'false') {
$B.css('top', -$W.scrollTop()).addClass('overlay sharebar--noscroll')
} else if ($B.hasClass('sharebar--noscroll')) {
const scrollTop = Math.abs(parseInt($B.css('top')))
$B.removeClass('overlay sharebar--noscroll')
!isNaN(scrollTop) && $B.css('top', '').scrollTop(scrollTop)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment