Home > database >  (JavaScript) How to Add Prev and Next Button to Tabs?
(JavaScript) How to Add Prev and Next Button to Tabs?

Time:06-18

Hope you all have a nice day and Happy Weekend!

I want to ask about adding prev and next button in my Tabs. I already add it, with some JavaScript that referenced to this https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow.

I know it's kind of weird to reference tab with the carousel but, I think that the principle is the same. But in my code, i don't know how to code it. I'm so sorry because I'm very noob in JavaScript. So, here's my attempt :

//Add & remove class tab, contents, & menu on click
window.addEventListener('DOMContentLoaded', ()=> {
    let tabs = document.querySelectorAll('.tab');
    let content = document.querySelectorAll('.content');
    let prev = document.querySelector('.previous');
    let next = document.querySelector('.next');
    let firstTab = function(tabs) {tabs.classList.add('tab-active')};
    let firstContent = function(content) {content.classList.add('content-active')};
  
    firstTab(tabs[0]);
    firstContent(content[0]);
        
        for (let i = 0; i < tabs.length; i  ) {            
            tabs[i].addEventListener('click', () => tabClick(i));
        }

        prev.addEventListener('click', (i) => tabClick(i - 1));
        next.addEventListener('click', (i) => tabClick(i   1));

        
        function tabClick(currentTab) {
          removeActive();
            //Add Active Class
            tabs[currentTab].classList.add('tab-active');
            content[currentTab].classList.add('content-active');
        }  
        function removeActive() {
          for (let i = 0; i < tabs.length; i  ) {
            //Remove Active Class
            content[i].classList.remove('content-active');
            content[i].classList.add('content-show');
            setTimeout(function() {
              content[i].classList.remove('content-show');
            },1500);
            tabs[i].classList.remove('tab-active');
          }
         }
       })
        /* WHOLE CONTAINER */
        .container {
          width: 96vw;
          height: 96vh;
        }
        
        /* TABS */
        .tabs {
            display: flex;
            height: 50px;
            overflow: hidden;
            align-items: center;
            justify-content: center;
            width: 100%;
        }

        .tab {
            font-size: 14px;
            padding: 5px 10px;
            cursor: pointer;
            letter-spacing: 2px;
        }

        #red.tab-active {background-color: rgb(245, 66, 66);}
        #blue.tab-active {background-color: rgb(66, 135, 245);}
        #yellow.tab-active {background-color: rgb(245, 215, 66);}
        #green.tab-active {background-color: rgb(56, 235, 98);}
        #cyan.tab-active {background-color: rgb(79, 247, 219);}


        /* TAB CONTENTS */
        .contents {
            width: 100%;
            margin-top: 5px;
            height: 80%;
        }

        .content {
            width: 96%;
            height: 80%;
            display: none;
            padding: 0;
            margin: 0;
            border: none;
            position: absolute;
        }

        .content-show {
            display: flex;
            animation-name: fade-out;
            animation-duration: 2.5s;
        }
        
        @keyframes fade-out {
            0% {
                opacity: 1;
                display: flex;
            }
            99% {
                opacity: 0;
                display: flex;
            }
            100% {
                opacity: 0;
                display: none;
            }
        }

        .content-active {
            display: flex;
            border: none;
            justify-content: center;
            animation-name: fade-in;
            animation-duration: 2.5s;
        }

        @keyframes fade-in {
            0% {
                display: none;
                opacity: 0;
            }
            1% {
                display: block;
                opacity: 0.01;
            }
            100%{
                display: block;
                opacity: 1;
            }
        }

        #red.content-active {background-color: rgb(245, 66, 66);}
        #blue.content-active {background-color: rgb(66, 135, 245);}
        #yellow.content-active {background-color: rgb(245, 215, 66);}
        #green.content-active {background-color: rgb(56, 235, 98);}
        #cyan.content-active {background-color: rgb(79, 247, 219);}

        /* BUTTON PREVIOUS NEXT */
        .button {
          position: absolute;
          top: 49%;
          z-index: 4;
        }

        .previous, .next {
          margin: 5px 10px;
          letter-spacing: 2px;
          background-color: white;
          font-size: 14px;
          text-align: center;
          padding: 5px;
          cursor: pointer;
        }
<div >
   <div >
       <div id="red" >RED</div>
       <div id="blue" >BLUE</div>
       <div id="yellow" >YELLOW</div>
       <div id="green" >GREEN</div>
       <div id="cyan" >CYAN</div>
   </div>

   <div >
       <div id="red" ></div>
       <div id="blue" ></div>
       <div id="yellow" ></div>
       <div id="green" ></div>
       <div id="cyan" ></div>
   </div>
  
   <div >
       <div >PREV</div>
       <div >NEXT</div>
   </div>
</div>

Please help me to solve this code guys. Thanks a lot.

CodePudding user response:

The only problem that you are facing is when you click on the prev or next button you have passed the argument i in tabClick function but i is not the index rather it is the property of click event.

Here's what you have done

  • Related