mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-09 17:45:32 -04:00
Refactor stick-at-top to remove top-specific code
This commit is contained in:
@@ -31,6 +31,12 @@
|
|||||||
if ($els.length > 0) {
|
if ($els.length > 0) {
|
||||||
sticky.$els = $els
|
sticky.$els = $els
|
||||||
|
|
||||||
|
$els.each(function (i, el) {
|
||||||
|
var $el = $(el)
|
||||||
|
|
||||||
|
$el.data('scrolled-from', sticky.getElementOffset($el).top)
|
||||||
|
})
|
||||||
|
|
||||||
if (sticky._scrollTimeout === false) {
|
if (sticky._scrollTimeout === false) {
|
||||||
$(global).scroll(sticky.onScroll)
|
$(global).scroll(sticky.onScroll)
|
||||||
sticky._scrollTimeout = global.setInterval(sticky.checkScroll, 50)
|
sticky._scrollTimeout = global.setInterval(sticky.checkScroll, 50)
|
||||||
@@ -62,21 +68,24 @@
|
|||||||
onResize: function () {
|
onResize: function () {
|
||||||
sticky._hasResized = true
|
sticky._hasResized = true
|
||||||
},
|
},
|
||||||
|
scrolledFromInsideWindow: function (scrolledFrom) {
|
||||||
|
var windowVerticalPosition = sticky.getWindowPositions().scrollTop
|
||||||
|
|
||||||
|
return windowVerticalPosition < scrolledFrom
|
||||||
|
},
|
||||||
checkScroll: function () {
|
checkScroll: function () {
|
||||||
if (sticky._hasScrolled === true) {
|
if (sticky._hasScrolled === true) {
|
||||||
sticky._hasScrolled = false
|
sticky._hasScrolled = false
|
||||||
|
|
||||||
var windowVerticalPosition = sticky.getWindowPositions().scrollTop
|
|
||||||
|
|
||||||
var windowDimensions = sticky.getWindowDimensions()
|
var windowDimensions = sticky.getWindowDimensions()
|
||||||
|
|
||||||
sticky.$els.each(function (i, el) {
|
sticky.$els.each(function (i, el) {
|
||||||
var $el = $(el)
|
var $el = $(el)
|
||||||
var scrolledFrom = $el.data('scrolled-from')
|
var scrolledFrom = $el.data('scrolled-from')
|
||||||
|
|
||||||
if (scrolledFrom && windowVerticalPosition < scrolledFrom) {
|
if (scrolledFrom && sticky.scrolledFromInsideWindow(scrolledFrom)) {
|
||||||
sticky.release($el)
|
sticky.release($el)
|
||||||
} else if (windowDimensions.width > 768 && windowVerticalPosition >= sticky.getElementOffset($el).top) {
|
} else if (windowDimensions.width > 768 && !sticky.scrolledFromInsideWindow(scrolledFrom)) {
|
||||||
sticky.stick($el)
|
sticky.stick($el)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -106,18 +115,20 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
addShimForEl: function ($el, width, height) {
|
||||||
|
$el.before('<div class="shim" style="width: ' + width + 'px; height: ' + height + 'px"> </div>')
|
||||||
|
},
|
||||||
stick: function ($el) {
|
stick: function ($el) {
|
||||||
if (!$el.hasClass('content-fixed')) {
|
if (!$el.hasClass('content-fixed')) {
|
||||||
$el.data('scrolled-from', sticky.getElementOffset($el).top)
|
|
||||||
var height = Math.max($el.height(), 1)
|
var height = Math.max($el.height(), 1)
|
||||||
var width = $el.width()
|
var width = $el.width()
|
||||||
$el.before('<div class="shim" style="width: ' + width + 'px; height: ' + height + 'px"> </div>')
|
|
||||||
|
sticky.addShimForEl($el, width, height)
|
||||||
$el.css('width', width + 'px').addClass('content-fixed')
|
$el.css('width', width + 'px').addClass('content-fixed')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
release: function ($el) {
|
release: function ($el) {
|
||||||
if ($el.hasClass('content-fixed')) {
|
if ($el.hasClass('content-fixed')) {
|
||||||
$el.data('scrolled-from', false)
|
|
||||||
$el.removeClass('content-fixed').css('width', '')
|
$el.removeClass('content-fixed').css('width', '')
|
||||||
$el.siblings('.shim').remove()
|
$el.siblings('.shim').remove()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user