добавил атрибут data-scroll-top опциональный к элементам которых могут вызвать hx-swap, наличие атрибута говорит о том, что после свапа надо проскроллить страничку в самый верх

design
serr 2025-06-08 00:22:39 +03:00
parent 71084be5e3
commit e88a869167
3 changed files with 24 additions and 21 deletions

View File

@ -6,6 +6,14 @@
<link rel="shortcut icon" href="/assets/pic/favicon.ico?v={{ .version }}" type="image/x-icon">
<link rel="stylesheet" href="/assets/css/styles.css?v={{ .version }}" type="text/css">
<script src="/assets/scripts/htmx.js?v={{ .version }}"></script>
<script>document.addEventListener('htmx:afterSwap', function() {window.scrollTo(0, 0);});</script>
<script>
document.addEventListener('htmx:afterSwap', function(evt) {
const triggeringElement = evt.detail.requestConfig.elt;
console.log(triggeringElement);
if (triggeringElement.hasAttribute('data-scroll-top')) {
window.scrollTo(0,0);
}
});
</script>
</head>
{{ end }}

View File

@ -6,7 +6,11 @@
</div>
<div class="pulse-container">
<p class="pulse-text">
<code>🎵&nbsp;last played track: </code><code id="lastfm">waiting for a response from server... (JavaScript is required)</code><code>&nbsp;🎵</code>
<code>🎵&nbsp;last played track: </code><code id="lastfm"
hx-get="/api/lastfm"
hx-trigger="load, every 30s"
hx-target="#lastfm"
hx-swap="innerHTML">waiting for a response from server... (JavaScript is required)</code><code>&nbsp;🎵</code>
</p>
</div>
<div>
@ -23,13 +27,15 @@
hx-get="/?ajax=true"
hx-target="main"
hx-swap="outerHTML"
hx-push-url="/">
hx-push-url="/"
data-scroll-top="true">
main page</a> or go to
<a href="/posts/0"
hx-get="/posts/0?ajax=true"
hx-target="main"
hx-swap="outerHTML"
hx-push-url="/posts/0">
hx-push-url="/posts/0"
data-scroll-top="true">
posts section
</a>
</p>
@ -39,19 +45,5 @@
and also you can subscribe to my <a href="https://t.me/lolistack" target="_blank">telegram channel</a> with pictures!
</p>
</div>
<script>
function updateLastfm() {
fetch('/api/lastfm')
.then(response => response.text())
.then(text => {
document.getElementById('lastfm').textContent = text;
})
.catch(error => {
console.error('Error fetching lastfm:', error);
});
}
updateLastfm();
setInterval(updateLastfm, 30000);
</script>
</header>
{{ end }}

View File

@ -17,7 +17,8 @@
hx-get="{{ $post.Link }}?ajax=true"
hx-target="main"
hx-swap="outerHTML"
hx-push-url="{{ $post.Link }}">read more</a>
hx-push-url="{{ $post.Link }}"
data-scroll-top="true">read more</a>
</p>
<p>
<code>create time: {{ $post.Timestamp }}</code>
@ -29,13 +30,15 @@
hx-get="/posts/{{ .prevPageNumber }}?ajax=true"
hx-target="main"
hx-swap="outerHTML"
hx-push-url="/posts/{{ .prevPageNumber }}"><-</a>
hx-push-url="/posts/{{ .prevPageNumber }}"
data-scroll-top="true"><-</a>
{{ .pageNumberInc }}/{{ .pagesCount }}
<a href="/posts/{{ .nextPageNumber }}"
hx-get="/posts/{{ .nextPageNumber }}?ajax=true"
hx-target="main"
hx-swap="outerHTML"
hx-push-url="/posts/{{ .nextPageNumber }}">-></a>
hx-push-url="/posts/{{ .nextPageNumber }}"
data-scroll-top="true">-></a>
</div>
</main>
{{ end }}