引自免费微信小程序:皆我百晓生
在您的HTML代码中,您已经成功地设置了容器的宽度、高度以及边框样式,并且将内容放在了元素内。为了实现无缝滚动效果,您可以考虑使用JavaScript结合CSS Flexbox布局。以下是修改后的代码示例:
首先,确保您的CSS文件包含以下规则:
/* 基础样式 */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
/* 容器样式 */
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* 内容区域样式 */
.content {
max-width: 800px;
padding: 2rem;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
/* 图片列表样式 */
.img-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
/* 图片样式 */
.img-item {
width: 100%;
max-width: 150px;
height: auto;
margin-bottom: 20px;
position: relative;
overflow: hidden;
}
/* 图片缩略图样式 */
.img-thumbnail {
width: 100%;
height: auto;
object-fit: cover;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
/* 省略图片 */
.img-ellipsis {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 50%;
z-index: -1;
width: 100%;
height: 100%;
background: #ccc;
opacity: 0.5;
}
/* 初始状态 */
.img-item.is-expanded {
animation: expand 1s cubic-bezier(0.165, 0.84, 0.44, 1) forwards;
}
/* 缩放动画 */
@keyframes expand {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}
接下来,添加JavaScript代码来控制滑动效果:
// 获取所有图片元素
var imgs = document.querySelectorAll('.img-item');
// 添加鼠标悬停事件处理程序
imgs.forEach(img => {
img.addEventListener('mouseover', () => {
// 将图片缩略图显示为完全展开状态
img.classList.add('is-expanded');
});
img.addEventListener('mouseout', () => {
// 当移除鼠标时,隐藏缩略图
img.classList.remove('is-expanded');
});
});
通过上述步骤,您应该可以实现一个无缝滚动的效果。注意,此代码仅适用于标准HTML5页面结构。如果您尝试在非标准环境中(例如移动应用)使用此代码,可能需要进行一些调整。








