网格视图列表视图切换
Grid View List View Toggle
我目前有一个以列表格式列出产品的页面。我希望能够在两种视图之间切换。列表是动态填充的。
下面是一个静态的ul,我想动态填充它:
$('button').on('click', function(e) {
if ($(this).hasClass('grid')) {
$('#container ul').removeClass('list').addClass('grid');
} else if ($(this).hasClass('list')) {
$('#container ul').removeClass('grid').addClass('list');
}
});
<div id="container">
<div class="buttons">
<button class="grid">Grid View</button>
<button class="list">List View</button>
</div>
<ul class="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
</ul>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("a.switcher").bind("click", function(e) {
e.preventDefault();
var theid = $(this).attr("id");
var theproducts = $("ul#products");
var classNames = $(this).attr('class').split(' ');
if ($(this).hasClass("active")) {
return false;
} else {
if (theid == "gridview") {
$(this).addClass("active");
$("#listview").removeClass("active");
$("#listview").children("img").attr("src", "images/list-view.png");
var theimg = $(this).children("img");
theimg.attr("src", "images/grid-view-active.png");
theproducts.removeClass("list");
theproducts.addClass("grid");
} else if (theid == "listview") {
$(this).addClass("active");
$("#gridview").removeClass("active");
$("#gridview").children("img").attr("src", "images/grid-view.png");
var theimg = $(this).children("img");
theimg.attr("src", "images/list-view-active.png");
theproducts.removeClass("grid")
theproducts.addClass("list");
}
}
});
});
</script>
<table style="float: right;">
<tr>
<td>
<a href="#" id="gridview" class="switcher">
<img src="images/grid-view.png" alt="Grid">
</a>
</td>
<td> </td>
<td>
<a href="#" id="listview" class="switcher active">
<img src="images/list-view-active.png" alt="List">
</a>
</td>
</tr>
</table>
相关文章:
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- 如何将参数从列表视图中的项传递到模板
- 根据手机上是否存在文件,在jQuery mobile中动态填充列表视图
- 如何在我传递给视图的ViewModel集合中获得jQuery可选对象的值,以作为有序列表进行迭代
- 取消选择滚动启动时的所有列表视图项目
- 使用PhoneGap和jQuery Mobile在android设备上显示动态列表视图
- 通过格式化将jquery移动组件动态添加到列表视图中
- 如何让li项目在这个jQuery列表中滑动和/或淡出视图
- jqmobile列表视图中元素之间的间距相等
- 如何在Vue.js中使用列表视图
- jQuery移动列表视图和面板
- 剑道移动列表视图更改过滤器onclick
- 无法将对象列表从视图模型设置为 javascript 变量
- Metro 应用程序中的列表视图项目单击
- 列表视图在Android上滚动断断续续且缓慢
- 带有砖石结构的轴网视图/列表视图
- 在不涉及控制器的情况下对MVC视图列表中的项进行排序
- 网格视图列表视图切换
- 推送到数组不会通过ng repeat指令更新视图列表
- Javascript / jquery在两个树视图列表之间拖放