jQuery实现文件列表和缩略图模式切换

  做一个简单的仿网盘前端页面,实现文件以列表模式和缩略图模式切换功能。

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8">
		<title>列表/缩略图模式切换</title>
		<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
		<style>
			* {
				margin: 0;
				padding: 0;
				box-sizing: border-box;
				font-family: Arial, sans-serif;
			}

			.container {
				max-width: 1450px;
				margin: 30px auto;
				padding: 0 20px;
			}

			/* 切换按钮 */
			.view-switch {
				margin-bottom: 20px;
				display: flex;
				gap: 10px;
			}

			.view-switch button {
				padding: 8px 16px;
				border: 1px solid #ddd;
				background: #fff;
				cursor: pointer;
				border-radius: 4px;
				font-size: 14px;
			}

			.view-switch button.active {
				background: #409eff;
				color: #fff;
				border-color: #409eff;
			}

			/* 列表表格 */
			.file-list {
				width: 100%;
				border-collapse: collapse;
			}

			.file-list th,
			.file-list td {
				padding: 12px 15px;
				text-align: left;
				border-bottom: 1px solid #eee;
			}

			.file-list th {
				background: #f5f7fa;
				font-weight: 600;
			}

			/* 缩略图容器 */
			.file-grid {
				display: grid;
				grid-template-columns: repeat(5, 1fr); /* 每行5个 */
				gap: 15px;
			}

			/* 隐藏样式 */
			.grid-hide {
				display: none !important;
			}

			.grid-item {
				border: 1px solid #eee;
				border-radius: 8px;
				padding: 12px;
				transition: 0.2s;
			}

			.grid-item:hover {
				box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
			}

			/* 图片容器 */
			.img-box {
				width: 100%;
				aspect-ratio: 16/9;
				/* 固定比例 */
				overflow: hidden;
				border-radius: 4px;
				margin-bottom: 8px;
			}

			.img-box img {
				width: 100%;
				height: 100%;
				object-fit: cover;
				/* 填满容器 */
			}

			.grid-info {
				font-size: 12px;
				line-height: 1.5;
			}

			.grid-info p {
				color: #666;
				margin: 2px 0;
			}

			.grid-info strong {
				display: block;
				margin-bottom: 5px;
				color: #333;
				font-size: 13px;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<div class="view-switch">
				<button class="active" data-view="list">列表模式</button>
				<button data-view="grid">缩略图模式</button>
			</div>

			<table class="file-list" id="fileList">
				<thead>
					<tr>
						<th>文件名</th>
						<th>大小</th>
						<th>类型</th>
						<th>修改日期</th>
					</tr>
				</thead>
				<tbody id="listBody"></tbody>
			</table>

			<div class="file-grid grid-hide" id="fileGrid"></div>
		</div>

		<script>
			$(function() {
				const fileData = [{
						name: "风景图.jpg",
						size: "2.4MB",
						type: "图片",
						date: "2015-01-15",
						url: "https://picsum.photos/300/200?random=1",
						resolution: "1920×1080"
					},
					{
						name: "文档.pdf",
						size: "850KB",
						type: "PDF",
						date: "2015-01-10",
						url: "https://picsum.photos/300/200?random=2",
						resolution: "-"
					},
					{
						name: "设计稿.png",
						size: "5.1MB",
						type: "图片",
						date: "2015-01-05",
						url: "https://picsum.photos/300/200?random=3",
						resolution: "2560×1440"
					},
					{
						name: "演示文稿.pptx",
						size: "12.8MB",
						type: "PPT",
						date: "2015-12-28",
						url: "https://picsum.photos/300/200?random=4",
						resolution: "-"
					},
					{
						name: "照片.png",
						size: "3.7MB",
						type: "图片",
						date: "2015-12-20",
						url: "https://picsum.photos/300/200?random=5",
						resolution: "1280×720"
					},
					{
						name: "截图.jpg",
						size: "1.2MB",
						type: "图片",
						date: "2015-12-18",
						url: "https://picsum.photos/300/200?random=6",
						resolution: "800×600"
					},
					{
						name: "壁纸.jpg",
						size: "4.2MB",
						type: "图片",
						date: "2015-12-10",
						url: "https://picsum.photos/300/200?random=7",
						resolution: "3840×2160"
					}
				];

				function renderList() {
					let html = '';
					fileData.forEach(item => {
						html +=
							`<tr><td>${item.name}</td><td>${item.size}</td><td>${item.type}</td><td>${item.date}</td></tr>`;
					})
					$("#listBody").html(html);
				}

				function renderGrid() {
					let html = '';
					fileData.forEach(item => {
						html += `
            <div class="grid-item">
              <div class="img-box">
                <img src="${item.url}" alt="${item.name}">
              </div>
              <div class="grid-info">
                <strong>${item.name}</strong>
                <p>大小:${item.size}</p>
                <p>类型:${item.type}</p>
                <p>日期:${item.date}</p>
                <p>分辨率:${item.resolution}</p>
              </div>
            </div>
          `
					})
					$("#fileGrid").html(html);
				}

				renderList();
				renderGrid();

				// 切换视图
				$("[data-view]").click(function() {
					$(this).addClass('active').siblings().removeClass('active');
					let v = $(this).data('view');
					if (v == 'list') {
						$('#fileList').show();
						$('#fileGrid').addClass('grid-hide');
					} else {
						$('#fileList').hide();
						$('#fileGrid').removeClass('grid-hide');
					}
				})
			})
		</script>
	</body>
</html>

本站原创内容,转载请注明来源:https://www.liutonghui.com/114

PHP利用正则表达式提取内容中图片的函数
上一篇 2015-01-07
女教师辞职信:世界那么大,我想去看看
下一篇 2015-04-16

评论列表(0条)

  • 暂无评论

发表评论

captcha

相关推荐

  • jQuery实现文章列表指定行后插入广告

      jQuery插入广告位功能:可指定固定位置,第几行后插入广告和随机某一行后插入广告,每次刷新位置不一样两种模式。 &lt;!DOCTYPE html&gt; &lt;html lang="zh-CN"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;文章列表插入广告&lt;/title&gt; &lt;script src="https://code......

    2014-09-12
    12250
  • Dedecms文章内容页和图片集内容页调用缩略图的方法

      织梦DedeCMS的缩略图一般显示在列表页,但有时我们需要在文章内容页缩略图的调用,图片集内容页缩略图的调用,使用方法如下:   文章内容页和图片集内容页,缩略图的调用。适合内页中调用。 &lt;img src="{dede:field.picname runphp='var_dmp(@me)' /}"/&gt;   文章频道页、列表页调用缩略图: {dede:list pagesize='20'} [field:array ru......

    2015-04-29
    28090
  • jQuery Ajax无刷新批量删除数据功能

      在 jQuery 中使用 Ajax 实现批量删除的基本思路,使用 jQuery 监听&ldquo;批量删除&rdquo;按钮的点击事件,收集用户选择的要删除的项目的标识(例如,ID)。   使用 Ajax 发送异步请求到后端,将要删除的项目标识传递给服务器。在后端,接收到请求后,根据接收到的项目标识进行删除操作,并返回相应的结果给前端。   以下是一个简单的示例代码:   index.html &lt;!DOCTYPE html&g......

    2015-06-15
    21340
  • jQuery为当前栏目导航添加CSS样式

      在网站制作过程中,经常见到很多网站的导航栏中当前所在的栏目分类链接被添加了css样式,呈高亮状态,点击哪个分类链接哪个就会变成高亮状态,那么这种功能是怎么实现的呢,其实用jQuery就可以很轻松的实现。   jquery代码: /* 当前链接加css */ $('nav ul li a').each(function () { if ($(this)[0].href == String(window.location)) $(t......

    2015-11-06
    20900
  • Ajax实现无刷新表格实时编辑

      简单的jQuery Ajax表格实时编辑功能,点击单元格数据即可实时编辑更新数据,无需刷新跳转页面。   index.html &lt;!DOCTYPE html&gt; &lt;html lang="utf-8"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Ajax 表格实时编辑&lt;/title&gt; &lt;script src="ht......

    2016-02-20
    12260
  • 使用 Layui 和 jQuery 实现表单验证

      对于 Layui 表单验证,可以结合 jQuery 使用 Layui 的验证规则。以下是一个示例,展示了如何使用 Layui 和 jQuery 实现用户名、密码、手机号、邮箱、身份证号的表单验证: &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" con......

    2020-06-14
    21380
  • PHP文字水印,缩略图,图片水印实现类与用法示例

      本文实例讲述了PHP开发的文字水印,缩略图,图片水印实现类与用法。分享给大家供大家参考,具体如下:   1.实现类ImageToTest.class.php参考代码 class ImageToTest { /** * 图片的基本信息 */ private $info; private $image; public function __construct($src){ $info = getima......

    2020-08-22
    19930
  • PHP生成任意尺寸并居中裁剪缩略图的函数

      在项目中经常需要对图片进行各种处理,比如生成缩略图,并且生成的缩略图还要保持原有图片的宽高比例不变形,下面就分享一个非常好用的PHP生成缩略图函数,可以手动输入自定义尺寸并居中裁剪保持图片比例不变形,代码如下: &lt;?php // 应用公共文件 /** * 居中裁剪图片 * @param string $source [原图路径] * @param int $width [设置宽度] * @param int $heig......

    2020-10-03
    19922