diff --git a/src/htmx.js b/src/htmx.js index fc1e855d2..492f6e3d3 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -1192,6 +1192,8 @@ var htmx = (() => { __handleScroll(swapSpec, target) { if (swapSpec.scroll) { let scrollTarget = swapSpec.scrollTarget ? this.__findExt(target, swapSpec.scrollTarget) : target; + // window has no scrollTop or scrollIntoView, so use the document's scrolling element + if (scrollTarget === window) scrollTarget = document.scrollingElement; if (scrollTarget) { if (swapSpec.scroll === 'top') { scrollTarget.scrollTop = 0; @@ -1202,6 +1204,7 @@ var htmx = (() => { } if (swapSpec.show === 'top' || swapSpec.show === 'bottom') { let showTarget = swapSpec.showTarget ? this.__findExt(target, swapSpec.showTarget) : target; + if (showTarget === window) showTarget = document.scrollingElement; showTarget?.scrollIntoView?.(swapSpec.show === 'top') } } diff --git a/test/tests/attributes/hx-swap.js b/test/tests/attributes/hx-swap.js index 0b973e7d1..9d6374213 100644 --- a/test/tests/attributes/hx-swap.js +++ b/test/tests/attributes/hx-swap.js @@ -74,6 +74,36 @@ describe('hx-swap modifiers', function() { assert.equal(container.scrollTop, 0) }) + it('scrollTarget:window scrolls the window', async function () { + mockResponse('GET', '/test', '

New

') + createProcessedHTML(` +
Old
+
spacer
`) + window.scrollTo(0, 1000) + try { + find('#trigger').click() + await forRequest() + assert.equal(window.scrollY, 0) + } finally { + window.scrollTo(0, 0) + } + }) + + it('showTarget:window shows the bottom of the window', async function () { + mockResponse('GET', '/test', '

New

') + createProcessedHTML(` +
Old
+
spacer
`) + window.scrollTo(0, 0) + try { + find('#trigger').click() + await forRequest() + assert.isAbove(window.scrollY, 0) + } finally { + window.scrollTo(0, 0) + } + }) + it('processes scripts in swapped content', async function () { mockResponse('GET', '/test', '
') let div = createProcessedHTML('
Old
'); diff --git a/www/src/content/reference/01-attributes/08-hx-swap.md b/www/src/content/reference/01-attributes/08-hx-swap.md index 6ab1eca3d..ab8d442bb 100644 --- a/www/src/content/reference/01-attributes/08-hx-swap.md +++ b/www/src/content/reference/01-attributes/08-hx-swap.md @@ -260,7 +260,7 @@ Target a different element using a CSS or [extended selector](/docs#targeting-wi Scroll the window: ```html -
+
``` ### `show`