简体   繁体   English

用不同的阴影更改背景颜色

[英]Change background-color with different shades of color

I am trying to make a webpage with a background that changes color with the shades of the rainbow. 我试图制作一个网页,其背景随着彩虹的阴影而改变颜色。 I don't want any smooth transitions, I want them to change instantly. 我不希望有任何平滑过渡,而是希望它们立即更改。 With shades, I mean it doesn't just go from red to orange to green etc, I mean it uses the shades of each color. 带有阴影,我的意思是它不仅从红色变成橙色再到绿色等等,我的意思是它使用每种颜色的阴影。

It's a bit difficult to explain, I got it from this website. 有点难以解释,我是从这个网站上得到的。 If you hit 'inspect element' on the log in page, and then visit console, you'll see a bunch of rainbow text. 如果您在登录页面上单击“检查元素”,然后访问控制台,则会看到一堆彩虹文字。 The code used to achieve this is: 用于实现此目的的代码是:

background:linear-gradient(to left, red,orange,yellow,green,blue,indigo,violet)

As you can see there, it's a bunch of different shades from each color in the code. 如您所见,代码中的每种颜色都有许多不同的阴影。 How would I use this, but as a background of my webpage? 我如何将其用作网页背景?

To instantly change the background color we can do it using jquery. 要立即更改背景颜色,我们可以使用jquery进行更改。 See this link. 请参阅此链接。

https://jsfiddle.net/9bcgw7x4/6/ https://jsfiddle.net/9bcgw7x4/6/

$(document).ready(function(){
  var a = 1;
  var add =2;

  function changeBG(){
    var r = Math.floor((255 * a) / 100);  
    var g = Math.floor((255 * (100 - a)) / 100);
    var b = Math.floor((255 * a) / 100);  
    if(a<=0){
      add = 2;
    }else if(a>99){
      add=-2;
    }  
    a=a+add;    
    $('body').css('background-color', 'rgb('+r+','+g+','+b+')');
  }    
  setInterval(changeBG, 1000);

});

background changing with all the shades of the rainbow: 背景随着彩虹的所有阴影而变化:

  setInterval(function () { document.body.style.background = "red"; setTimeout(function () { document.body.style.background = "orange" }, 1000); setTimeout(function () { document.body.style.background = "yellow" }, 2000); setTimeout(function () { document.body.style.background = "green" }, 3000); setTimeout(function () { document.body.style.background = "blue" }, 4000); setTimeout(function () { document.body.style.background = "indigo" }, 5000); setTimeout(function () { document.body.style.background = "violet" }, 6000); }, 1000); 
 body{ transition:background 1s linear; } 

background moving with the colors of the rainbow: 用彩虹的颜色移动的背景:

 body{ color:white } 
 <marquee scrollamount="20" style="position:fixed;top:0;left:0;bottom:0;right:0;width:100%;height:100vh;z-index:-9999999;margin:0;padding:0"> <div style="width:200%;height:100vh;margin-left:-100%;background:linear-gradient(to left, red,orange,yellow,green,blue,indigo,violet,red,orange,yellow,green,blue,indigo,violet);"></div> </marquee> body body body body body body body body body body body body body body body body body 

try using transition-timing-function like step-start and step-end. 尝试使用过渡计时功能,例如逐步开始和逐步结束。

transition-timing-function: step-start;
transition-timing-function: step-end;

See the fiddle for understanding: https://jsfiddle.net/9bcgw7x4/5/ 参见小提琴以了解: https : //jsfiddle.net/9bcgw7x4/5/

In the code at fiddle replace the background colours with your desired ones. 在小提琴的代码中,将背景色替换为所需的背景色。

Updated 更新

Below is an example of the code you're looking for. 下面是您要查找的代码的示例。 If you want it to apply to the whole page then you should add it to the body tag. 如果要将其应用于整个页面,则应将其添加到body标签。 I used http://www.colorzilla.com/gradient-editor/ to generate this code snippet. 我使用http://www.colorzilla.com/gradient-editor/生成了此代码段。

body { /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#6db3f2+0,f9e759+0,f9e759+0,f9e759+0,f2f257+16,e85353+16,e85353+57,4239a0+57,4239a0+57,4239a0+83,66ff5b+83,66ff5b+83 */
background: #6db3f2; /* Old browsers */
background: -moz-linear-gradient(left, #6db3f2 0%, #f9e759 0%, #f9e759 0%, #f9e759 0%, #f2f257 16%, #e85353 16%, #e85353 57%, #4239a0 57%, #4239a0 57%, #4239a0 83%, #66ff5b 83%, #66ff5b 83%); /* FF3.6-15 */
background: -webkit-linear-gradient(left, #6db3f2 0%,#f9e759 0%,#f9e759 0%,#f9e759 0%,#f2f257 16%,#e85353 16%,#e85353 57%,#4239a0 57%,#4239a0 57%,#4239a0 83%,#66ff5b 83%,#66ff5b 83%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to right, #6db3f2 0%,#f9e759 0%,#f9e759 0%,#f9e759 0%,#f2f257 16%,#e85353 16%,#e85353 57%,#4239a0 57%,#4239a0 57%,#4239a0 83%,#66ff5b 83%,#66ff5b 83%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6db3f2', endColorstr='#66ff5b',GradientType=1 ); /* IE6-9 */ }

Here is an example with the colors you mentioned. 这是您提到的颜色的示例。

linear-gradient(to left, red 15%, orange 15%, orange 30%, yellow 30%, yellow 45%, green 45%, green 60%, blue 60%, blue 75%, indigo 75%, indigo 90%, violet 90%, violet)

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

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