轮播相册
该组件为本地引入图片并嵌入到下方容器实现循环往返式轮播
引入
HTML中代码为:
<div class="projectItemRightimg">
<img data-src="assets/1ae.gif" id="img-1" alt="description">
<img data-src="assets/2ae.gif" id="img-1" alt="description">
<img data-src="assets/3ae.gif" id="img-1" alt="description">
<img data-src="assets/4ae.gif" id="img-1" alt="description">
<img data-src="assets/5ae.gif" id="img-1" alt="description">
<img data-src="assets/6ae.gif" id="img-1" alt="description">
</div>
css部分为
.projectItemRightimg {
transition: width 0.4s ease;
display: flex;
align-items: center;
backdrop-filter: blur(10px);
background: rgba(20, 20, 20, 0.396);
height: 120px; /* 设置一个合适的高度,或者根据需要自动调整 */
max-width: 99%;
overflow: hidden; /* 隐藏超出容器的内容 */
}
#img-1 {
max-width: 100%;
max-height: 100%;
margin-right: 6px; /* 添加右侧间隔 */
}
JS代码为
const projectItemRightimg = document.querySelector('.projectItemRightimg');
const img = document.getElementById('img-1');
// 克隆第一张图片并添加到末尾以实现循环效果
const firstImg = projectItemRightimg.firstElementChild.cloneNode(true);
projectItemRightimg.appendChild(firstImg);
let startX = 0;
let scrollLeft = 0;
let autoScrollInterval = null;
const autoScrollSpeed = 0.5; // 调整自动滑动的速度,值越小滑动越慢
function handleMouseDown(e) {
e.preventDefault();
startX = e.pageX - projectItemRightimg.offsetLeft;
scrollLeft = projectItemRightimg.scrollLeft;
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
document.addEventListener('mouseleave', handleMouseUp);
stopAutoScroll(); // 当用户开始手动滑动时,停止自动滑动
}
function handleMouseMove(e) {
const x = e.pageX - projectItemRightimg.offsetLeft;
const walk = (x - startX) * 3; // 滑动速度
projectItemRightimg.scrollLeft = scrollLeft - walk;
}
function handleMouseUp() {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
document.removeEventListener('mouseleave', handleMouseUp);
startAutoScroll(); // 当用户停止手动滑动时,重新开始自动滑动
}
function handleTouchStart(e) {
startX = e.touches[0].pageX - projectItemRightimg.offsetLeft;
scrollLeft = projectItemRightimg.scrollLeft;
stopAutoScroll(); // 当用户开始触摸滑动时,停止自动滑动
}
function handleTouchMove(e) {
e.preventDefault();
const x = e.touches[0].pageX - projectItemRightimg.offsetLeft;
const walk = (x - startX) * 3; // 滑动速度
projectItemRightimg.scrollLeft = scrollLeft - walk;
}
function startAutoScroll() {
autoScrollInterval = setInterval(() => {
projectItemRightimg.scrollLeft += autoScrollSpeed;
// 检查是否滚动到了克隆的图片区域,这里减去2是位移的缓冲
if (projectItemRightimg.scrollLeft >= projectItemRightimg.scrollWidth - projectItemRightimg.clientWidth - 2) {
// 如果滚动到了克隆的图片,立即返回到正确的位置
projectItemRightimg.scrollLeft = 0;
}
}, 16); //大约每秒60帧
}
function stopAutoScroll() {
if (autoScrollInterval) {
clearInterval(autoScrollInterval);
autoScrollInterval = null;
}
}
// 懒加载功能
function lazyLoad() {
const lazyImages = document.querySelectorAll('img[data-src]');
const config = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
let observer = new IntersectionObserver((entries, self) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
let img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
self.unobserve(img);
}
});
}, config);
lazyImages.forEach(image => {
observer.observe(image);
});
}
// 初始化懒加载
document.addEventListener('DOMContentLoaded', lazyLoad);
// 添加事件监听器
projectItemRightimg.addEventListener('mousedown', handleMouseDown);
projectItemRightimg.addEventListener('touchstart', handleTouchStart);
projectItemRightimg.addEventListener('touchmove', handleTouchMove);
// 开始自动滑动
startAutoScroll();
// 图片弹出监听
function pop(imgPath) {
document.getElementById('popupImage').src = imgPath;
document.getElementById('imagePopup').style.display = 'block';
document.getElementById('imagePopup').addEventListener('click', closePopup);
}
function closePopup() {
document.getElementById('imagePopup').style.display = 'none';
document.getElementById('imagePopup').removeEventListener('click', closePopup);
}