Overlap two background-color with javascript.

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

Overlap two background-color with javascript

问题

你可以使用 JavaScript 的以下代码实现你想要的效果:

  1. var teamAElements = document.getElementsByClassName('teamA');
  2. var teamBElements = document.getElementsByClassName('teamB');
  3. for (var i = 0; i < teamAElements.length; i++) {
  4. teamAElements[i].style.backgroundColor = "rgba(0, 0, 255, 0.3)";
  5. }
  6. for (var i = 0; i < teamBElements.length; i++) {
  7. teamBElements[i].style.backgroundColor = "rgba(255, 0, 0, 0.3)";
  8. }

这样,具有 'teamA' 和 'teamB' 类的 div 元素将分别设置为对应的颜色。

英文:

Can i overlap two background-colors?

I'm coding div tags for two team, one is teamA the other is teamB.
Each div is for each member.

  1. document.getElementsByClassName(&#39;teamA&#39;).style.backgroundColor = &quot;rgba(0, 0, 255, 0.3)&quot;;
  2. document.getElementsByClassName(&#39;teamB&#39;).style.backgroundColor = &quot;rgba(255, 0, 0, 0.3)&quot;;

The result i want is, a div class(user) in which both 'teamA' and 'teamB' belong, is a mixture of two colors(mixture of teamA and teamB color).

How can i do with javascript?

答案1

得分: 2

你可以使用单独的类和linear-gradient为两个团队的用户。

英文:

> You can use separate classes and linear-gradient for both teams users

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

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

  1. .user{
  2. width:200px;
  3. height:100px;
  4. border:1px solid lightgrey;
  5. padding:10px;
  6. margin:10px;
  7. }
  8. .teamA{
  9. background-color:rgba(0, 0, 255, 0.3);
  10. }
  11. .teamB{
  12. background-color:rgba(255, 0, 0, 0.3);
  13. }
  14. .teamAB{
  15. background:linear-gradient(to left,rgba(255, 0, 0, 0.3),rgba(0, 0, 255, 0.3));
  16. }

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

  1. &lt;div class=&#39;user teamA&#39;&gt;
  2. I am in Team A
  3. &lt;/div&gt;
  4. &lt;div class=&#39;teamB user&#39;&gt;
  5. I am in Team B
  6. &lt;/div&gt;
  7. &lt;div class=&#39;user teamAB&#39;&gt;
  8. I am in Team A,B
  9. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

以下是您要翻译的内容:

"One approach is below, though this answer may need revising should you update the question. At this point, though, I feel it answers your question; explanatory comments are in the code to explain what's happening and there are references at the end to aid in further research.

Please note that one method I use in this answer is to use the color-mix() function which is – at the time of writing – a very new feature, you may wish to look at the compatibility table for that function (it's on the reference linked in the references, below) and it may not work, or it may appear to do nothing in your current browser (use the developer tools to inspect the element if so):

  1. /* defining shared variables here, in the "global" context: */
  2. :root {
  3. --spacing: 0.5rem;
  4. --teamA: rgb(0 0 255 / 0.3);
  5. --teamB: rgb(255 0 0 / 0.3);
  6. }
  7. /* resetting default margins and padding to 0 (zero), and
  8. explicitly forcing the browser to use the same sizing
  9. algorithm, "border-box," to ensure that padding and
  10. border-sizes are included within the declared element
  11. size: */
  12. *,
  13. ::before,
  14. ::after {
  15. box-sizing: border-box;
  16. margin: 0;
  17. padding: 0;
  18. }
  19. body {
  20. /* ensuring that the <body> element takes the full height
  21. of the viewport: */
  22. block-size: 100vh;
  23. /* applying padding to the element on the 'block' axis,
  24. the axis on which block content is placed, the vertical
  25. axis in English, and other European languages: */
  26. padding-block: var(--spacing);
  27. }
  28. main {
  29. /* setting the <main> element to take 100% of the available
  30. space on the block axis: */
  31. block-size: 100%;
  32. /* using CSS grid layout: */
  33. display: grid;
  34. /* setting a gap between adjacent grid-items, whether on the
  35. block, or inline, axes: */
  36. gap: var(--spacing);
  37. /* using the repeat() function to create 3 grid-columns, each
  38. taking 1 fraction of the available space: */
  39. grid-template-columns: repeat(3, 1fr);
  40. /* setting the size of the element on the inline axis, the axis
  41. on which inline content (such as text) is placed, so the
  42. horizontal axis (in English, and European languages) and is
  43. equivalent to declaring "width" in those locales; the clamp()
  44. function defines a 50% size, with a minimum size of
  45. 20rem, and an upper limit of 900px: */
  46. inline-size: clamp(20rem, 50%, 900px);
  47. /* setting an auto margin on the inline axis, in order to center
  48. the <main> element on that axis: */
  49. margin-inline: auto;
  50. padding: var(--spacing);
  51. }
  52. main > * {
  53. /* setting a 2px border on the grid-items (the children of
  54. the <main> element: */
  55. border: 2px solid hsl(300deg 90% 80% / 0.9);
  56. /* an inset box-shadow to create an "inner-border" to
  57. differentiate the element border from the background-color: */
  58. box-shadow: inset 0 0 0 0.2rem #fff;
  59. text-align: center;
  60. }
  61. main * {
  62. /* all descendants of the <main> element have a padding
  63. determined by the --spacing custom property: */
  64. padding: var(--spacing);
  65. }
  66. .explainer {
  67. /* flex layout: */
  68. display: flex;
  69. /* the children of the element will be laid out in
  70. columns: */
  71. flex-direction: column;
  72. /* again, this sets the gap between adjacent elements: */
  73. gap: var(--spacing);
  74. /* this element will span 3 grid-columns: */
  75. grid-column: span 3;
  76. /* and the content is centered on the flow-axis (the axis
  77. on which the flex-content is laid out): */
  78. justify-content: center;
  79. }
  80. .teamA {
  81. /* setting the --color custom property to the value
  82. set by the --teamA custom property: */
  83. --color: var(--teamA);
  84. }
  85. .teamB {
  86. /* as above, for --teamB: */
  87. --color: var(--teamB);
  88. }
  89. span {
  90. /* styling the background-color of all <span> elements to
  91. the value of the --color custom-property or - if that
  92. custom-property is invalid/undefined - to transparent: */
  93. background-color: var(--color, transparent);
  94. }
  95. .gradientMix {
  96. /* using a linear-gradient: */
  97. background-image: linear-gradient(
  98. /* the gradient is at 90 degrees: */
  99. 90deg,
  100. /* the first color is the value of --teamA: */
  101. var(--teamA),
  102. /* and transitions over the element's size
  103. to the value of --teamB: */
  104. var(--teamB)
  105. );
  106. }
  107. .gradientStoppedMix {
  108. /* using a linear-gradient, again: */
  109. background-image: linear-gradient(
  110. 90deg,
  111. /* again starting with the value of --teamA,
  112. but this color starts at 0 and runs until
  113. 50% of the available space: */
  114. var(--teamA) 0 50%,
  115. /* the color held in --teamB starts at 50%,
  116. and continues; using these color-stops
  117. means that we can place hard edges on the
  118. gradient, rather than smooth transitions: */
  119. var(--teamB) 50%
  120. );
  121. }
  122. .colorMix {
  123. /* here we use the (new, at the time of writing) color-mix()
  124. function to mix two colors together: */
  125. background-color: color-mix(
  126. /* we're using the srgb color space: */
  127. in srgb,
  128. /* unfortunately (at least in Firefox) we can't (yet) use
  129. custom properties in color-mix(), so instead I've had
  130. to hard-code --teamB's value, which is mixed at 50% */
  131. rgb(255 0 0 / 0.3) 50%,
  132. /* into the "base" color, again hard-coding --teamA's value: */
  133. rgb(0 0 255 / 0.3));
  134. }
  135. code {
  136. background-color: #eee;
  137. display: block;
  138. font-family: monospace;
  139. }
  1. <main>
  2. <div class="explainer">
  3. Using CSS linear-gradient: <code>linear-gradient(90deg, var(--teamA), var(--teamB))</code>
  4. </div>
  5. <span class="teamA">Team A</span>
  6. <span class="teamB">Team B</span>
  7. <span class="mixed gradientMix">Team A + Team B</span>
  8. <div class="explainer">
  9. Using CSS linear-gradient: <code>linear-gradient(90deg, var(--teamA) 0 50%, var(--teamB) 50%)</code>
  10. </
  11. <details>
  12. <summary>英文:</summary>
  13. One approach is below, though this answer may need revising should you update the question. At this point, though, I feel it answers your question; explanatory comments are in the code to explain what&#39;s happening and there are references at the end to aid in further research.
  14. Please note that one method I use in this answer is to use the `color-mix()` function which is &amp;ndash; at the time of writing &amp;ndash; a very new feature, you may wish to look at the compatibility table for that function (it&#39;s on the reference linked in the references, below) and it may not work, or it may appear to do nothing in your current browser (use the developer tools to inspect the element if so):
  15. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  16. &lt;!-- language: lang-css --&gt;
  17. /* defining shared variables here, in the &quot;global&quot; context: */
  18. :root {
  19. --spacing: 0.5rem;
  20. --teamA: rgb(0 0 255 / 0.3);
  21. --teamB: rgb(255 0 0 / 0.3);
  22. }
  23. /* resetting default margins and padding to 0 (zero), and
  24. explicitly forcing the browser to use the same sizing
  25. algorithm, &quot;border-box,&quot; to ensure that padding and
  26. border-sizes are included within the declared element
  27. size: */
  28. *,
  29. ::before,
  30. ::after {
  31. box-sizing: border-box;
  32. margin: 0;
  33. padding: 0;
  34. }
  35. body {
  36. /* ensuring that the &lt;body&gt; element takes the full height
  37. of the viewport: */
  38. block-size: 100vh;
  39. /* applying padding to the element on the &#39;block&#39; axis,
  40. the axis on which block content is placed, the vertical
  41. axis in English, and other European languages: */
  42. padding-block: var(--spacing);
  43. }
  44. main {
  45. /* setting the &lt;main&gt; element to take 100% of the available
  46. space on the block axis: */
  47. block-size: 100%;
  48. /* using CSS grid layout: */
  49. display: grid;
  50. /* setting a gap between adjacent grid-items, whether on the
  51. block, or inline, axes: */
  52. gap: var(--spacing);
  53. /* using the repeat() function to create 3 grid-columns, each
  54. taking 1 fraction of the available space: */
  55. grid-template-columns: repeat(3, 1fr);
  56. /* setting the size of the element on the inline axis, the axis
  57. on which inline content (such as text) is placed, so the
  58. horizontal axis (in English, and European languages) and is
  59. equivalent to declaring &quot;width&quot; in those locales; the clamp()
  60. function defines a 50% size, with a minimum size of
  61. 20rem, and an upper limit of 900px: */
  62. inline-size: clamp(20rem, 50%, 900px);
  63. /* setting an auto margin on the inline axis, in order to center
  64. the &lt;main&gt; element on that axis: */
  65. margin-inline: auto;
  66. padding: var(--spacing);
  67. }
  68. main &gt; * {
  69. /* setting a 2px border on the grid-items (the children of
  70. the &lt;main&gt; element: */
  71. border: 2px solid hsl(300deg 90% 80% / 0.9);
  72. /* an inset box-shadow to create an &quot;inner-border&quot; to
  73. differentiate the element border from the background-color: */
  74. box-shadow: inset 0 0 0 0.2rem #fff;
  75. text-align: center;
  76. }
  77. main * {
  78. /* all descendants of the &lt;main&gt; element have a padding
  79. determined by the --spacing custom property: */
  80. padding: var(--spacing);
  81. }
  82. .explainer {
  83. /* flex layout: */
  84. display: flex;
  85. /* the children of the element will be laid out in
  86. columns: */
  87. flex-direction: column;
  88. /* again, this sets the gap between adjacent elements: */
  89. gap: var(--spacing);
  90. /* this element will span 3 grid-columns: */
  91. grid-column: span 3;
  92. /* and the content is centered on the flow-axis (the axis
  93. on which the flex-content is laid out): */
  94. justify-content: center;
  95. }
  96. .teamA {
  97. /* setting the --color custom property to the value
  98. set by the --teamA custom property: */
  99. --color: var(--teamA);
  100. }
  101. .teamB {
  102. /* as above, for --teamB: */
  103. --color: var(--teamB);
  104. }
  105. span {
  106. /* styling the background-color of all &lt;span&gt; elements to
  107. the value of the --color custom-property or - if that
  108. custom-property is invalid/undefined - to transparent: */
  109. background-color: var(--color, transparent);
  110. }
  111. .gradientMix {
  112. /* using a linear-gradient: */
  113. background-image: linear-gradient(
  114. /* the gradient is at 90 degrees: */
  115. 90deg,
  116. /* the first color is the value of --teamA: */
  117. var(--teamA),
  118. /* and transitions over the element&#39;s size
  119. to the value of --teamB: */
  120. var(--teamB)
  121. );
  122. }
  123. .gradientStoppedMix {
  124. /* using a linear-gradient, again: */
  125. background-image: linear-gradient(
  126. 90deg,
  127. /* again starting with the value of --teamA,
  128. but this color starts at 0 and runs until
  129. 50% of the available space: */
  130. var(--teamA) 0 50%,
  131. /* the color held in --teamB starts at 50%,
  132. and continues; using these color-stops
  133. means that we can place hard edges on the
  134. gradient, rather than smooth transitions: */
  135. var(--teamB) 50%
  136. );
  137. }
  138. .colorMix {
  139. /* here we use the (new, at the time of writing) color-mix()
  140. function to mix two colors together: */
  141. background-color: color-mix(
  142. /* we&#39;re using the srgb color space: */
  143. in srgb,
  144. /* unfortunately (at least in Firefox) we can&#39;t (yet) use
  145. custom properties in color-mix(), so instead I&#39;ve had
  146. to hard-code --teamB&#39;s value, which is mixed at 50% */
  147. rgb(255 0 0 / 0.3) 50%,
  148. /* into the &quot;base&quot; color, again hard-coding --teamA&#39;s value: */
  149. rgb(0 0 255 / 0.3));
  150. }
  151. code {
  152. background-color: #eee;
  153. display: block;
  154. font-family: monospace;
  155. }
  156. &lt;!-- language: lang-html --&gt;
  157. &lt;main&gt;
  158. &lt;div class=&quot;explainer&quot;&gt;
  159. Using CSS linear-gradient: &lt;code&gt;linear-gradient(90deg, var(--teamA), var(--teamB))&lt;/code&gt;
  160. &lt;/div&gt;
  161. &lt;span class=&quot;teamA&quot;&gt;Team A&lt;/span&gt;
  162. &lt;span class=&quot;teamB&quot;&gt;Team B&lt;/span&gt;
  163. &lt;span class=&quot;mixed gradientMix&quot;&gt;Team A + Team B&lt;/span&gt;
  164. &lt;div class=&quot;explainer&quot;&gt;
  165. Using CSS linear-gradient: &lt;code&gt;linear-gradient(90deg, var(--teamA) 0 50%, var(--teamB) 50%)&lt;/code&gt;
  166. &lt;/div&gt;
  167. &lt;span class=&quot;teamA&quot;&gt;Team A&lt;/span&gt;
  168. &lt;span class=&quot;teamB&quot;&gt;Team B&lt;/span&gt;
  169. &lt;span class=&quot;mixed gradientStoppedMix&quot;&gt;Team A + Team B&lt;/span&gt;
  170. &lt;div class=&quot;explainer&quot;&gt;
  171. Using CSS color-mix: &lt;code&gt;color-mix(in srgb, rgb(0 0 255 / 0.3) 50%, rgb(255 0 0 / 0.3))&lt;/code&gt;
  172. &lt;/div&gt;
  173. &lt;span class=&quot;teamA&quot;&gt;Team A&lt;/span&gt;
  174. &lt;span class=&quot;teamB&quot;&gt;Team B&lt;/span&gt;
  175. &lt;span class=&quot;mixed colorMix&quot;&gt;Team A + Team B&lt;/span&gt;
  176. &lt;/main&gt;
  177. &lt;!-- end snippet --&gt;
  178. [JS Fiddle demo](https://jsfiddle.net/davidThomas/kpbLcehf/1/).
  179. References:
  180. * [`background-color`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-color).
  181. * [`block-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/block-size).
  182. * [`box-shadow`](https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow).
  183. * [`box-sizing`](https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing).
  184. * [`clamp()`](https://developer.mozilla.org/en-US/docs/Web/CSS/clamp).
  185. * [`color-mix()`](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/color-mix).
  186. * [CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*).
  187. * [CSS logical properties](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_logical_properties).
  188. * [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/display).
  189. * [`flex`](https://developer.mozilla.org/en-US/docs/Web/CSS/flex).
  190. * [`flex-direction`](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction).
  191. * [`font-family`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-family).
  192. * [`gap()`](https://developer.mozilla.org/en-US/docs/Web/CSS/gap).
  193. * [`grid-column`](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column).
  194. * [`grid-template-columns`](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns).
  195. * [`inline-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/inline-size).
  196. * [`justify-content`](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content).
  197. * [`linear-gradient()`](https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient).
  198. * [`margin`](https://developer.mozilla.org/en-US/docs/Web/CSS/margin).
  199. * [`margin-inline`](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-inline).
  200. * [`padding`](https://developer.mozilla.org/en-US/docs/Web/CSS/padding).
  201. * [`padding-block`](https://developer.mozilla.org/en-US/docs/Web/CSS/padding-block).
  202. * [`padding-inline`](https://developer.mozilla.org/en-US/docs/Web/CSS/padding-inline).
  203. * [`place-contents`](https://developer.mozilla.org/en-US/docs/Web/CSS/place-contents).
  204. * [`repeat()`](https://developer.mozilla.org/en-US/docs/Web/CSS/repeat).
  205. * [`text-align`](https://developer.mozilla.org/en-US/docs/Web/CSS/text-align).
  206. </details>

huangapple
  • 本文由 发表于 2023年5月7日 00:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189848.html
匿名

发表评论

匿名网友

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

确定