diff --git a/CHANGELOG.md b/CHANGELOG.md index ab33a24..d1de13d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ * Removed "style-disabled" property and GetDisabledStyle function * Added GoogleFonts field to AppParams -* Added functions: GetWhiteSpace, GetWordBreak +* Added functions: GetWhiteSpace, GetWordBreak, ScrollIntoViewIfNeeded # v0.20.0 diff --git a/app_scripts.js b/app_scripts.js index 72c42b2..229083e 100644 --- a/app_scripts.js +++ b/app_scripts.js @@ -1280,6 +1280,17 @@ function scrollToEnd(elementId) { } } +function scrollIntoViewIfNeeded(elementId) { + const element = document.getElementById(elementId); + if (element) { + if (element.scrollIntoViewIfNeeded) { + element.scrollIntoViewIfNeeded() + } else { + element.scrollIntoView({block: "nearest", inline: "nearest"}); + } + } +} + function focus(elementId) { const element = document.getElementById(elementId); if (element) { diff --git a/scrollEvent.go b/scrollEvent.go index 2b457ee..10e302a 100644 --- a/scrollEvent.go +++ b/scrollEvent.go @@ -100,3 +100,14 @@ func ScrollViewToEnd(view View, subviewID ...string) { view.Session().callFunc("scrollToEnd", view.htmlID()) } } + +// ScrollIntoViewIfNeeded scrolls the current view into the visible area of the browser window if it's not already within the visible area of the browser window. +// If the element is already within the visible area of the browser window, then no scrolling takes place. +// +// The second argument (subviewID) specifies the path to the child element whose value needs to be returned. +// If it is not specified then a value from the first argument (view) is returned. +func ScrollIntoViewIfNeeded(view View, subviewID ...string) { + if view = getSubview(view, subviewID); view != nil { + view.Session().callFunc("scrollIntoViewIfNeeded", view.htmlID()) + } +}