问题
I'm making a simply UWP (Universal Windows 10 App) web app using a webview. How can I return on the top of the page clicking on a button? I can't find it on the MSDN.
回答1:
You can use window.scrollTo(0,0)
method, it is a JavaScript method that scrolls the document to position "0" horizontally and "0" vertically.
You can interact with the content of the WebView by using the InvokeScriptAsync method to invoke or inject script into the WebView
content.
For example:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="theWebView" Source="https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.aspx">
</WebView>
<Button Content="To The Top" Click="Button_Click"></Button>
</Grid>
Code behind:
private string ScrollToTopString = @"window.scrollTo(0,0);";
private async void Button_Click(object sender, RoutedEventArgs e)
{
await theWebView.InvokeScriptAsync("eval", new string[] { ScrollToTopString });
}
来源:https://stackoverflow.com/questions/36500747/how-to-programmatically-scroll-uwp-webview