简体   繁体   English

我该如何使这个Tab控制器代码更简洁一些?

[英]How could I make this tab controller code cleaner and simpler?

How would I be able to simplify this jquery code. 我将如何简化此jquery代码。 I feel like I am repeating myself and just wondering if there is a shorter way to write this which I'm sure there is. 我觉得我在重复自己,只是想知道是否有较短的书写方式,我敢肯定。 I am a bit new to javascript and jquery. 我对javascript和jquery有点陌生。 I have created a two tabs with their own containers with miscellaneous information in them. 我创建了两个选项卡,它们带有自己的容器,其中包含其他信息。 Basically I want the container to open when it's related tab is clicked on. 基本上,我希望在单击“容器”的“相关”选项卡时将其打开。 I also would like the tab to be highlighted when it's active. 我也希望该标签处于活动状态时突出显示。 Also, how would I be able to write code to make all tab containers disappear when you click off from the tab containers. 此外,当您从标签容器中单击时,如何编写代码使所有标签容器消失。

<!-- HTML Code -->
<div class="sort-filters">
  <span class="sort-by active">SORT BY</span>
  <span class="filter">FILTER</span>
</div>

<div class="sort-containers">
  <div class="sort-by-container">Sort by click me here</div>
  <div class="filter-container">Filter click me here</div>
</div>

/* CSS */
.sort-filters {
  display: flex;
  width: 500px;
  height: 30px;
}

.sort-by,
.filter {
  background: #CCC;
  color: #756661;
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Arial', sans-serif;
  cursor: pointer;
}

.sort-by-container,
.filter-container {
  width: 500px;
  background: #756661;
  color: #FFF;
  height: 100px;
  display: none;
}

.active {
  background: #756661;
  color: #FFF;
  transition: 0.2s;
}

// Jquery Code
js = $.noConflict();
var sort = js('.sort-by');
var filter = js('.filter');
var sortContainer = js('.sort-by-container');
var filterContainer = js('.filter-container');

js(sort).click(function() {
  js(filterContainer).hide();
  js(sortContainer).show();
  js(sort).addClass('active');
  js(filter).removeClass('active');
});

js(filter).click(function() {
  js(sortContainer).hide();
  js(filterContainer).show();
  js(filter).addClass('active');
  js(sort).removeClass('active');
});

In order to avoid such repetitive actions I like to stick to naming conventions, so that I can apply the ID's, classes or attributes from one element to select other elements, for instance: 为了避免此类重复操作,我喜欢遵循命名约定,以便可以将一个元素的ID,类或属性应用于其他元素,例如:

<div id="tabs">
  <span class="active" data-type="sort-by">SORT BY</span>
  <span data-type="filter">FILTER</span>
</div>

Now, all you need is one click handler on #tabs span , and get the data-type of the span you clicked on. 现在,您只需要在#tabs span上单击处理程序,即可获得单击的#tabs spandata-type You can use that to filter on the classes of the other container elements. 您可以使用它来过滤其他容器元素的类。

The second thing is that you can attach handler to more than 1 element at the same time. 第二件事是您可以同时将处理程序附加到1个以上的元素。 So in your example, js('#sort-containers div').hide(); 因此,在您的示例中, js('#sort-containers div').hide(); will hide all the div 's that match the selector at once. 会一次隐藏所有与选择器匹配的div

results 结果

I changed some classes to ID's, and some classes to data attributes. 我将某些类更改为ID,将某些类更改为数据属性。 Here's a fiddle: https://jsfiddle.net/mq9xk29y/ 这是一个小提琴: https : //jsfiddle.net/mq9xk29y/

HTML: HTML:

<div id="tabs">
  <span data-type="sort-by">SORT BY</span>
  <span data-type="filter">FILTER</span>
</div>

<div id="sort-containers">
  <div class="sort-by-container">Sort by click me here</div>
  <div class="filter-container">Filter click me here</div>
</div>

JS: JS:

js = $.noConflict();
var $tabs = js('#tabs span');
$tabs.click(function() {
  var $clicked = js(this); //get the element thats clicked on
  var type = $clicked.data('type'); //get the data-type value

  $tabs.removeClass('active'); //remove active from all tabs
  $clicked.addClass('active'); //add active to the current tab
  js('#sort-containers div').hide(); //hide all containers
  js('.' + type + '-container').show().addClass('active'); //add active to current container
});

As long as you follow the naming convention of data-type: bla in the tabs, and bla-container on the classes in sort-container , you never have to worry about coding for additional tabs. 只要遵循data-type: bla的命名约定data-type: bla选项卡中的bla-container以及sort-container的类的bla-container sort-container ,您就不必担心为其他选项卡编码。

There might still be things that could be further optimised, but at least it'll take care of the repetition. 可能仍有一些事情可以进一步优化,但至少它将处理重复问题。

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

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