javascript - 根据背景颜色更改多个元素颜色

标签 javascript css

我有一个包含多个元素的固定菜单。我正在尝试找到一种方法让所有这些元素根据背景颜色改变颜色。

元素是一个

#page::before,
.logo-scroll

这两个元素都有白色边框(无填充)

主导航.main-navigation的链接及其边框为白色

白色的标志。我也有黑色版本。

我的网站由 3 种颜色组成,黑色、白色和黄色。

当背景部分为黄色或白色时,我希望元素切换为黑色。

该网站正在 build 中,但您可以在此处查看: https://www.sheree-new.shereewalker.com

我试过这个标志

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

但无法让它工作。我为元素尝试了 mix-blend 模式,但它使黄色的线条变成蓝色。我尝试使用混合混合模式,然后使用去饱和或灰度滤镜,但没有成功。

在一个问题中解决这个问题可能太多了,但我想也许 Wordpress 中有一个插件可以处理这个问题?

基本上我需要的是所有元素 https://codepen.io/whatthephuc/pen/QQagBj

包含左右导航元素的标题:

<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 -->

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;
}

所有部分都有黄色或白色的类别 - 默认背景为黑色。

任何关于合适插件的帮助或建议都会很棒。

**编辑 - 如果它适用于背景颜色,这样的东西会很完美

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

我也刚刚尝试过这种方法,但也会随机生成背景色

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' );

        }

    });

}

最佳答案

这是使用 javascript 控制文本颜色的一种非常基本的方法。

您可以根据滚动高度精确控制颜色变化的位置。

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>

关于javascript - 根据背景颜色更改多个元素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218526/

相关文章:

html - 为什么在移动设备上点击时会应用悬停样式?

jquery - Bootstrap 导航栏折叠不起作用

jquery - 使用 Walker 类自定义菜单结构

html - 如何在保持元素垂直对齐的同时将元素居中放置在容器中?

css - 背景大小和 100% 高度 block 的问题

javascript - Angular ng-model绑定(bind)到元素名称

javascript - 如何在 JavaScript 中检查输入是否包含字符串

javascript - 如何修复由于 React SSR 初始化数据库和云函数 firebase 初始化数据库而多次初始化的 firebase 数据库?

javascript - 在 react 中具有常量接口(interface)的外部组件

javascript - MapReduce 还是普通查询? (每个 map 发出几个)