当前位置:首页 > JavaScript

用js实现图片切换效果

2026-01-31 08:21:00JavaScript

基础图片切换实现

通过修改<img>标签的src属性实现图片切换效果:

const images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
let currentIndex = 0;
const imgElement = document.getElementById('myImage');

function changeImage() {
  currentIndex = (currentIndex + 1) % images.length;
  imgElement.src = images[currentIndex];
}

// 每3秒切换一次
setInterval(changeImage, 3000);

添加过渡动画效果

使用CSS过渡实现淡入淡出效果:

#myImage {
  transition: opacity 0.5s ease-in-out;
}

.fade-out {
  opacity: 0;
}
function fadeImage() {
  imgElement.classList.add('fade-out');

  setTimeout(() => {
    currentIndex = (currentIndex + 1) % images.length;
    imgElement.src = images[currentIndex];
    imgElement.classList.remove('fade-out');
  }, 500);
}

按钮控制切换

添加前进和后退按钮控制:

用js实现图片切换效果

document.getElementById('prevBtn').addEventListener('click', () => {
  currentIndex = (currentIndex - 1 + images.length) % images.length;
  imgElement.src = images[currentIndex];
});

document.getElementById('nextBtn').addEventListener('click', () => {
  currentIndex = (currentIndex + 1) % images.length;
  imgElement.src = images[currentIndex];
});

缩略图导航实现

创建缩略图点击切换功能:

const thumbnailsContainer = document.getElementById('thumbnails');

images.forEach((image, index) => {
  const thumb = document.createElement('img');
  thumb.src = image;
  thumb.className = 'thumbnail';
  thumb.addEventListener('click', () => {
    currentIndex = index;
    imgElement.src = image;
  });
  thumbnailsContainer.appendChild(thumb);
});

响应式图片切换

根据窗口大小加载不同尺寸图片:

用js实现图片切换效果

function getResponsiveImage(index) {
  const width = window.innerWidth;
  if (width < 768) {
    return `mobile-${images[index]}`;
  } else if (width < 1024) {
    return `tablet-${images[index]}`;
  }
  return images[index];
}

window.addEventListener('resize', () => {
  imgElement.src = getResponsiveImage(currentIndex);
});

预加载图片优化

提升切换流畅度:

function preloadImages() {
  images.forEach(image => {
    const img = new Image();
    img.src = image;
  });
}

window.addEventListener('DOMContentLoaded', preloadImages);

无限循环轮播

实现无缝循环效果:

function infiniteCarousel() {
  const firstClone = imgElement.cloneNode();
  const lastClone = imgElement.cloneNode();

  // 添加到DOM
  container.insertBefore(lastClone, imgElement);
  container.appendChild(firstClone);

  let counter = 1;
  const size = imgElement.clientWidth;

  container.style.transform = `translateX(${-size * counter}px)`;

  container.addEventListener('transitionend', () => {
    if (imgElement[counter] === lastClone.id) {
      container.style.transition = 'none';
      counter = images.length - 2;
      container.style.transform = `translateX(${-size * counter}px)`;
    }
    // 类似处理开头情况
  });
}

标签: 效果图片
分享给朋友:

相关文章

vue实现图片预览

vue实现图片预览

Vue 实现图片预览的方法 使用 Element UI 的 el-image 组件 Element UI 提供了 el-image 组件,支持图片预览功能。通过设置 preview-src-list…

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div> <…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现滚动效果

vue实现滚动效果

Vue 实现滚动效果的方法 使用原生滚动方法 通过 Vue 的 ref 获取 DOM 元素,调用原生 scrollTo 或 scrollIntoView 方法实现滚动。 <template&g…

vue实现添加图片

vue实现添加图片

使用 <input type="file"> 上传图片 通过 HTML 原生文件输入框获取用户选择的图片文件,结合 Vue 的 v-on 监听文件变化事件。 <template…

vue 实现图片切换

vue 实现图片切换

实现图片切换的基本思路 在Vue中实现图片切换通常涉及数据绑定、事件处理和动态样式。核心是通过维护一个当前图片索引的状态,利用Vue的响应式特性更新显示的图片。 数据准备与绑定 定义一个数组存储图片…