简体   繁体   English

从浅到深动态地改变颜色

[英]Dynamically change color form light to dark

I have a function that currently changes a series of div from dark to lighter, but I want to reverse it so it goes from light to dark. 我有一个功能,当前可以将一系列div从黑暗更改为更亮,但我想将其反转,以便将其从明亮变为黑暗。

It would be great if someone could help me. 如果有人可以帮助我,那将是很棒的。 Thanks 谢谢

function color(r,g,b){
 return 'rgb('+r+','+g+','+b+')';
}

for (var i=-10; i < $('#lazyload:eq(0) .scroll-section').length/2 ; i++) {
     var r = 198;
     var g = 198;
     var b = 198;
    $('#lazyload:eq(0) .scroll-section:eq('+(i+10)+')').css('background' , color(r+(i*10),g+(i*10),b+(i*10)) );
};

https://jsfiddle.net/rn3yfp9o/2/ https://jsfiddle.net/rn3yfp9o/2/

Juste change + with - : 正义改变+-

color(r-(i*10),g-(i*10),b-(i*10)) 
-------^--------^--------^

https://jsfiddle.net/rn3yfp9o/4/ https://jsfiddle.net/rn3yfp9o/4/

(i + 10) => loop iterates from start to end. (i + 10) =>循环从头到尾进行迭代。 to reverse the order, use .scroll-section length as len - (i + 10) . 要反转顺序,请使用.scroll-section length作为.scroll-section len - (i + 10)

$('#lazyload:eq(0) .scroll-section:eq('+(len - i - 10)+')').css('background' , color(r+(i*10),g+(i*10),b+(i*10)) );

check the fiddle 检查小提琴

Try using it as: 尝试将其用作:

function color(r,g,b){
  return 'rgb('+r+','+g+','+b+')';
 }

for (var i=$('#lazyload:eq(0) .scroll-section').length/2; i >=-10  ; i--) {
       var r = 198;
       var g = 198;
       var b = 198;
  $('#lazyload:eq(0) .scroll-section:eq('+(i-10)+')').css('background' , color(r-(i*10),g-(i*10),b-(i*10)) );
 };

 function color(r,g,b){ return 'rgb('+r+','+g+','+b+')'; } for (var i=0; i < $('#lazyload:eq(0) .scroll-section').length ; i++) { var r = 255; var g = 255; var b = 255; $('#lazyload:eq(0) .scroll-section:eq('+i+')').css('background' , color(r-(i*10),g-(i*10),b-(i*10)) ); }; 
 #lazyload{ display: block; height: 100px; width: 100%; } .scroll-section{ width: 5%; height: 100%; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="lazyload"> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> <div class="scroll-section"></div> </div> 

暂无
暂无

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

相关问题 动态地将列表中项目的背景颜色从深色更改为浅色 - dynamically change the background color of items in a list from dark to light 更改 openlayers map 颜色(深色和浅色样式) - change openlayers map color (dark and light style) 如何在切换亮/暗模式时更改 iframe 的背景颜色? - How to change background color of an iframe while toggling light/dark mode? 如何使用 Switch 和 withStyles() 动态更改主题的调色板类型(暗/亮) - How to dynamically change palette type (dark/light) of theme using Switch and withStyles() 根据暗或亮模式更改styles - Change styles based on dark or light mode React:我怎样才能为明暗模式动态更改整个背景? Redux 持有我的黑暗模式 state - React: How can I dynamically change the entire background dynamically for light and dark mode? Redux holds my darkMode state 你可以使用 Chakra-UI 的 colorMode 将背景颜色更改为任何颜色,还是仅适用于明暗模式? - Can you use Chakra-UI's colorMode to change the background color to any color or is it only for light and dark mode? Dash 将主题从浅色变为深色 - Dash change theme from light to dark JavaScript函数返回给定颜色的“n”色调(从暗到亮) - JavaScript function to return “n” shades of a given color from (dark to light) TailwindCSS - 在“浅色”、“深色”或“系统设置”之间切换颜色主题 - TailwindCSS - Switch color theme between “Light”, “Dark” OR “System setting”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM