<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>移动端响应式图像选择器</title>
<style>
/* 全局样式重置 */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
padding: 15px;
background-color: #f8f9fa;
}
/* 响应式计数器 */
#selectionCounter {
position: sticky;
top: 0;
background: white;
padding: 12px 15px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 100;
font-size: 16px;
text-align: center;
border-radius: 12px;
margin-bottom: 15px;
backdrop-filter: blur(10px);
background-color: rgba(255, 255, 255, 0.85);
}
/* 响应式网格布局 - 移动设备优先 */
#imageGallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 15px;
}
/* 平板设备 */
@media (min-width: 600px) {
#imageGallery {
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 20px;
}
}
/* 桌面设备 */
@media (min-width: 900px) {
#imageGallery {
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 25px;
}
}
/* 用户卡片容器 */
.user-container {
position: relative;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
transition: all 0.25s ease;
background: white;
aspect-ratio: 3/4; /* 保持统一高度比例 */
}
.user-container:hover {
transform: translateY(-3px);
box-shadow: 0 6px 16px rgba(0,0,0,0.12);
}
/* 图像容器 */
.image-wrapper {
position: relative;
width: 100%;
height: 0;
padding-top: 140%; /* 保持原始比例 */
}
/* 完整显示图片 */
.user-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain;
background: #f0f2f5;
}
/* 移动端触摸优化 */
.user-container:active {
transform: scale(0.98);
}
/* 自定义复选框 - 移动端友好 */
.custom-checkbox {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
width: 28px;
height: 28px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s;
}
.custom-checkbox::after {
content: "";
width: 14px;
height: 14px;
border-radius: 50%;
background: #4CAF50;
opacity: 0;
transform: scale(0.8);
transition: all 0.2s;
}
input[type="checkbox"]:checked + .custom-checkbox::after {
opacity: 1;
transform: scale(1);
}
/* 用户标签 */
.user-label {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(transparent, rgba(0,0,0,0.7));
color: white;
padding: 12px 8px 8px;
font-size: 14px;
font-weight: 500;
text-align: center;
}
/* 选中状态 */
.user-container.selected {
box-shadow: 0 0 0 3px #4CAF50;
}
/* 底部操作栏 - 移动端优化 */
#actionBar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: white;
padding: 15px;
box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
display: flex;
justify-content: space-around;
z-index: 100;
}
.action-btn {
padding: 12px 25px;
border-radius: 50px;
border: none;
background: #4CAF50;
color: white;
font-weight: 600;
font-size: 16px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: all 0.2s;
}
.action-btn:active {
transform: translateY(2px);
box-shadow: 0 2px 3px rgba(0,0,0,0.1);
}
#resetBtn {
background: #f44336;
}
</style>
</head>
<body>
<div id="selectionCounter">Selected: <span id="count">0</span>/2</div>
<div id="imageGallery"></div>
<div id="actionBar">
<button id="confirmBtn" class="action-btn">Confirm selection</button>
<button id="resetBtn" class="action-btn">Reset</button>
</div>
<script>
// 示例用户数据(不同比例图像)
const users = [
{ id: "ID30", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID53.jpg" },
{ id: "ID27", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID2.jpg" },
{ id: "ID45", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID3.jpg" },
{ id: "ID12", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID4.jpg" },
{ id: "ID88", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID5.jpg" },
{ id: "ID76", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID6.jpg" },
{ id: "ID15", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID7.jpg" },
{ id: "ID22", image: "https://digimktgsolution.com/HA_HousingInTAIPhotoGallery/upload/ID8.jpg" }
];
const MAX_SELECTION = 2;
// 创建图像卡片
function createImageCards() {
const gallery = document.getElementById('imageGallery');
users.forEach(user => {
// 创建卡片容器
const container = document.createElement('div');
container.className = 'user-container';
container.dataset.userId = user.id;
// 图像包装容器
const imageWrapper = document.createElement('div');
imageWrapper.className = 'image-wrapper';
// 创建图像
const image = document.createElement('img');
image.className = 'user-image';
image.src = user.image;
image.alt = `${user.id} 的照片`;
image.loading = "lazy"; // 延迟加载优化
// 隐藏的复选框
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `checkbox-${user.id}`;
checkbox.className = 'user-checkbox';
checkbox.style.display = 'none';
// 自定义可视化复选框
const customCheckbox = document.createElement('div');
customCheckbox.className = 'custom-checkbox';
customCheckbox.setAttribute('for', checkbox.id);
// 用户ID标签
const label = document.createElement('div');
label.className = 'user-label';
label.textContent = user.id;
// 组装组件
imageWrapper.appendChild(image);
container.appendChild(checkbox);
container.appendChild(customCheckbox);
container.appendChild(imageWrapper);
container.appendChild(label);
gallery.appendChild(container);
// 触摸/点击事件
container.addEventListener('click', function(e) {
if (e.target !== checkbox) {
const currentChecked = checkbox.checked;
const selectedCount = document.querySelectorAll('.user-checkbox:checked').length;
// 移动端触摸反馈
this.classList.add('touched');
setTimeout(() => this.classList.remove('touched'), 200);
// 检查选择限制
if (!currentChecked && selectedCount >= MAX_SELECTION) {
alert(`You can only select up to ${MAX_SELECTION} users`);
return;
}
checkbox.checked = !currentChecked;
updateSelectionState(checkbox);
updateSelectionCount();
}
});
});
}
// 更新选择状态
function updateSelectionState(checkbox) {
const container = checkbox.closest('.user-container');
if (checkbox.checked) {
container.classList.add('selected');
} else {
container.classList.remove('selected');
}
}
// 更新选择计数
function updateSelectionCount() {
const selected = document.querySelectorAll('.user-checkbox:checked').length;
document.getElementById('count').textContent = selected;
}
// 确认选择
document.getElementById('confirmBtn').addEventListener('click', () => {
const selected = [...document.querySelectorAll('.user-checkbox:checked')]
.map(cb => cb.id.replace('checkbox-', ''));
if(selected.length === 0) {
alert('Please select at least 1 user');
} else {
alert(`Confirmed user selection: ${selected.join(', ')}`);
// 实际应用中这里可以处理后续操作
}
});
// 重置选择
document.getElementById('resetBtn').addEventListener('click', () => {
document.querySelectorAll('.user-checkbox').forEach(cb => {
cb.checked = false;
updateSelectionState(cb);
});
updateSelectionCount();
});
// 初始化
document.addEventListener('DOMContentLoaded', () => {
createImageCards();
updateSelectionCount();
// 移动端手势检测
document.querySelectorAll('.user-container').forEach(container => {
container.addEventListener('touchstart', function() {
this.classList.add('touched');
});
container.addEventListener('touchend', function() {
this.classList.remove('touched');
});
});
});
</script>
</body>
</html>
根據以上代碼,調整image checkbox 根據圖片的高度調整大小,不用每個一樣高度
最新发布