Home > Enterprise >  how to give multiple classes a style with js
how to give multiple classes a style with js

Time:10-16

//  sticty menu
window.addEventListener("scroll", function() {
    var header = document.querySelector("header");
    header.classList.toggle("sticky", window.scrollY > 0);
});

// heder color
const headerScroll = document.querySelector(".header");
const allMenuItem = document.querySelectorAll(".onscroll");


window.addEventListener("scroll", () => {
    if (window.scrollY > 0) {
        headerScroll.classList.add('head-change');
        allMenuItem.forEach(v => {
            v.classList.add('menu-change');
        });
    } else {
        headerScroll.classList.remove('head-change');
        v.classList.remove('menu-change');

    };

});
 .head-change {
    background-color: white;
}

.menu-change {
    color: black:
}
/* ==============   ================== */

a {
    text-decoration: none;
}

ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

* {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    text-decoration: none;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

header.sticky .navigation {
    line-height: 60px;
    transition: 0.6s;
    transition-property: line-height;
}

.header {
    position: fixed;
    display: flex;
    justify-content: space-between;
    align-items: center;
    top: 0;
    left: 0;
    width: 100%;
    padding: 0 15px;
    z-index: 100;
    background-color: transparent;
    box-shadow: 0 5px 25px rgb(0 0 0 / 20%);
     transition: 0.6s;
}

.header:hover {
    background-color: white;
}

.head-change {
    background-color: white;
}

.menu-change {
    color: black;
}

header .navigation {
    position: relative;
    line-height: 75px;
}

header .navigation .menu {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
 }

 

.menu>li a {
    color: white;
    font-size: 15px;
}

.header:hover .menu>li a {
    color: var(--black);
}

.sub-menu>li a {
    color: var(--black);
    font-size: 1em;
}

.menu-item>a {
    color: white;
    font-size: 15px;
    line-height: 22px;
    font-weight: 500;
    padding: 25px 0;
    margin: 18px;
    transform: 0.5s;
}

.menu-item>a:hover {
    color: #469dff;
    transition: 0.3s;
}

.menu .header-icon {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
}
 

.menu-item:hover .sub-menu {
    pointer-events: all;
    transform: translateY(0px);
    opacity: 1;
}

.sub-item {
    position: relative;
    padding: 7px 0;
    cursor: pointer;
    box-shadow: inset 0px -30px 5px -30px rgb(0 0 0 / 20%);
}

.sub-item a {
    font-size: 1em;
    padding: 15px 30px;
}

.sub-item:hover {
    background: #f4f6F7FF;
    ;
}

.sub-item:last-child:hover {
    border-radius: 0 0 8px 8px;
}

 
 

.slider {
    height: 100vh;
    padding: 13vh 0 0;
    box-sizing: border-box;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background-color: #343134;
    background-image: url(../assets/images/mr.webp);
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

.slider .slider-head {
    height: 50vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    line-height: 0;
}

.slider-head .Be {
    font-size: 7em;
    font-weight: 900;
    color: var(--main-color);
}

.slider-head:nth-child(2) {
    font-size: 7em;
    font-weight: 900;
}

.slider-head .h-header {
    font-size: 18px;
    line-height: 0;
}



.slider-head .theme-version {
    display: inline-block;
    padding: .35em .65em;
    font-size: .75em;
    font-weight: 700;
    line-height: 1;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    border-radius: .25rem;
    margin-bottom: 1rem;
    background-color: var(--main-color);
}
 
<header id="head">
    <div >
        <a href="#">logo</a>
        <nav >
            <!-- menu-icon -->

            <ul >
                <li >
                    <a  href="#">item</a></li>
                <li >
                    <a  href="#">item</a>

                </li>
                <li ><a  href="#">item</a></li>
                <li >
                    <a  href="#">item</a>

                </li>
                <li ><a  href="#">item</a></li>
       
 
            </ul>
    </div>

    </nav>


</header>
<section >


    <div >
        <h1 >R2</h1>
        <h1 >Create <span>anything</span></h1>
        <h1 >you can imagine</h1>
        <div >Version 1.0 not compleat yet</div>

    </div>

    <div >

        <img src="assets/images/slider-demos-new.webp" alt="slider-demos">
    </div>
</section>
<section>
    <div>sectionn</div>
</section>

I am trying to make a sticky header, and then scroll the header style change. the header background changed but there was a problem with the ul link.

for now, all the ul a links have "onscroll" class, but there was no change.

a brief for the header HTML & CSS code below

const headerScroll = document.querySelector(".header");
const allMenuItem = document.querySelectorAll(".onscroll");


window.addEventListener("scroll", () => {
    if (window.scrollY > 0) {
        headerScroll.classList.add('head-change');
        allMenuItem.forEach(v => {
            v.classList.add('menu-change');
        });
    } else {
        headerScroll.classList.remove('head-change');
        v.classList.remove('menu-change');

    };

});

a brief for the header HTML & CSS code below.

CodePudding user response:

The first problem is: your v is not defined in else, so you should use for each in else too.

The second problem is: your class .menu-change for some reason doesn't overwrite the default style, so just add !important to it in CSS.

one of the items in your nav is still white, I'll leave that to you to fix.

//  sticty menu
window.addEventListener("scroll", function() {
    var header = document.querySelector("header");
    header.classList.toggle("sticky", window.scrollY > 0);
});

// heder color
const headerScroll = document.querySelector(".header");
const allMenuItem = document.querySelectorAll(".onscroll");


window.addEventListener("scroll", () => {
    if (window.scrollY > 0) {
        headerScroll.classList.add('head-change');
        allMenuItem.forEach(v => {
            v.classList.add('menu-change');
        });
    } else {
        headerScroll.classList.remove('head-change');
        allMenuItem.forEach(v => {
            v.classList.remove('menu-change');
        });

    };

});
.head-change {
  background-color: white;
}

.menu-change {
  color: black !important;
}

/* ==============   ================== */

a {
  text-decoration: none;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

* {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  text-decoration: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

header.sticky .navigation {
  line-height: 60px;
  transition: 0.6s;
  transition-property: line-height;
}

.header {
  position: fixed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  top: 0;
  left: 0;
  width: 100%;
  padding: 0 15px;
  z-index: 100;
  background-color: transparent;
  box-shadow: 0 5px 25px rgb(0 0 0 / 20%);
   transition: 0.6s;
}

.header:hover {
  background-color: white;
}

.head-change {
  background-color: white;
}

.menu-change {
  color: black;
}

header .navigation {
  position: relative;
  line-height: 75px;
}

header .navigation .menu {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}



.menu>li a {
  color: white;
  font-size: 15px;
}

.header:hover .menu>li a {
  color: var(--black);
}

.sub-menu>li a {
  color: var(--black);
  font-size: 1em;
}

.menu-item>a {
  color: white;
  font-size: 15px;
  line-height: 22px;
  font-weight: 500;
  padding: 25px 0;
  margin: 18px;
  transform: 0.5s;
}

.menu-item>a:hover {
  color: #469dff;
  transition: 0.3s;
}

.menu .header-icon {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
}


.menu-item:hover .sub-menu {
  pointer-events: all;
  transform: translateY(0px);
  opacity: 1;
}

.sub-item {
  position: relative;
  padding: 7px 0;
  cursor: pointer;
  box-shadow: inset 0px -30px 5px -30px rgb(0 0 0 / 20%);
}

.sub-item a {
  font-size: 1em;
  padding: 15px 30px;
}

.sub-item:hover {
  background: #f4f6F7FF;
  ;
}

.sub-item:last-child:hover {
  border-radius: 0 0 8px 8px;
}




.slider {
  height: 100vh;
  padding: 13vh 0 0;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: #343134;
  background-image: url(../assets/images/mr.webp);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

.slider .slider-head {
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  line-height: 0;
}

.slider-head .Be {
  font-size: 7em;
  font-weight: 900;
  color: var(--main-color);
}

.slider-head:nth-child(2) {
  font-size: 7em;
  font-weight: 900;
}

.slider-head .h-header {
  font-size: 18px;
  line-height: 0;
}

.slider-head .theme-version {
  display: inline-block;
  padding: .35em .65em;
  font-size: .75em;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: .25rem;
  margin-bottom: 1rem;
  background-color: var(--main-color);
}
  <header id="head">
    <div >
        <a href="#">logo</a>
        <nav >
            <!-- menu-icon -->

            <ul >
                <li >
                    <a  href="#">item</a></li>
                <li >
                    <a  href="#">item</a></li>
                <li >
                  <a  href="#">item</a></li>
                <li >
                  <a  href="#">item</a></li>
                <li >
                  <a  href="#">item</a></li>
            </ul>
    </div>

    </nav>


</header>
<section >


    <div >
        <h1 >R2</h1>
        <h1 >Create <span>anything</span></h1>
        <h1 >you can imagine</h1>
        <div >Version 1.0 not compleat yet</div>

    </div>

    <div >

        <img src="assets/images/slider-demos-new.webp" alt="slider-demos">
    </div>
</section>
<section>
    <div>sectionn</div>
</section>

  • Related