WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Hide scrollbars in WebView2

Open jpalo opened this issue 5 years ago • 28 comments

How would I hide scrollbars in WebView2, like with classic WebBrowser you set ScrollBarsEnabled=false?

WinForms, C#

AB#26488166

jpalo avatar May 19 '20 20:05 jpalo

Great feature request! You might be able to inject CSS or script to hide scrollbars from within the web content, but we don't have a setting on the WebView2 API to turn off scrollbars.

Can you elaborate on your scenario? How are you using WebView2 and why is it useful to hide scrollbars in your case?

david-risney avatar May 19 '20 20:05 david-risney

In my particular case, I'd prefer to hide the scrollbars to prevent them from rendering. which disrupts the experience when the app is in dark mode since the scrollbars are white.

dstaley avatar May 19 '20 21:05 dstaley

Experience disruption here as well, picture speaks more than words. image

This is how I injected CSS with previous WebBrowser control to modify some other things, I'd be happy to receive tips if I can do something similar with WebView2. In my case injecting JS would work as well.

private void SolarWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    IHTMLDocument2 doc = (solarWebBrowser.Document.DomDocument) as IHTMLDocument2;
    // The first parameter is the url, the second is the index of the added style sheet.
    IHTMLStyleSheet ss = doc.createStyleSheet("", 0);

    ss.cssText = Properties.Settings.Default.SolarGraphAdditionalStyles.Replace(";#GT;#", ">");
}

jpalo avatar May 20 '20 07:05 jpalo

We have CoreWebView2.ExecuteScriptAsync that will execute whatever JavaScript you pass in. Although I'm not sure what the equivalent of the code you have there would be in JS.

At any rate, we'll look into a setting to hide scrollbars.

Thanks!

david-risney avatar May 20 '20 17:05 david-risney

Of course, CoreWebView2.ExecuteScriptAsync worked great and was able to do all the things I needed including setting body overflow style to hidden to hide the scroll bars.

I will close this and leave it for you to decide if separate scrollbar hiding property is needed, I'm leaning towards it not being needed.

jpalo avatar May 20 '20 19:05 jpalo

I see this is closed but I would like to add my request for the ability to disable or hide scrollbars. My Windows Forms app displays content on a wall-mounted TV Monitor using WebView2. No reason for scrollbars to occupy screen space in this scenario.

Thanks.

mikemeinz avatar Aug 10 '20 16:08 mikemeinz

@mikemeinz The workaround works, but you'd like an easier API, is that correct?

champnic avatar Sep 15 '20 01:09 champnic

an API for this would be great.

julesx avatar Nov 25 '20 12:11 julesx

I would like to request an ability to hide scrollbars but retain scroll functionality. Default scrollbars don't fit well with my application UI.

80lvlpalladin avatar Dec 15 '20 01:12 80lvlpalladin

I too would like to see the scroll bars removed. My use case is that I would like my WPF window not to be inflicted with an ugly 1980's styled scroll bar.

darbid avatar Mar 01 '21 06:03 darbid

@darbid What's not to love about a gray rectangle? It probably saves battery life, or something :P

I didn't realize this was still marked as closed, so reopening and it's now tracked on our backlog. Thanks!

champnic avatar Mar 03 '21 20:03 champnic

@mikemeinz The workaround works, but you'd like an easier API, is that correct?

Yes!

mikemeinz avatar Mar 03 '21 20:03 mikemeinz

We also would highly appreciate the API option for hiding scrollbars as we are using the WebView2 control on a small touchscreen.

@jpalo How did you manage to hide scrollbars using CoreWebView2.ExecuteScriptAsync()? Unfortunately I'm not familiar with JavaScript ...

ghost avatar Mar 11 '21 07:03 ghost

@heering You need to use the NavigationCompleted event handler and execute a simple javascript. Details here: https://blog.jussipalo.com/2021/02/webview2-how-to-hide-scrollbars.html

jpalo avatar Mar 11 '21 08:03 jpalo

@jpalo Thank you very much. You saved my day 💯

ghost avatar Mar 11 '21 15:03 ghost

@jpalo I now recogized that, with this workaround, scrolling is disabled at all. I just want to hide the scrollbars because our users want to scroll by swiping the touchscreen, so I tried a different script:

ExecuteScriptAsync("document.querySelector('body').setAttribute('style', '-ms-overflow-style: none; ');");

This style property is MS specific: https://reference.codeproject.com/Book/css/-ms-overflow-style

But without success. Do you have any idea? Is the syntax incorrect?

ghost avatar Mar 12 '21 14:03 ghost

@heering The "-ms" prefix will only work with Internet Explorer and the old Microsoft Edge. The new Microsoft Edge is based on Chromium, so according to this page you can try the -webkit-scrollbar CSS psuedo-element. https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar

champnic avatar Mar 16 '21 00:03 champnic

@champnic I tried this (any many other syntax alternatives) with no luck: wv.ExecuteScriptAsync("document.querySelector('body').style['-webkit-scrollbar'] = 'display: none'"); I'm not used to JavaScrpt, so maybe there is a syntax problem.

ghost avatar Mar 16 '21 08:03 ghost

I'm not familiar with the syntax here myself, but StackOverflow may be able to help: https://stackoverflow.com/questions/311052/setting-css-pseudo-class-rules-from-javascript

You can also right-click > Inspect on your page to get access to devtools and try out javascript in the Console tab. This should enable a faster test loop than recompiling with a different ExecuteScriptAsync script.

champnic avatar Mar 17 '21 00:03 champnic

The correct code to run is

document.querySelector('body').style.overflow='scroll';
var style=document.createElement('style');
style.type='text/css';
style.innerHTML='::-webkit-scrollbar{display:none}';
document.getElementsByTagName('body')[0].appendChild(style);

but for some reason injecting new STYLE nodes isn't working in ExecuteScriptAsync on my machine. It does work if you manually run that Javasript in Debugging Console by Inspecting the WebView2.

jpalo avatar Mar 17 '21 17:03 jpalo

Working here?? 2021-03-17_173440

ukandrewc avatar Mar 17 '21 17:03 ukandrewc

You mean it is working for you?

jpalo avatar Mar 17 '21 17:03 jpalo

Not sure it's doing it specifically for me ;-)

ukandrewc avatar Mar 17 '21 17:03 ukandrewc

This snippet is working for me, too! Thanks a lot :)

ghost avatar Mar 18 '21 12:03 ghost

Still works, would be nice to have this feature official supported.

radove avatar Oct 16 '21 04:10 radove

Auto hide scrollbar for all web pages:

webView2.CoreWebView2InitializationCompleted += (sender, args) =>
            {
                if (args.IsSuccess && webView.CoreWebView2 != null)
                {
                    webView.CoreWebView2.DOMContentLoaded += (s, a) =>
                        {
                            //隐藏滚动条:https://blog.jussipalo.com/2021/02/webview2-how-to-hide-scrollbars.html
                            webView.ExecuteScriptAsync("document.querySelector('body').style.overflow='scroll';var style=document.createElement('style');style.type='text/css';style.innerHTML='::-webkit-scrollbar{display:none}';document.getElementsByTagName('body')[0].appendChild(style)");
                        };
                }
            };

microspaze avatar Nov 04 '22 13:11 microspaze

I removed scroll bars by including this in my style sheet: body{overflow:hidden;text-overflow:clip;white-space: nowrap;}

I'm no CSS expert so if anyone knows a better method, please comment.

mikemeinz avatar Feb 15 '23 21:02 mikemeinz

Seems no solution will work to disable the scrolling when viewing a PDF inside the webview2 browser.

acrolink avatar Aug 05 '24 12:08 acrolink