Skip to content

Commit

Permalink
Add scrollBy() #1255
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjbradshaw committed May 24, 2024
1 parent af7bcbc commit 3f44766
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"compat",
"consts",
"filesize",
"Loggable",
"sarif",
"sonarjs"
Expand Down
7 changes: 7 additions & 0 deletions example/child/frame.absolute.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
>Bottom</a
>
&nbsp;
<a
href="#"
class="back"
onclick="if ('parentIFrame' in window) window.parentIFrame.scrollBy(0,50);return false;"
>scrollBy(50)</a
>
&nbsp;
<a
href="#"
class="back"
Expand Down
6 changes: 4 additions & 2 deletions example/child/frame.nested.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ <h2>Nested iFrame</h2>
*/

const iframes = window.iframeResize({
license: 'GPLv3',
inPageLinks: true,
log: true,

// Callback fn when iFrame resized, update the data being displayed
onResized(messageData) {
Expand All @@ -85,8 +87,8 @@ <h2>Nested iFrame</h2>
window.iFrameResizer = {
onMessage: (message) =>
iframes.forEach((iframe) =>
iframe.iFrameResizer?.sendMessage(`${message} (via parent ${id} )`),
),
iframe.iFrameResizer?.sendMessage(`${message} (via parent ${id} )`),
),
}
</script>
</body>
Expand Down
1 change: 1 addition & 0 deletions example/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ <h2>Automagically resizing iFrame</h2>
// offset: 10, // Alter the returned height
// direction: 'none', // 'vertical' or 'horizontal' or 'none'
license: 'GPLv3',
log: true,

onResized(messageData) {
// Callback fn when resize is received
Expand Down
5 changes: 5 additions & 0 deletions packages/child/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ declare module '@iframe-resizer/child' {
*/
getParentProperties(callback: (data: ParentProperties) => void): void

/**
* Scroll the parent page bys x and y
*/
scrollBy(x: number, y: number): void

/**
* Scroll the parent page to the coordinates x and y
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/child/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ The <b>getPageInfo()</> method has been deprecated and replaced with <b>getPare
resetIFrame('parentIFrame.reset')
},

scrollBy(x, y) {
sendMsg(y, x, 'scrollBy') // X&Y reversed at sendMsg uses height/width
},

scrollTo(x, y) {
sendMsg(y, x, 'scrollTo') // X&Y reversed at sendMsg uses height/width
},
Expand Down
16 changes: 16 additions & 0 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,17 @@ function iframeListener(event) {
}
}

function scrollBy() {
const x = messageData.width
const y = messageData.height

const target = window.parentIFrame || window

log(iframeId, `Scroll request received from parent: x: ${x} y: ${y}`)

target.scrollBy(x, y)
}

function scrollRequestFromChild(addOffset) {
/* istanbul ignore next */ // Not testable in Karma
function reposition() {
Expand Down Expand Up @@ -508,6 +519,10 @@ See <u>https://iframe-resizer.com/setup/#child-page-setup</> for more details.
settings[iframeId].autoResize = JSON.parse(getMsgBody(9))
break

case 'scrollBy':
scrollBy()
break

case 'scrollTo':
scrollRequestFromChild(false)
break
Expand Down Expand Up @@ -1036,6 +1051,7 @@ The <b>sizeWidth</>, <b>sizeHeight</> and <b>autoResize</> options have been rep
}

function processOptions(options) {
console.log('options', options)

Check warning

Code scanning / ESLint

Disallow the use of `console` Warning

Unexpected console statement.
settings[iframeId] = {
iframe,
firstRun: true,
Expand Down

0 comments on commit 3f44766

Please sign in to comment.