简体   繁体   English

通过Chrome扩展程序隐藏/显示滚动条

[英]Hide/show scrollbar from chrome extension

I'm trying to hide\\show scrollbars on pages via my Chrome Extension. 我试图通过我的Chrome扩展程序隐藏/显示页面上的滚动条。

I hide it by inserting this CSS from background.js file: 我通过从background.js文件插入以下CSS来隐藏它:

::-webkit-scrollbar {
    display: none !important;
}

::-webkit-scrollbar-button {
    display: none !important;
}

and I try to show it again by inserting this CSS from background.js file: 我尝试通过从background.js文件插入以下CSS再次显示它:

::-webkit-scrollbar {
    display: block !important;
}

::-webkit-scrollbar-button {
    display: block !important;
}

Hiding works, but I'm unable to restore it afterwards. 隐藏有效,但之后无法恢复。 When I inspect the page through Chrome DevTools, it shows as if both of the inserted CSS are active at same time. 当我通过Chrome DevTools检查页面时,它似乎好像两个插入的CSS都同时处于活动状态。

Is there any other way to do this? 还有其他方法吗? Important thing to note is that this should work on any page, so I'm able to remove and restore scrollbars from any page CSS is inserted to. 需要注意的重要一点是,它应该在任何页面上都可以工作,因此我可以从插入CSS的任何页面上删除和恢复滚动条。

I'm open for any other way, JavaScript too. 我也以其他任何方式开放,JavaScript也是如此。

This example may give you a hint on how to do it: 这个例子可以给你一些提示:

https://jsfiddle.net/PVZB8/139/ https://jsfiddle.net/PVZB8/139/

It's using CSS and JavaScript to achieve the result, but it seems like you can achieve that by using only CSS (example given on jsfiddle ): 它使用CSS和JavaScript来实现结果,但是似乎您可以仅使用CSS来实现这一点( jsfiddle上给出的示例 ):

div.mousescroll {
    overflow: hidden;
}
div.mousescroll:hover {
    overflow-y: scroll;
}

I accomplished this through content-script. 我是通过内容脚本完成的。 This code removes scrollbars and still allows you to scroll with mousewheel or keyboard buttons: 此代码删除了滚动条,仍然允许您使用鼠标滚轮或键盘按钮进行滚动:

    var styleElement = document.createElement('style');
    styleElement.id = 'remove-scroll-style';
    styleElement.textContent =
        'html::-webkit-scrollbar{display:none !important}' +
        'body::-webkit-scrollbar{display:none !important}';
    document.getElementsByTagName('body')[0].appendChild(styleElement);

And this code restores scrollbars: 这段代码将恢复滚动条:

$('#remove-scroll-style').remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM