如何从JavaScript设置文本域元素的WebKit滚动条样式?

huangapple go评论53阅读模式
英文:

How can I set the WebKit scroll bar styles of a textarea element from JavaScript?

问题

I have a textarea DOM element. I am setting certain styles on it, for example:

textArea.style.border = "2px";

That's all fine. But how would I set a WebKit style, say the thumb colour?

I have tried the following:

textArea.style.webkitScrollbarThumbColor = "#ff0000";

and

textarea.style.scrollbarThumbColor = '#ff0000';

and

textarea.style.scrollbarColor = '#ff0000 #ff0000';

None of these work. I am using Chrome browser for my testing.

Thanks for any help.

英文:

I have a textarea DOM element. I am setting certain styles on it, for example:

textArea.style.border = "2px";

That's all fine. But how would I set a WebKit style, say the thumb colour?

I have tried the following:

textArea.style.webkitScrollbarThumbColor = "#ff0000";

and

textarea.style.scrollbarThumbColor = '#ff0000';

and

textarea.style.scrollbarColor = '#ff0000 #ff0000';

None of these work. I am using Chrome browser for my testing.

Thanks for any help.

答案1

得分: 0

自定义滚动条涉及设置与滚动条关联的伪元素的属性。您可以参考Mozilla开发者网络(MDN)文档,了解如何使用CSS ::-webkit-scrollbar 伪元素来样式化滚动条的更多信息:https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar

在JavaScript中,您无法直接选择伪元素。但是,您可以通过使用CSS变量设置滚动条宽度,然后使用JavaScript修改CSS变量来实现类似的效果。以下示例演示了如何做到这一点:

document.querySelector('textarea').style.setProperty('--scrollbarWidth', '12px');

以下是完整的示例代码:

<!-- 开始代码片段:js 隐藏:false 控制台:true 编译器:false -->

<!-- 语言:lang-js -->
document.querySelector('textarea').style.setProperty('--scrollbarWidth', '12px');

<!-- 语言:lang-css -->
textarea {
  overflow: auto;
  height: 150px;
  --scrollbarBG: white;
  --scrollbarColor: lightblue;
  --scrollbarWidth: 20px;
}

textarea::-webkit-scrollbar {
  width: var(--scrollbarWidth);
}

textarea::-webkit-scrollbar-track {
  background-color: var(--scrollbarBG)
}

textarea::-webkit-scrollbar-thumb {
  background-color: var(--scrollbarColor);
  border: 3px solid var(--scrollbarBG);
}

<!-- 语言:lang-html -->
<textarea>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</textarea>

<!-- 结束代码片段 -->
英文:

Customizing the scrollbar involves setting properties on the pseudo-element associated with the scrollbar. You can refer to the Mozilla Developer Network (MDN) documentation for more information on how to style the scrollbar using the CSS ::-webkit-scrollbar pseudo-element: https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar

In JavaScript, you cannot directly select a pseudo-element. However, you can achieve a similar effect by setting the scrollbar width using CSS variables and then modifying the CSS variables using JavaScript. The following example demonstrates how to do this:

document.querySelector(&#39;textarea&#39;).style.setProperty(&#39;--scrollbarWidth&#39;, &#39;12px&#39;);

Here's the full sample code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;textarea&#39;).style.setProperty(&#39;--scrollbarWidth&#39;, &#39;12px&#39;);

<!-- language: lang-css -->

textarea {
  overflow: auto;
  height: 150px;
  --scrollbarBG: white;
  --scrollbarColor: lightblue;
  --scrollbarWidth: 20px;
}

textarea::-webkit-scrollbar {
  width: var(--scrollbarWidth);
}

textarea::-webkit-scrollbar-track {
  background-color: var(--scrollbarBG)
}

textarea::-webkit-scrollbar-thumb {
  background-color: var(--scrollbarColor);
  border: 3px solid var(--scrollbarBG);
}

<!-- language: lang-html -->

&lt;textarea&gt;
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
&lt;/textarea&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月6日 19:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定