mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 09:38:19 +08:00
ui-default: init
将ui-default合并至主存储库便于维护
This commit is contained in:
1 parent
6317c9057f
commit
5faa54c7ca
564 files changed
+23083
-14
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import 'jquery.easing';
|
||||
|
||||
import { AutoloadPage } from 'vj/misc/Page';
|
||||
|
||||
const smoothScrollPage = new AutoloadPage('smoothScrollPage', null, () => {
|
||||
const HISTORY_SUPPORT = !!(typeof window.history === 'object' && window.history.pushState);
|
||||
const ANCHOR_REGEX = /^#[^ ]+$/;
|
||||
const OFFSET_HEIGHT = 10 + ($('.nav').height() || 0);
|
||||
|
||||
function scrollIfAnchor(href, pushToHistory) {
|
||||
if (!ANCHOR_REGEX.test(href)) {
|
||||
return false;
|
||||
}
|
||||
const match = document.getElementById(href.slice(1));
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
const rect = match.getBoundingClientRect();
|
||||
const anchorOffset = window.pageYOffset + rect.top - OFFSET_HEIGHT;
|
||||
$('html,body').animate({ scrollTop: anchorOffset }, 200, 'easeOutCubic');
|
||||
if (HISTORY_SUPPORT && pushToHistory) {
|
||||
window.history.pushState({}, document.title, window.location.pathname + href);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function scrollToCurrent() {
|
||||
scrollIfAnchor(window.location.hash);
|
||||
}
|
||||
|
||||
function delegateAnchors(e) {
|
||||
if (e.metaKey || e.ctrlKey || e.shiftKey) {
|
||||
return;
|
||||
}
|
||||
const elem = e.target;
|
||||
if (
|
||||
elem.nodeName === 'A'
|
||||
&& scrollIfAnchor(elem.getAttribute('href'), true)
|
||||
) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
|
||||
$(document).on('vjPageFullyInitialized', () => {
|
||||
scrollToCurrent();
|
||||
window.addEventListener('hashchange', scrollToCurrent);
|
||||
document.body.addEventListener('click', delegateAnchors);
|
||||
});
|
||||
});
|
||||
|
||||
export default smoothScrollPage;
|
||||
Reference in new issue
Block a user