user7082139
user7082139

Reputation:

Smooth scrollTo in WebView UWP

scrollTo in WebView UWP may be achieved through:

private string ScrollToTopString = @"window.scrollTo(0,0);";
private async void ButtonClick(object sender, RoutedEventArgs e)
{
    await WebViewTest.InvokeScriptAsync("eval", new string[] { ScrollToTopString });
}

But what about animated/Smooth scrolling in WebView UWP? In Android that is achieved either officially or through variations (for example, using android.animation.ObjectAnimator), whereas in UWP only ScrollViewer seems to support it as far as I know. Example 1 Example 2

Any ideas?

Upvotes: 2

Views: 481

Answers (1)

RareNCool
RareNCool

Reputation: 436

You can't animate the WebView because the WebView itself isn't actually scrolling, just like your browser doesn't actually scroll; it's the "window" element that's scrolling.

However, if I understand what you're wanting correctly, just replace your ScrollToTopString with this:

var ScrollToTopString = @"var int = setInterval(function() { 
window.scrollBy(0, -5);

if( window.pageYOffset === 0 )
    clearInterval(int);
}, 1);";

This will scroll that "window" element I mentioned. You can raise the speed of the interval (I have it set to 1) to slow down the animation, or lower the value of scrollBy to make it go faster.

Upvotes: 2

Related Questions