jQuery - 搜索结果查看 - 列表&格

时间:2013-10-28 11:14:08

标签: javascript jquery optimization

我是jQuery的新手,我正在创建一个结果页面,用户可以在列表和网格视图之间切换。它似乎确实有效,但当我有很多结果时,它似乎是错误的。谁能明白为什么会这样呢?

CODEPEN DEMO

非常感谢任何帮助。

JS

    $('.btn.grid').click(function() {
      if (!$(this).hasClass("active")) {
        $(this).addClass("active");
        $('.results-wrapper .grid_12').removeClass("grid_12").addClass("grid_3");
        $('.wrapper .results').addClass("grid-view-active");
        if ($(".btn.list").hasClass("active")) {
          $(".btn.list").removeClass("active");
          $('.wrapper .results').removeClass("list-view-active");
        }
      }
    });
    $('.btn.list').click(function() {
      if (!$(this).hasClass("active")) {
        $(this).addClass("active");
        $('.results-wrapper .grid_3').removeClass("grid_3").addClass("grid_12");
        $('.wrapper .results').addClass("list-view-active");
        if ($(".btn.grid").hasClass("active")) {
          $(".btn.grid").removeClass("active");
          $('.wrapper .results').removeClass("grid-view-active");
        }
      }
    });

HTML

<span class="btn grid active">grid</span>
<span class="btn list">list</span>

<div class="wrapper">
  <div class="results-wrapper">
    <ul class="results">
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
      <li class="grid_3">
        <h1>Title</h1>
        <img src="http://rosherunwoven.co.uk/images/Nike%20Free%20Trainer%205.0%20Mens%20Trainers%20Black.jpg"/>
      </li>
    </ul>
  </div>
</div>

CSS

.btn {
  background: #ccc;
  cursor: pointer;
  display: inline-block;
  height: 35px;
  width: 35px;
}
.btn.active {
  background: red;
}
li {
  float: left;
  height: 200px;
  width: 200px;
}
li img {
  max-width: 100%;
  max-height: 100%;
}
.grid_3 {
  width: 25%;
}
.grid_12 {
  width: 100%;
}

1 个答案:

答案 0 :(得分:1)

var gridButton = $('.btn.grid');
var listButton = $('.btn.list');
var isGridActive = true;

gridButton.click(function() { 
  if(!isGridActive) {
    toggleFunction();
  }
});


listButton.click(function() { 
  if(isGridActive) {
    toggleFunction();
  }
});

var toggleFunction = function(){
  listButton.toggleClass("active");
  gridButton.toggleClass("active");

  if(isGridActive) {
    $('.results-wrapper .grid_3').removeClass("grid_3").addClass("grid_12");
  $('.wrapper .results').addClass("list-view-active").removeClass("grid-view-active");
  } else { 
    $('.results-wrapper .grid_12').removeClass("grid_12").addClass("grid_3");
        $('.wrapper .results').addClass("grid-view-active").removeClass("list-view-active");
  }
  isGridActive = !isGridActive;
};

如果有人能够做出更好的if声明,那就更好了。