0

I want to randomly (of a selected list) change the color of the background of my header which I've done and is working great, using this JS:

var bgcolorlist=new Array("#ef5c20", "#a7dbca", "#c5e53f", "#ffad14")

$(".cbp-hsinner").css("background-color",bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]);

but I want the same random color to apply to the 'border-top-color' of this a:after tag here:

.cbp-hsmenu > li.cbp-hsitem-open > a:after {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: transparent;
border-top-color: #ef5c20;
border-width: 10px;
left: 50%;
margin-left: -10px;
}

HTML:

 <nav class="cbp-hsmenu-wrapper" id="cbp-hsmenu-wrapper">




                <div class="cbp-hsinner">

                 <div class="site-header__logo-alt"><a href="http://helloarchie.blue/"><img width="145" alt="Hello Archie" src="<?php echo theme_url('/img/HA-LOGO3.png'); ?>" /></a></div>

                    <ul class="cbp-hsmenu">

                    <li>
                            <a href="http://helloarchie.blue">Home</a>
                        </li>



                        <li>
                            <a href="#">Categories</a>
                            <ul class="cbp-hssubmenu">
                                <li><a href="http://helloarchie.blue/category/personal/"><span>Personal</span></a></li>
                            <li><a href="http://helloarchie.blue/category/monthly-updates/"><span>Monthly Updates</span></a></li>
                                <li><a href="http://helloarchie.blue/category/informative/"><span>Informative</span></a></li>
                                <li><a href="http://helloarchie.blue/category/reviews/"><span>Reviews</span></a></li>
                            <li><a href="http://helloarchie.blue/category/guides/"><span>Guides</span></a></li>
                            </ul>
                        </li>

                       <li>
                            <a href="http://helloarchie.blue/about">About</a>
                        </li>

                        <li>
                            <a href="#">Elsewhere</a>
                            <ul class="cbp-hssubmenu">
                                <li><a href="http://kaye.at/"><span>Portfolio</span></a></li>
                                <li><a href="http://kaye.at/baby/"><span>The Baby Project</span></a></li>

                            </ul>
                        </li>

                    </ul>
                </div>
            </nav>

Is this possible?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Kaye Huett
  • 61
  • 9

1 Answers1

0

Create a variable of the color and then apply it to both css rules.

var bgcolorlist=new Array("#ef5c20", "#a7dbca", "#c5e53f", "#ffad14");
var rgb = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
$(".cbp-hsinner").css("background-color",rgb);

This way you're only pulling the random color once and applying it to both places you want it. Other than that there is no way to set the border-top-color of one element to the background-color of another element directly in CSS. Since you can't access the :after directly in JS you will need to use one of the tecniques from this link to add the css the way you want.

Community
  • 1
  • 1
CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50