Javascript+CSS制作的Tab标签组件

Javascript+CSS制作的Tab标签组件

这是一个漂亮的Javascript+CSS制作的Tab标签组件,标签切换的时候有淡出效果。

使用方式

创建标签内容和导航如下

<div class="material-tabs">
  <div class="tabbed-section__selector">
    <a class="tabbed-section__selector-tab-1 active" href="#">Tab 1</a>
    <a class="tabbed-section__selector-tab-2" href="#">Tab 2</a>
    <a class="tabbed-section__selector-tab-3" href="#">Tab 3</a>
    <span class="tabbed-section__highlighter"></span>
  </div>

  <div class="tabbed-section-1 visible">
    Section 1
  </div>
  <div class="tabbed-section-2 hidden">
    Section 2
  </div>
  <div class="tabbed-section-3 hidden">
    Section 3
  </div>
</div>

标签组件CSS/CSS3样式

.material-tabs {
  display: block;
  float: left;
  padding: 16px;
  padding-top: 0;
  width: 100%;
  max-width: 480px;
  left: calc(50% - 480px/2);
  position: relative;
  margin: 96px auto;
  background: #fff;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23) !important;
  border-radius: 2px;
}
@media all and (max-width: 480px) {

.material-tabs {
  max-width: 100%;
  left: 0;
}
}

.visible {
  position: relative;
  opacity: 1;
  width: 100%;
  height: auto;
  float: left;
  -webkit-transition: opacity .35s ease;
  transition: opacity .35s ease;
  z-index: 3;
}

.hidden {
  position: absolute;
  opacity: 0;
  z-index: 0;
  -webkit-transition: opacity 0s ease;
  transition: opacity 0s ease;
}

.tabbed-section__selector {
  position: relative;
  height: 32px;
  top: -31.2px;
  left: -16px;
  padding: 0;
  margin: 0;
  width: 100%;
  float: left;
}

.tabbed-section__selector [class*="-tab-"] {
  float: left;
  display: block;
  height: 32px;
  line-height: 32px;
  width: 100px;
  text-align: center;
  background: #fff;
  font-weight: bold;
  text-decoration: none;
  color: black;
  font-size: 14px;
}

.tabbed-section__selector [class*="-tab-"].active { color: #4F2CCA; }

.tabbed-section__selector a:first-child { border-top-left-radius: 2px; }

.tabbed-section__selector a:last-of-type { border-top-right-radius: 2px; }

.tabbed-section__highlighter {
  position: absolute;
  z-index: 10;
  bottom: 0;
  height: 2px;
  background: #4F2CCA;
  max-width: 100px;
  width: 100%;
  -webkit-transform: translateX(0);
  -ms-transform: translateX(0);
  transform: translateX(0);
  display: block;
  left: 0;
  -webkit-transition: -webkit-transform 0.23s ease;
  transition: transform 0.23s ease;
}

.tabbed-section__selector-tab-3.active ~ .tabbed-section__highlighter {
  -webkit-transform: translateX(200px);
  -ms-transform: translateX(200px);
  transform: translateX(200px);
}

.tabbed-section__selector-tab-2.active ~ .tabbed-section__highlighter {
  -webkit-transform: translateX(100px);
  -ms-transform: translateX(100px);
  transform: translateX(100px);
}

.tabbed-section__selector-tab-1.active ~ .tabbed-section__highlighter {
  -webkit-transform: translateX(0);
  -ms-transform: translateX(0);
  transform: translateX(0);
}

.divider {
  background: rgba(0, 0, 0, 0.1);
  position: relative;
  display: block;
  float: left;
  width: 100%;
  height: 1px;
  margin: 8px 0;
  padding: 0;
  overflow: hidden;
}

标签组件JavaScript代码

var $ = function (selector) {
  return document.querySelectorAll(selector);
};

var tabs = [
  '.tabbed-section__selector-tab-1',
  '.tabbed-section__selector-tab-2',
  '.tabbed-section__selector-tab-3'
];

var toggleTab = function(element) {
  var parent = element.parentNode;
  
  $(element)[0].addEventListener('click', function(){
    this.parentNode.childNodes[1].classList.remove('active');
    this.parentNode.childNodes[3].classList.remove('active');
    this.parentNode.childNodes[5].classList.remove('active');

    this.classList.add('active');
    
    if(this.classList.contains('tabbed-section__selector-tab-1')) {
      // and change the classes, show the first content panel
      $('.tabbed-section-1')[0].classList.remove('hidden');
      $('.tabbed-section-1')[0].classList.add('visible');
      
      $('.tabbed-section-2')[0].classList.remove('visible');
      $('.tabbed-section-2')[0].classList.add('hidden');
       $('.tabbed-section-3')[0].classList.remove('visible');
      $('.tabbed-section-3')[0].classList.add('hidden');
    }

    if(this.classList.contains('tabbed-section__selector-tab-2')) {
      $('.tabbed-section-2')[0].classList.remove('hidden');
      $('.tabbed-section-2')[0].classList.add('visible');
      $('.tabbed-section-1')[0].classList.remove('visible');
      $('.tabbed-section-1')[0].classList.add('hidden');
      $('.tabbed-section-3')[0].classList.remove('visible');
      $('.tabbed-section-3')[0].classList.add('hidden');
    }
    
    if(this.classList.contains('tabbed-section__selector-tab-3')) {
      $('.tabbed-section-3')[0].classList.remove('hidden');
      $('.tabbed-section-3')[0].classList.add('visible');
      $('.tabbed-section-1')[0].classList.remove('visible');
      $('.tabbed-section-1')[0].classList.add('hidden');
      $('.tabbed-section-2')[0].classList.remove('visible');
      $('.tabbed-section-2')[0].classList.add('hidden');
    }
  });
};

for (var i = tabs.length - 1; i >= 0; i--) {
  toggleTab(tabs[i])
};
打赏
下载 在线预览 积分 (-2) 吐槽 (1)
×
加载中,请稍候…

您需要 登录 后才能发表评论。

avatar

- 评论

  • 加载中,请稍候…
我要吐槽