ChatGPT解决这个技术问题 Extra ChatGPT

CSS scrollbar style cross browser [duplicate]

This question already has answers here: CSS customized scroll bar in div (20 answers) Closed 7 years ago. The community reviewed whether to reopen this question 9 months ago and left it closed: Original close reason(s) were not resolved

How can I define a CSS scrollbar style cross browser? I tested this code, it only works in IE and opera, but failed in Chrome, Safari and Firefox.

<style type="text/css">
<!--    
body {
    scrollbar-face-color: #000000;
    scrollbar-shadow-color: #2D2C4D;
    scrollbar-highlight-color:#7D7E94;
    scrollbar-3dlight-color: #7D7E94;
    scrollbar-darkshadow-color: #2D2C4D;
    scrollbar-track-color: #7D7E94;
    scrollbar-arrow-color: #C1C1D1;
}
-->
</style>
For WebKit browsers (Chrome/Safari), you can use this: stackoverflow.com/questions/7713599/…
@graphicdivine, that would only be the case if they're changing the scrollbar for the overall body. There are plenty of other scrollbars that could be modified (within the webpage) where it has nothing to do with the browser chrome.
This link shows you have to do it on Webkit browsers: css-tricks.com/custom-scrollbars-in-webkit

C
Community

Webkit's support for scrollbars is quite sophisticated. This CSS gives a very minimal scrollbar, with a light grey track and a darker thumb:

::-webkit-scrollbar
{
  width: 12px;  /* for vertical scrollbars */
  height: 12px; /* for horizontal scrollbars */
}

::-webkit-scrollbar-track
{
  background: rgba(0, 0, 0, 0.1);
}

::-webkit-scrollbar-thumb
{
  background: rgba(0, 0, 0, 0.5);
}

This answer is a fantastic source of additional information.


this however does not work in IE
this will work on chrome. what about firefox and IE?
@SHEKHARSHETE, yes this approach is only for WebKit. You should reference other answers if you need to support other browsers, perhaps in tandem with these vendor-prefixed CSS selectors.
don't work on FF
T
Till Helge

Scrollbar CSS styles are an oddity invented by Microsoft developers. They are not part of the W3C standard for CSS and therefore most browsers just ignore them.


Now also supported in Webkit though. And I think Opera supports them as well. So in fact, as of 2012, it seems 70% of the browser market share does support it (it being the styling of scrollbars, they do still use different syntax per browser to get the styling done sadly).
And now, 2013, Opera is moving to Webkit as well. Awesome!
See stackoverflow.com/a/14150577/276648 for a more complete answer with alternatives.
@jmendeth Well, I did not take the time to test it all myself, but according to this page it should work for IE, Chrome, Firefox. And according to this forum thread the IE style rules also work(ed?) in Opera, but only on the main page scrollbars. Not on those for textarea or div etc...
Everyone would like it. The single smart idea coming from the IE era ?
k
ktutnik

jScrollPane is a good solution to cross browser scrollbars and degrades nicely.


Does that only work on vertical scrollbars?
@paradroidΨ it also works horizontally scrolling content.
It does not degrade nicely. There are numerous situations where it doesn't create scrollbars (I have on in Firefox21).
G
Geza Kerecsenyi

nanoScrollerJS is simply to use. I always use them...

Browser compatibility:

IE7+

Firefox 3+

Chrome

Safari 4+

Opera 11.60+

Mobile browsers support:

iOS 5+ (iPhone, iPad and iPod Touch)

iOS 4 (with a polyfill)

Android Firefox

Android 2.2/2.3 native browser (with a polyfill)

Android Opera 11.6 (with a polyfill)

Code example from the Documentation,

Markup - The following type of markup structure is needed to make the plugin work.

<div id="about" class="nano">
    <div class="nano-content"> ... content here ...  </div>
</div>

D
DMTintner

As of IE6 I believe you cannot customize the scroll bar using those properties. The Chris Coyier article linked to above goes into nice detail about the options for webkit proprietary css for customizing the scroll bar.

If you really want a cross browser solution that you can fully customize you're going to have to use some JS. Here is a link to a nice plugin for it called FaceScroll: http://www.dynamicdrive.com/dynamicindex11/facescroll/index.htm


Were there really people in 2013 still coding to IE6?
@user2867288 - Yes...yes there were. I actually did freelance work for a small web agency that would farm some of their work to me, and that demanded I make all things IE6 capable. I finally dropped the entire agency as a client around that time because it was getting so ridiculous. Lost a decent amount of income...but kept my sanity. I would not doubt that they still require it to this day in 2016.
This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether.
Y
Yousef Altaf

try this it's very easy to use and tested on IE and Safari and FF and worked fine and beside no need for many div around it just add id and it will work fine, after you link you Js and Css files. FaceScroll Custom scrollbar

hope it help's

Edit Step 1: Add the below script to the section of your page:

<link href="general.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.ui.touch-punch.min.js"></script>

<script type="text/javascript" src="facescroll.js"></script>
<script type="text/javascript">
    jQuery(function(){ // on page DOM load
        $('#demo1').alternateScroll();
        $('#demo2').alternateScroll({ 'vertical-bar-class': 'styled-v-bar', 'hide-bars': false });  
    })
</script>

Step 2: Then in the BODY of your page, add the below sample HTML block to your page.

<p><b>Scrollbar (default style) shows onMouseover</b></p>

<div id="demo1" style="width:300px; height:250px; padding:8px; background:lightyellow; border:1px solid gray; resize:both; overflow:scroll">

From Wikipedia- Gunpowder, also known since in the late 19th century as black powder, was the first chemical explosive and the only one known until the mid 1800s.[2] It is a mixture of sulfur, charcoal, and potassium nitrate (saltpeter) - with the sulfur and charcoal acting as fuels, while the saltpeter works as an oxidizer.[3] Because of its 
</div>

<br />

<p><b>Scrollbar (alternate style), always shown</b></p>

<div id="demo2" style="width:400px; height:130px; padding:10px; padding-right:8px; background:lightyellow; border:1px solid gray; overflow:scroll; resize:both;">

From Wikipedia- Gunpowder, also known since in the late 19th century as black powder, was the first chemical explosive and the only one known until the mid 1800s.[2] It is a mixture of sulfur, charcoal, and potassium nitrate (saltpeter) - with the sulfur and charcoal acting as fuels, while the saltpeter works as an oxidizer.[3] Because of its 
</div>

This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether.