mirror of
https://github.com/flarum/framework.git
synced 2025-05-21 22:36:01 +08:00
Massive JavaScript cleanup
- Use JSX for templates - Docblock/comment everything - Mostly passes ESLint (still some work to do) - Lots of renaming, refactoring, etc. CSS hasn't been updated yet.
This commit is contained in:
22
js/lib/utils/anchorScroll.js
Normal file
22
js/lib/utils/anchorScroll.js
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* The `anchorScroll` utility saves the scroll position relative to an element,
|
||||
* and then restores it after a callback has been run.
|
||||
*
|
||||
* This is useful if a redraw will change the page's content above the viewport.
|
||||
* Normally doing this will result in the content in the viewport being pushed
|
||||
* down or pulled up. By wrapping the redraw with this utility, the scroll
|
||||
* position can be anchor to an element that is in or below the viewport, so
|
||||
* the content in the viewport will stay the same.
|
||||
*
|
||||
* @param {DOMElement} element The element to anchor the scroll position to.
|
||||
* @param {Function} callback The callback to run that will change page content.
|
||||
*/
|
||||
export default function anchorScroll(element, callback) {
|
||||
const $element = $(element);
|
||||
const $window = $(window);
|
||||
const relativeScroll = $element.offset().top - $window.scrollTop();
|
||||
|
||||
callback();
|
||||
|
||||
$window.scrollTop($element.offset().top - relativeScroll);
|
||||
}
|
Reference in New Issue
Block a user