Change element color to black or white based on background color
AntunCrnja
I'm building a new website and I want to change the logo color (ie ) position: fixed
based on the background color or image .
I found an example on this site . You can see how the logo and certain elements change color based on the background.
body {
margin: 0;
}
.logo img {
position: fixed;
width: 100px;
}
.black {
background: black;
width: 100%;
height: 400px;
}
.yellow {
background: yellow;
width: 100%;
height: 400px;
}
.red {
background: red;
width: 100%;
height: 400px;
}
.imageBg {
background: url(https://images.pexels.com/photos/2208895/pexels-photo-2208895.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
width: 100%;
height: 400px;
background-size: cover;
}
<div class="logo">
<img src="http://www.clker.com/cliparts/n/y/a/M/G/h/fdl-logo.svg">
</div>
<!-- change logo color to: black -->
<section class="yellow"></section>
<!-- change logo color to: white -->
<section class="black"></section>
<!-- change logo color to: white -->
<section class="red"></section>
<!-- change logo color to: black -->
<section class="yellow"></section>
<!-- change logo color to: white -->
<section class="imageBg"></section>
Here is my fiddle : https://jsfiddle.net/o0gnayht/
Udetetis
By using, filter:invert()
you can change the logo color from black to white. If you use jquery in the onscroll
event , you can call filter
based on the position closest to the topsection
$(window).scroll(function ()
{
var windowTop = Math.max($('body').scrollTop(), $('html').scrollTop());
$('section').each(function (index)
{
if (windowTop > ($(this).position().top - 100))
{
if ($(this).hasClass('yellow'))
{
$('.logo img').css('filter', 'invert(0)');
}
else
{
$('.logo img').css('filter', 'invert(1)');
}
}
});
}).scroll();
body {
margin: 0;
}
.logo img {
position: fixed;
width: 100px;
}
.black {
background: black;
width: 100%;
height: 400px;
}
.yellow {
background: yellow;
width: 100%;
height: 400px;
}
.red {
background: red;
width: 100%;
height: 400px;
}
.imageBg {
background: url(https://images.pexels.com/photos/2208895/pexels-photo-2208895.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
width: 100%;
height: 400px;
background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="logo">
<img src="http://www.clker.com/cliparts/n/y/a/M/G/h/fdl-logo.svg">
</div>
<!-- change logo color to: black -->
<section class="yellow"></section>
<!-- change logo color to: white -->
<section class="black"></section>
<!-- change logo color to: white -->
<section class="red"></section>
<!-- change logo color to: black -->
<section class="yellow"></section>
<!-- change logo color to: white -->
<section class="imageBg"></section>