Webkit滚动条动态样式
我目前正在使用 Webkit 的 ::-webkit-scrollbar CSS 属性设置滚动条的样式,并希望在 mousemove 事件上更改这些属性。问题是我似乎找不到动态获取滚动条 CSS 的方法。
是否可以通过 javascript(可能使用 jQuery)动态设置 webkit 滚动条的样式?
I'm currently styling the scrollbar using Webkit's ::-webkit-scrollbar CSS properties and would like to change these properties on a mousemove event. The problem is that I can't seem to find a way to get to the scrollbar's CSS dynamically.
Is it possible to style the webkit scrollbar dynamically, through javascript (possibly using jQuery)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,你可以做到。
您需要将动态 css 样式规则包含到样式表中。
然后去改变它。
你可以通过这个插件来做到这一点
如果你不需要使用jQuery - 你可以通过纯 Javascript 来完成:
链接 1
链接 2。
但存在跨浏览器问题。
另请参阅从 JavaScript 设置 CSS 伪类规则
Yes, you can do it.
You need to include dynamically css style rule into stylesheet.
And then to change it.
You can do it by this plugin
If you don't need to use jQuery - you can do it by pure Javascript:
link 1
link 2.
But there is cross-browser problems.
Also see Setting CSS pseudo-class rules from JavaScript
如果您想在鼠标悬停在滚动条上时更改滚动条属性。您可以使用 CSS 来完成此操作,这里有一个示例 http://jsfiddle.net/olgis/7Lg2R/(抱歉颜色设置丑陋)。
如果您想在鼠标悬停在容器上方时更改滚动条颜色,请查看这篇文章样式特定状态下的 webkit 滚动条 。描述了使用或不使用 JavaScript 的几种方法。
备注:我不知道出于什么原因,这些示例(使用 CSS 和 JavaScript)都不能在我的 Firefox 11 for Mint 中工作,但它们都在 Chrome 18.0.1025.151 中完美工作。
If you want to change a scrollbar properties when mouse is over it. You can do it with CSS, here an example http://jsfiddle.net/olgis/7Lg2R/ (sorry for ugly colorset).
If you want to change scrollbar colour if the mouse is over a container then look at this post Style webkit scrollbar on certain state . There are described several ways of doing it, with and without JavaScript.
REMARK: I do not know for which reason none of those example (with CSS neither JavaScript) do NOT work in my Firefox 11 for Mint, but all of them works perfectly in Chrome 18.0.1025.151.
我创建了带有四个选项卡的页面,每个选项卡都有不同的颜色设置以及滚动条
提供类到 body 标签
/
html
然而,这只能通过为每个选项卡按钮
javascript 来实现(此处仅显示滚动条部分) ScreenShot1 GreenScrollBar 图片
ScreenShot2 蓝色滚动条图像
i created page with four tabs each different color set as well as scroll bar
however this only worked by giving class to body tag
/
html
javascript for each tab button(only scroll bar section shown here)
ScreenShot1 GreenScrollBar Image
ScreenShot2 BlueScrollBar Image
为此,您应该完全替换滚动条。
只需选择为您提供最简单 API 的一个即可。
For this you should replace the scrollbar altogether.
It's just a matter of picking whichever one gives you the easiest API.
您可以使用 CSS3 设置滚动条的样式,这些通常仅适用于内部滚动条,而不适用于实际的浏览器主滚动条。您还可以将 MOZ 属性添加到以下内容。
演示:http://geryit.com/lib/custom-css3-scrollbars
下载源:http://geryit.com/lib/custom- css3-scrollbars/custom-css3-scrollbars.zip
You can style scrollbars with CSS3, these generally only work for internal scrollbars and not the actual browser main scrollbar. You can also add the MOZ attribute to the following.
Demo: http://geryit.com/lib/custom-css3-scrollbars
Download Source: http://geryit.com/lib/custom-css3-scrollbars/custom-css3-scrollbars.zip
您可以使用
id="scrollbar_style"
制作标记,然后像这样动态地在其中添加 css:
只需记住使用 innerHTML 在元素上不会仅仅添加您的新代码,它还将删除该元素内的任何内容。
问题解决了。
you can make a
<style>
tag withid="scrollbar_style"
and then add css inside it dynamicly like this :just remember that using innerHTML on an element WILL NOT JUST ADD your new code, it WILL ALSO DELETE whatever was inside that element.
problem solved.
您可以使用自己的 css 在 JavaScript 中定义函数。
在 html 中使用:
更多信息: https://css-tricks.com/almanac/属性/s/滚动条/
you can define a function in JavaScript with your own css.
using in html:
More Info: https://css-tricks.com/almanac/properties/s/scrollbar/
对于这个问题有一个很好的解决方法,您可以为滚动条添加多个具有不同样式的 css 类,然后使用 Javascript 动态更改这些类。
示例:
在类
red
和blue
之间切换的按钮:这是一个工作小提琴:http://jsfiddle.net/promatik/wZwJz/18/
There is a nice workaround for this problem, you can add multiple css classes with diffident styles for the scrollbar, and then change the classes dynamically with Javascript.
Example:
A button that toggles between the classes
red
andblue
:Here is a working fiddle: http://jsfiddle.net/promatik/wZwJz/18/