简体   繁体   English

根据根背景图片或颜色反转文本颜色

[英]Invert text color based on root background image or color

some parts of this question have already been covered here . 这个问题的某些部分已经在这里讨论了

I am trying to create a navigation bar that goes on top of all elements like this: 我正在尝试创建一个导航栏,该导航栏位于所有这样的元素之上:

<div class="navbar">
  ...links...
</div>
<div class="hero-with-image">
  <h1>Hello</h1>
</div>

And then invert navigation links if my hero is dark or has an image on the background. 然后,如果我的英雄是黑暗的或背景上有图像,则反转导航链接。 Squarespace has a good example on their home page when you scroll slides. 滚动幻灯片时, Squarespace在其主页上有一个很好的例子。

I am using Ruby On Rails for my back end and one of the solutions I found is to create a helper method for my navigation bar where I define my controller.controller_name && controller.action_name and then add an extra class .has-dark-background to my html tag. 我将Ruby On Rails用于后端,发现的解决方案之一是为导航栏创建一个辅助方法,在其中定义我的controller.controller_name && controller.action_name ,然后添加一个额外的类.has-dark-background到我的html标签。 But what if I have multiple pages with dark background and multiple pages with light background, is there any way to invert text color based on hero object using jQuery? 但是,如果我有深色背景的多页和浅色背景的多页,有什么方法可以使用jQuery基于英雄对象来反转文本颜色?

Here is a good solution for jquery that I found: 这是我发现的jquery的一个很好的解决方案:

var rgb = [255, 0, 0];

// randomly change to showcase updates
setInterval(setContrast, 1000);

function setContrast() {
  // randomly update
  rgb[0] = Math.round(Math.random() * 255);
  rgb[1] = Math.round(Math.random() * 255);
  rgb[2] = Math.round(Math.random() * 255);

  // http://www.w3.org/TR/AERT#color-contrast
  var o = Math.round(((parseInt(rgb[0]) * 299) +
                     (parseInt(rgb[1]) * 587) +
                     (parseInt(rgb[2]) * 114)) / 1000);
  var fore = (o > 125) ? 'black' : 'white';
  var back = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
  $('#bg').css('color', fore); 
  $('#bg').css('background-color', back);
}

The code above works fine but it can only inver text color based on the current object background. 上面的代码可以正常工作,但是只能根据当前对象背景显示文本颜色。

In other words I do not want to create multiple navigation bars, I just want to invert colours based on the content (hero) background color. 换句话说,我不想创建多个导航栏,我只想根据内容(英雄)背景色来反转颜色。

Is there any other solution based on Jquery or Ruby on Rails? 还有其他基于Jquery或Ruby on Rails的解决方案吗?

Excuse me for my broken English. 打扰一下我的英语不好意思。 Thank you for your help and time. 感谢您的帮助和时间。

Is this close to what you want? 这接近您想要的吗?

 div { width: 200px; height: 200px; } .text { mix-blend-mode: difference; color: white; } .wrapper { background: url(http://lorempixel.com/400/400/); } 
 <div class="wrapper"> <div class="text">This is my text, hellow World! This is my text, hellow World! This is my text, hellow World! This is my text, hellow World!</div> </div> 

Here is the answer that I came up with. 这是我想出的答案。 I created a helper method for my navigation bar: 我为导航栏创建了一个辅助方法:

# helpers/application_helper.rb
def color_attr
  if %w(pages).include?(params[:controller])
    return 'has-dark-background'
  end
end

Where %w(pages) is a controller name. 其中%w(pages)是控制器名称。 Then, I just used my helper inside my header tag: 然后,我只是在header标签中使用了我的助手:

# haml syntax
%header(class="#{color_attr} other-classes" role="banner")

Class has-dark-background manually changes text color to white with additional styles whereas default colors dark. has-dark-background手动将文本颜色更改为带有其他样式的白色,而默认颜色为深色。 Unfortunately, this method will not change text color dynamically as I wanted. 不幸的是,这种方法不会像我所希望的那样动态改变文本颜色。

Update 更新

Not sure if this is the best way of doing it but here is another way: 不知道这是否是最好的方法,但是这是另一种方法:

# about.html.erb
- content_for :navbar_class, 'new_class_1 new_class_2 ...'

# _navbar.html.erb
...
  %header(id="navbarPrimary" class="navbar #{content_for :navbar_class}" role="navigation")
... 

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

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