在网页HTML / Javascript中的两个菜单选项卡之间切换

时间:2019-04-07 10:16:23

标签: javascript html

我想在显示不同图形的两个选项卡之间切换。当前,页面打开并打开第一个选项卡,您可以选择第二个选项卡。但是,您无法重新打开第一个标签。我希望能够在两个标签之间自由切换。

我正在尝试修复一些我没有构建的东西,并且对其工作原理的了解非常有限。

HTML代码是:

                <div class="tabsmenuwaste" id="tabsldelay" style="margin-left: auto; margin-right:auto; Margin-top: -50px;">


                    <div id="tab1waste" class="tabheaderwaste" onClick="JavaScript:selectTabwaste(1);">
                        <h4 id="tab1headerwaste" class="active">Daily Summary</h4>
                    </div>

                    <div id="tab2waste" class="tabheaderwaste" onClick="JavaScript:selectTabwaste(2);">
                        <h4 id="tab2headewaster">How's my Business</h4>
                    </div>


                </div>

JavaScript代码为:

function selectTabwaste(tabIndex) {

        //Hide All Tabs
        var index = 0;
        var lb = document.querySelectorAll("div.tableauPlaceholderhub").length;


        for (; index < lb; index++) {

            document.querySelectorAll("div.tableauPlaceholderhub")[index].style.display = "none";

            document.querySelectorAll("div.tableauPlaceholderhmb")[index].style.display = "none";

        }

        if (tabIndex == 2) {
            document.getElementById("tab1waste").getElementsByTagName("h4")[0].className = "tabheaderwaste";
            document.getElementById("tab2waste").getElementsByTagName("h4")[0].className = "tabheaderwaste active";

        } else if (tabIndex == 1) {

          document.getElementById("tab1waste").getElementsByTagName("h4")[0].className = "tabheaderwaste active";
            document.getElementById("tab2waste").getElementsByTagName("h4")[0].className = "tabheaderwaste";
        }

        var index = 0;
        var l = document.querySelectorAll("div.tab" + tabIndex + "Contentwaste").length;

        for (; index < l; index++) {

            document.querySelectorAll("div.tab" + tabIndex + "Contentwaste")[index].style.display = "block";

        }

        //Show the Selected Tab
    }

我希望能够在两个标签之间自由切换。

1 个答案:

答案 0 :(得分:1)

使用此代码。

'use strict';

let	tab = document.getElementsByClassName('tab'),
		tabContent = document.getElementsByClassName('tab-item-content'),
		selector = document.querySelector('.selector'),
		tabs = document.querySelector('.tabs');

function hideTabsContent(a) {
		for (let i = a; i < tabContent.length; i++) {
			tabContent[i].classList.remove('active');
			tab[i].classList.remove('active-item');
		}
	}

selector.style.width = (tab[0].offsetWidth + 'px');

tabs.addEventListener('click', function () {
	let target = event.target;
	if (target.className === 'tab') {
		for (let i = 0; i < tab.length; i++) {
			if (target === tab[i]) {
				showTabsContent(i);
				selector.style.left = (target.offsetLeft + 'px');
				selector.style.width = (target.offsetWidth + 'px');
				break;
			}
		}
	}
});


function showTabsContent(b) {
	hideTabsContent(0);
	tab[b].classList.add('active-item');
	tabContent[b].classList.add('active');
}
@import url('https://fonts.googleapis.com/css?family=Ubuntu');

*{
	margin: 0;
	padding: 0;
}
body{
	font-family: 'Ubuntu', sans-serif;
	background: #6395ed;
	padding-top: 40px;
}
#horizontal-tabs{
	max-width: 600px;
	display: block;
	margin: 0 auto;
}
.tabs{
	display: inline-flex;
	justify-content: flex-start;
	align-items: center;
	list-style: none;
	background-color: #bfd3f8;
	color: #fff;
	font-weight: 400;
	border-radius: 7px;
	width: content-box;
	position: relative;

}
.tab{
	display: block;
	padding: 11px 20px;
	position: relative;
	z-index: 99999;

}
.tab:hover{
	cursor: pointer;
}

.selector{
	display:inline-block;
	position: absolute;
	left:0;
	top:0;
	height: 40px;
	z-index:99999;
	border-radius:7px;
	transition: all 500ms cubic-bezier(0.080, -0.195, 0.140, 0.970);
	background: #92b4f2;
}

.selector:after{
	content:'';
	position: absolute;
	z-index: -1;
	left: calc(50% - 10px);
	bottom: -10px;
	width: 0;
	height: 0;
	border-style: solid;
	border-width: 10px 10px 0 10px;
	border-color: #92b4f2 transparent transparent transparent;
}
.tab-content .tab-item-content{
	background: #ffffff;
	border-radius: 10px;
	box-sizing: border-box;
	display: none;
}

.tab-content .tab-item-content.active{
	padding: 20px;
	height: 200px;
	display: block;
}

li.tab .hidden-tab-name{
	font-size: 0;
	visibility: hidden;
	opacity: 0;
	transition: visibility 0s, opacity 0.5s linear;
}
li.tab.active-item .hidden-tab-name{
	display: inline;
	visibility: visible;
	opacity: 1;
	font-size: inherit;
}
<section class="tabs-section">
  <div id="horizontal-tabs">
    <ul class="tabs">
      <div class="selector"></div>
      <li class="tab active-item">Tab 1</li>
      <li class="tab">2</li>
      <li class="tab"><span class="hidden-tab-name">Hidden name </span>3</li>
      <li class="tab">4 [special long name]</li>
    </ul>
    <div class="tab-content">
      <div id="item1" class="tab-item-content  active">// Some content(1)</div>
      <div id="item2" class="tab-item-content">// Some content (2)</div>
      <div id="item3" class="tab-item-content">// Third Slide</div>
      <div id="item4" class="tab-item-content">// Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus corporis deserunt expedita magni nemo numquam qui quidem quisquam rerum voluptate.</div>
    </div>
  </div>
</section>