简体   繁体   English

通过 javascript 将 CSS 样式添加到特定的 class

[英]Add CSS style to a specific class via javascript

I got these lines of code and want to add them only to a specific div class.我得到了这些代码行,只想将它们添加到特定的 div class。 So if the background is dark it should be filter: invert(1);因此,如果背景较暗,则应使用filter: invert(1); and when the background is brighter filter: invert(0);当背景更亮时filter: invert(0); . . How can I do that?我怎样才能做到这一点? Hope you can help me...I'm still at the very beginning of understanding js.希望你能帮助我......我还处于了解js的最开始。

    function isDark( color ) {
    var match = /rgb\((\d+).*?(\d+).*?(\d+)\)/.exec(color);
    return ( match[1] & 255 )
         + ( match[2] & 255 )
         + ( match[3] & 255 )
           < 3 * 256 / 2;
}

$('div').each(function() {console.log($(this).css("background-color"))
    $(this).css("filter", isDark($(this).css("background-color")) ? 'invert(1)' : 'invert(0)');
});

You can check the condition separately in an if else statement as below.您可以在if else语句中单独检查条件,如下所示。

$('div').each(function() {
  var color = $(this).css("background-color");
  if (isDark(color)) {
    $(this).css("filter", 'invert(1)');
  } else {
    $(this).css("filter", 'invert(0)');
  }
});

OR或者

if you want a css class to be added, you can do it as below如果您想添加 css class ,您可以按以下方式进行

$('div').each(function() {
  var color = $(this).css("background-color");
  if (isDark(color)) {
    $(this).addClass("inverted");
  } else {
    $(this).addClass("not-inverted");
  }
});

in which the styles can be added for the new class in your CSS as below.其中 styles 可以为您的 CSS 中的新class添加如下。

.inverted {
  filter: invert(1);
}

.not-inverted {
  filter: invert(0);
}

See the snippet below.请参阅下面的片段。

 function isDark(color) { var match = /rgb\((\d+).*?(\d+).*?(\d+)\)/.exec(color); return (match[1] & 255) + (match[2] & 255) + (match[3] & 255) < 3 * 256 / 2; } /* $('div').each(function() {console.log($(this).css("background-color")) $(this).css("filter", isDark($(this).css("background-color"))? 'invert(1)': 'invert(0)'); }); */ $('div').each(function() { var color = $(this).css("background-color"); if (isDark(color)) { $(".logo").css("filter", 'invert(1)'); } else { $(".logo").css("filter", 'invert(0)'); } });
 body { height: 400vh; }.logo { position: fixed; top: 40px; right: 20px; background-image: url(https://image.freepik.com/free-vector/bicycle-shop-logo-design-vector_53876-40626.jpg); background-repeat: no-repeat; background-size: 100px; height: 100px; width: 100px; background-color: black; }.blabla { background-color: black; height: 50vh; }.blablabla { background-color: grey; height: 50vh; }.blablablabla { background-color: red; height: 50vh; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="logo"></div> <div class="blabla">test</div> <div class="blablabla">test</div> <div class="blablablabla">test</div>

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

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