【发布时间】:2013-04-10 08:04:01
【问题描述】:
我有一种强烈的感觉,这是不可能的 - 但这就是我在这里的原因。
我有一个网站,它的主页上有一个全屏幻灯片,一个带有透明背景和深色文本/内容的标题。问题是在较暗的图像上您看不到内容,因为它也很暗。
显然我可以给 div 一个背景,但这不是设计所要求的。
有什么方法可以让文本相对于背景着色,可能是“反转”颜色效果或类似的东西?
提前致谢
【问题讨论】:
标签: javascript jquery css colors
我有一种强烈的感觉,这是不可能的 - 但这就是我在这里的原因。
我有一个网站,它的主页上有一个全屏幻灯片,一个带有透明背景和深色文本/内容的标题。问题是在较暗的图像上您看不到内容,因为它也很暗。
显然我可以给 div 一个背景,但这不是设计所要求的。
有什么方法可以让文本相对于背景着色,可能是“反转”颜色效果或类似的东西?
提前致谢
【问题讨论】:
标签: javascript jquery css colors
将信息存储在文件名中(最简单) - 例如 image1_light.png、imagex_dark.jpg 并查看 _light 或 _dark。或者
要看的demo是http://www.script-tutorials.com/demos/158/index.html,这里需要确定文字的位置,看看文字下面的主色是什么
$(".navigation").position() 是 {上:30,左:381}$(".navigation").width()) 是 214$(".navigation").height()) 是 68
我认为代码应该是这样的
var ctx,canvas;
$(function(){ // on page load, this should likely be on image transition
// creating canvas object in memory.
// Since I do not append it anywhere it is not rendered
canvas = $("<canvas/>")[0]; // a jQuery object on its own does not work
ctx = canvas.getContext('2d');
var image = new Image(); // create an image object in memory
image.onload = function () {
// resize
canvas.width=this.width;
canvas.height=this.height;
// render the image on the canvas
ctx.drawImage(image, 0, 0);
var nav = $(".navigation"); // get the navigation container
// find a pixel about in the middle where the navigation would be
var pos = {left: nav.position().left+(nav.width()/2), top: nav.position().top+(nav.height()/2) }
var pixel = ctx.getImageData(pos.left,pos.top, 1, 1).data;
$canvas=null; // no longer need this canvas
var invertedPixelColor = "rgba("+(255-pixel[0])+", "+(255-pixel[1])+", "+(255-pixel[2])+", "+1+")"; // invert it, ignoring the alpha channel
nav.css("color",invertedPixelColor); // set the nav text to inverted color
// here you could save the colour and reuse it
// if the user navigates to the same image
}
image.src = $(body).css('background-image'); // load the image, triggering the calc
});
【讨论】:
你可以通过蛮力来做到这一点。确定每个图像的文本颜色,然后在图像根据新图像的名称发生变化时使用 Javascript 设置颜色。当然,这意味着如果您更改任何图像,您也必须更改 Javascript。
【讨论】: