0

我对 jQuery 相当陌生,我正在制作一个结果页面,用户可以在其中在列表和网格视图之间切换。它似乎确实有效,但是当我有很多结果时,它似乎有问题。谁能明白为什么会这样?

CODEPEN 演示

任何帮助将非常感激。

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%;
}
4

1 回答 1

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 语句更好,那就太好了。

于 2013-10-28T12:21:36.253 回答