Skip to content

Commit 2f2718f

Browse files
committed
Scroll to content after ajax load
1 parent 7cf33da commit 2f2718f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

public/js/app.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ function loadAjax(url, selector, callback = null, params = {}, method = "get") {
662662
}
663663

664664
if (!url) url = window.location.href;
665-
console.log(selector);
665+
666666
fetch(url, options).
667667
then((response) => {
668668
if (!response.ok) { throw new Error(response) }
@@ -688,6 +688,8 @@ function loadAjax(url, selector, callback = null, params = {}, method = "get") {
688688
currentElement.replaceWith(newElement);
689689
}
690690
}
691+
692+
if (callback) callback();
691693
}
692694

693695
window.dispatchEvent(new CustomEvent("vvveb.loadUrl", {detail: {url, selector}}));
@@ -704,7 +706,13 @@ document.addEventListener("click", function (e) {
704706

705707
if (!url) return;
706708

707-
loadAjax(url, selector, () => window.history.pushState({url, selector}, null));
709+
loadAjax(url, selector, () => {
710+
if (element.dataset.scroll) {
711+
let target = document.querySelector(selector);
712+
target.scrollIntoView({behavior: "smooth", block: element.dataset.scroll ?? "center", inline: "center"});
713+
}
714+
window.history.pushState({url, selector}, null);
715+
});
708716

709717
e.preventDefault();
710718
}

public/js/filter.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ document.addEventListener("click", (event) => {
7373
if (target) {
7474
let url = target.href;
7575
let selector = "#site-content";
76-
loadAjax(url, selector);
76+
loadAjax(url, selector, () => {
77+
//let target = document.querySelector(selector);
78+
let target = document.querySelector("body");
79+
target.scrollIntoView({behavior: "smooth", block: "start", inline: "start"});
80+
});
7781
window.history.pushState({url, selector}, null, url);
7882
event.preventDefault();
7983
}
80-
});
84+
});

0 commit comments

Comments
 (0)