简体   繁体   中英

Change multiple elements colour based on background colour

I have a fixed menu which consists of multiple elements. I am trying to find a way to have all these elements change colour depending on the background colour.

The elements are a

#page::before,
.logo-scroll

both of these elements have a white border (no fill)

The links of the main navigation .main-navigation and their borders are white

The logo which is white. I also have a black version.

My site is made up of 3 section colours, black, white and yellow.

I would like the items to switch to black when the background sections are either yellow or white.

The website is very much a work in progress but you can see it here: https://www.sheree-new.shereewalker.com

I have tried this for the logo

https://eduardoboucas.com/blog/2017/09/25/svg-clip-path-logo-colour.html

but could not get it to work. I tried mix-blend mode for the elements but it makes the lines blue when on the yellow. I tried to do mix-blend-mode and THEN use the desaturate or greyscale filter but with no luck.

This is perhaps too much to tackle in one question but I thought perhaps there was a plugin that handled this in Wordpress?

Essentially what I need is this for all elements https://codepen.io/whatthephuc/pen/QQagBj

The header which contains the left and right nav elements:

<div class="logo-scroll">
        <div class="scroll-text">
            <a href="/home"><img width="53px" height="260px" src="/wp-content/uploads/2019/07/sheree-walker-web-design-edinburgh-vertical-01.svg"/></a>
        </div>
    </div>  

    <header id="masthead" class="site-header">
        <nav id="site-navigation" class="main-navigation">
            <button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"><?php esc_html_e( 'Primary Menu', 'sheree_walker' ); ?></button>
            <?php
            wp_nav_menu( array(
                'theme_location' => 'menu-1',
                'menu_id'        => 'primary-menu',
            ) );
            ?>
        </nav><!-- #site-navigation -->
    </header><!-- #masthead -->

The CSS

header#masthead {
    height: calc(100vh - 60px);
    width: 75px;
    position: fixed;
    float: right;
    right: 30px;
    top:30px;
}

#site-navigation {
    transform: rotate(90deg);
    transform-origin: top left;
    position: relative;
    right: -75px;
    width: calc(100vh - 60px);
}

.main-navigation li {
    float: left;
    position: relative;
    text-align: center;
    width: 33.33%;
    padding: 23px 20px 21px 20px;
    font-size: 23px;
    font-family: 'NeurialGrotesk';
}

.main-navigation li {
    border-bottom: 2px solid white;
}

.main-navigation li:nth-child(n+1) {
    border-right: 2px solid white;
}

.main-navigation a {
    color: white;
    letter-spacing: .5px;
}

#page::before {
    content: "";
    position: fixed;
    top: 30px;
    bottom: 30px;
    left: 30px;
    right: 30px;
    border: 2px solid white;
    pointer-events: none;
}

.logo-scroll {
    position: fixed;
    left: 30px;
    top: 30px;
    bottom: 30px;
    border: 2px solid white;
    width: 75px;
}

.scroll-text {
    position: fixed;
}

All the sections have classes of either yellow or white - the default background is black.

Any help or advice on a suitable plugin would be great.

**Edit - something like this would be perfect if it applied to background colours

https://github.com/kennethcachia/background-check

I have also just tried this which sort of works but also generates a background colour at random

contrast();

function contrast() {

    var R, G, B, C, L;

    $( "main-navigation a" ).each(function() {

        R = (Math.floor(Math.random() * 256));
        G = (Math.floor(Math.random() * 256));
        B = (Math.floor(Math.random() * 256));

        $( this ).css( 'background-color', 'rgb(' + R + ',' + G + ',' + B + ')' );

        C = [ R/255, G/255, B/255 ];

        for ( var i = 0; i < C.length; ++i ) {

            if ( C[i] <= 0.03928 ) {

                C[i] = C[i] / 12.92

            } else {

                C[i] = Math.pow( ( C[i] + 0.055 ) / 1.055, 2.4);

            }

        }

        L = 0.2126 * C[0] + 0.7152 * C[1] + 0.0722 * C[2];

        if ( L > 0.179 ) {

            $( this ).css( 'color', 'black' );

        } else {

            $( this ).css( 'color', 'white' );

        }

    });

}

Here's a very basic way to control text color with javascript.

You can control exactly where you want the color changes based on the scroll height.

 var p = document.querySelector('p'); var d = document.querySelectorAll('div'); var colors = ['white', 'red', 'black']; var offset = 0.025; var scrollHeight = document.documentElement.scrollHeight-innerHeight; window.addEventListener('scroll', function () { var scroll = scrollY/scrollHeight; p.style.color = colors[0]; var h = 0; for (var i=1; i<d.length; i++) { h += d[i-1].offsetHeight; if (scroll > (h/scrollHeight)-offset) p.style.color = colors[i]; } }); 
 body { margin: 0; } div { } .black { background: black; height: 150vh; } .yellow { background: yellow; height: 100vh; } .white { background: white; height: 200vh; } p { color: white; position: fixed; } 
 <p>I'll change color on scroll</p> <div class="black"></div> <div class="yellow"></div> <div class="white"></div> 

This is a very basic implementation using JS + CSS, that adds coloured children to the navigation block on scroll on resize:

 addEventListener("scroll", fixNavigation); addEventListener("resize", fixNavigation); if (document.readyState === "complate") fixNavigation(); else addEventListener("load", fixNavigation); function fixNavigation() { [...document.querySelectorAll("#nav>.nav-background")].forEach(item => item.remove()); var nav = document.getElementById("nav"); var scrolled = document.documentElement.scrollTop; var currentHeight = 0; document.querySelectorAll(".box").forEach(function(box) { var navBackground = document.createElement("div"); navBackground.className = "nav-background"; nav.appendChild(navBackground); navBackground.setAttribute("style", `top: ${currentHeight - scrolled}px; background: ${box.getAttribute("other-color")};`); currentHeight += box.offsetHeight; }); } 
 body { margin: 0; } #nav { position: fixed; top: 0px; left: 0px; bottom: 0px; width: 40px; } .box { width: 100%; height: 100px; } .red { background: #ff8888; } .green { background: #a2ffa2; } .blue { background: #7171ff; } .nav-background { pointer-events: none; position: absolute; height: 100px; right: 0; left: 0; } 
 <div id="nav"> </div> <div class="box red" other-color="purple"></div> <div class="box green" other-color="black"></div> <div class="box blue" other-color="yellow"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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