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(` +New
') + createProcessedHTML(` +