Vad*_*dea 10 html javascript css jquery
是否可以使用CSS动态更改基于背景颜色的文本颜色,或者如果CSS无法使用JS可以动态更改文本颜色?我有一个文字下面的图像.我的文字颜色是白色,但图像上有白色.当文本超出白色形状时,我需要更深颜色的文本颜色.
HTML:
<div class="biography">
<img src="http://s16.postimg.org/c09zw94jp/author_photo.png" alt="author" class="bio-img">
<div class="biography-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit <br>
Vestibulum pellentesque auctor quam a sollicitudin.<br>
Pellentesque accumsan a sem eget dictum.
</p>
<p>
Morbi dictum lorem tortor, id consequat libero gravida ut.<br>
Nullam dictum sed massa et bibendum.
</p>
<p>Praesent sed dui mattis, dictum urna sit amet, imperdiet purus.</p>
<p> Suspendisse potenti.</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
body {
background-color: #7ccbe6;
}
.biography {
min-height: 410px;
}
.biography .biography-text {
position: relative;
margin-top: -420px;
padding: 82px 130px 0 380px;
z-index: 1;
}
.biography-text {
color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
这是一个Codepen,可以看到更好的图片.
也许你需要这个:
var rgb = [255, 0, 0];
setInterval(function() {
var c = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
var o = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000);
(o > 125) ? $('#bg').css('color', 'black'): $('#bg').css('color', 'white'); //http://www.w3.org/TR/AERT#color-contrast
$('#bg').css('background-color', c);
rgb[0] = Math.round(Math.random() * 255);
rgb[1] = Math.round(Math.random() * 255);
rgb[2] = Math.round(Math.random() * 255);
}, 1000);
Run Code Online (Sandbox Code Playgroud)
更新:
在JSFiddle上使用这个例子,只需更改background color
小智 5
我确实意识到您正在寻找 JS 解决方案。但是,如果有人想要一个 css 解决方案,这里是一个解决方案 http://codepen.io/hellouniverse/pen/JNwvyL
$bg-color: black; // This can change
$white : #fff;
$black : #000;
@function set-text-color($color) {
@if (lightness( $color ) > 50) {
@return $black; // Lighter color, return black
}
@else {
@return $white; // Darker color, return white
}
}
.text--color {
background: $bg-color;
color: set-text-color($bg-color);
}
// this is here just for demon purpose
.text {
padding: 10px;
text-align: center;
}
<p class="text text--color">I change in color</p>
Run Code Online (Sandbox Code Playgroud)
对于这种问题,我会使用纯 CSS 解决方案来概述文本......
.biography-text {
color: #fff;
font-weight: bold;
letter-spacing: 2px;
text-shadow: 1px 1px 0 #333,
-1px -1px 0 #333,
1px -1px 0 #333,
-1px 1px 0 #333,
2px 2px 5px rgba(0,0,0,0.65);
}
Run Code Online (Sandbox Code Playgroud)
根据您的需要更改属性letter-spacing的最终指令。text-shadow