简体   繁体   English

使用 vanilla JS 突出显示活动选项卡

[英]highlighting an active tab with vanilla JS

I am creating a tabbed navigation bar wherein when the tab is active it should change its color the color that i set it to change with. I am creating a tabbed navigation bar wherein when the tab is active it should change its color the color that i set it to change with. Navigating thru the pages with the tabs works fine however the color highlight on the active tab just don't seem to work.使用选项卡浏览页面工作正常,但活动选项卡上的颜色突出显示似乎不起作用。

here is my code so far:到目前为止,这是我的代码:

HTML: HTML:

<section class="tab" id="active_Current_Tabs">
      <header>Active/Current Tabs</header>
      <nav class="navbar">
        <ul>
          <li class="btn currentTab" onclick="activeTab(event, 'lorem7')">TAB1</li>
          <li class="btn currentTab" onclick="activeTab(event, 'lorem8')">TAB2</li>
          <li class="btn currentTab" onclick="activeTab(event, 'lorem9')">TAB3</li>
        </ul>
      </nav>
      <main class="main-doc">
        <article class="selectedPage" id='lorem7'>
          <h2>Lorem</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
        </article>
        <article class="selectedPage" id="lorem8">
          <h2>Lorem</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
        </article>
        <article class="selectedPage" id="lorem9">
          <h2>Lorem</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
        </article>
      </main>
    </section>

CSS: CSS:

article {
  text-align: center;
  width: 100%;
  height: 300px;
  max-height: 300px;
  margin: 0;
}

/*navbar css*/

nav {
width: 100%;
height: 75px;
padding: 0;
margin: 0;
}

nav ul {
  height: 100%;
  padding: 0;
  display: flex;
  list-style: none;
  justify-content: space-around;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
  height: 100%;
  background-color: #8b9d98;
  cursor: pointer;
  font-weight: 500;
}

.btn:hover {
  background-color: #d7e0e0;
  font-weight: 700;
  transition: .5s;
}

/*main css*/
main {
  margin-top: 0;
}


/*Active/Current Tab */

#lorem7 {
  display: flex;
  flex-direction: column;
  background-color: #49c2a4;
  align-items: center;
  justify-content: center;
}

#lorem8 {
  display: none;
  flex-direction: column;
  background-color:#35386f;
  align-items: center;
  justify-content: center;
}

#lorem9 {
  display: none;
  flex-direction: column;
  background-color:#e28968;
  align-items: center;
  justify-content: center;
}

Javascript: Javascript:

// active/current tab function

function activeTab(evnt, currPage) {
  var currenttab;
  var pages = document.getElementsByClassName('selectedPage');
  for (i = 0; i < pages.length; i++) {
    pages[i].style.display = "none";
  }
  //for dehighlighting inactive tabs
  currenttab = document.getElementsByClassName('currentTab');
  for(j = 0; j < currenttab.length; j++) {
    currenttab[j].className = currenttab[j].className.replace("green", " ");
  }
  document.getElementById(currPage).style.display = "flex";
  evnt.currentTarget.className += "green"; //this appends the color to active tab
}

help please!请帮忙! T_T T_T

evnt.currentTarget.className += "green";

This line will add to the already existing className.此行将添加到现有的 className。

So your class class="btn currentTab" becomes class="btn currentTabgreen" instead of class="btn currentTab green" if you didn't add green to it before.因此,如果您之前没有添加绿色,您的 class class="btn currentTab"将变为class="btn currentTabgreen"而不是class="btn currentTab green" So it would better to use currenttab[j].className.replace("green", "");所以最好使用currenttab[j].className.replace("green", ""); to reset the previous green classes and evnt.currentTarget.className += " green";重置之前的绿色类和evnt.currentTarget.className += " green"; to set the new green class.设置新的绿色类。

Edit: this does imply that the classname will keep growing with one space every time.编辑:这确实意味着类名每次都会以一个空格继续增长。 SO the optimal would be to use classList.add() and classList.remove() instead of manually editting the class string.所以最好的方法是使用classList.add()classList.remove()而不是手动编辑类字符串。

 function activeTab(evnt, currPage) { var currenttab; var pages = document.getElementsByClassName('selectedPage'); for (i = 0; i < pages.length; i++) { pages[i].style.display = "none"; } //for dehighlighting inactive tabs currenttab = document.getElementsByClassName('currentTab'); for(j = 0; j < currenttab.length; j++) { currenttab[j].className = currenttab[j].className.replace("green", ""); } document.getElementById(currPage).style.display = "flex"; evnt.currentTarget.className += " green"; //this appends the color to active tab }
 article { text-align: center; width: 100%; height: 300px; max-height: 300px; margin: 0; } /*navbar css*/ nav { width: 100%; height: 75px; padding: 0; margin: 0; } nav ul { height: 100%; padding: 0; display: flex; list-style: none; justify-content: space-around; } .btn { display: flex; align-items: center; justify-content: center; text-align: center; width: 100%; height: 100%; background-color: #8b9d98; cursor: pointer; font-weight: 500; } .btn:hover { background-color: #d7e0e0; font-weight: 700; transition: .5s; } /*main css*/ main { margin-top: 0; } /*Active/Current Tab */ #lorem7 { display: flex; flex-direction: column; background-color: #49c2a4; align-items: center; justify-content: center; } #lorem8 { display: none; flex-direction: column; background-color:#35386f; align-items: center; justify-content: center; } #lorem9 { display: none; flex-direction: column; background-color:#e28968; align-items: center; justify-content: center; } .green { background-color: green; }
 <section class="tab" id="active_Current_Tabs"> <header>Active/Current Tabs</header> <nav class="navbar"> <ul> <li class="btn currentTab" onclick="activeTab(event, 'lorem7')">TAB1</li> <li class="btn currentTab" onclick="activeTab(event, 'lorem8')">TAB2</li> <li class="btn currentTab" onclick="activeTab(event, 'lorem9')">TAB3</li> </ul> </nav> <main class="main-doc"> <article class="selectedPage" id='lorem7'> <h2>Lorem</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p> </article> <article class="selectedPage" id="lorem8"> <h2>Lorem</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p> </article> <article class="selectedPage" id="lorem9"> <h2>Lorem</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p> </article> </main> </section>

I'm not sure what you were trying to do with the "green" class because there were no rules for it in your CSS.我不确定你想用“绿色”类做什么,因为在你的 CSS 中没有关于它的规则。 I answered the question assuming you wanted the active tab to be the same color as the active page.我回答了这个问题,假设您希望活动选项卡与活动页面的颜色相同。 Sorry if that's not what you intended, but I think it makes sense.对不起,如果这不是你的意图,但我认为这是有道理的。

To avoid problems with specific class names, you can use .classList methods like "add" and "remove".为避免特定类名出现问题,您可以使用 .classList 方法,如“add”和“remove”。 This way you don't have to worry about the order of class names in the markup.这样您就不必担心标记中类名的顺序。 Examples:例子:

tabs[i].classList.remove('active')
e.currentTarget.classList.add('active')

You can also attach your event listener (click handler) dynamically to keep your HTML uncluttered.您还可以动态附加您的事件侦听器(单击处理程序)以保持 HTML 整洁。 Example:例子:

for(j = 0; j < tabs.length; j++) {
    // attach event listener to all tabs
    tabs[j].addEventListener('click', clickTab)
}

You can also make your CSS less repetitive by assigning similar styles to a common class:您还可以通过将相似的样式分配给一个公共类来减少 CSS 的重复性:

.page {display:none;}
.page.active {
    display:flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

I modified your IDs in order to be able to reference tabs and pages independently without explicitly passing parameters to the click handler functions.我修改了您的 ID,以便能够独立引用选项卡和页面,而无需将参数显式传递给点击处理函数。 Example:例子:

<li id="t2" class="tab">TAB2</li>
...
<article class="page" id="p2">...</article>

Here is my JS Bin:这是我的 JS Bin:

http://jsbin.com/defidih/edit?html,css,js,console,output http://jsbin.com/defidih/edit?html,css,js,console,output

To create a tabbed navigation bar wherein when the tab is active it should change its color to a custom color that you have set. To create a tabbed navigation bar wherein when the tab is active it should change its color to a custom color that you have set. You could use this few lines of vanilla JavaScript.您可以使用这几行普通的 JavaScript。

JS: JS:

var activeTab;
var acctOptions = document
  .querySelector(".account-options")
  .querySelectorAll("li");
acctOptions.forEach(option => {
  option.addEventListener("click", function() {
    if (activeTab) activeTab.classList.remove("active");
    activeTab = option;
    activeTab.classList.add("active");
  });
});

CSS: CSS:

.active {
  background: blue;
}

HTML: HTML:

<ul class="account-options">
   <li class='login'><a>Login</a></li>
   <li class='register'><a href="register.html">Register</a></li>
   <li class='account' ><a>My Account</a></li>
   <li class='reward-points'><a>Reward Points</a></li>
   <li class='password-reset'><a>Reset Password</a></li>
   <li class='logout'><a>Logout</a></li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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