vue-awesome-swiper 露出下一张图片部分+自定义按钮滑动到上一个/下一个slide

配置(注意版本,这个版本比较陈旧了)

swiper几乎每一个版本的配置都不一样,而且这个vue-awesome-swiper已经废弃了。

我的配置同之前这篇文章:《解决vue-awesome-swiper 4.x + swiper 5.x 分页pagination配置不生效问题》

{
    "swiper": "^5.4.5",
    "vue-awesome-swiper": "^4.1.1"
}

需求1露出下一张边框

swiper露出下一张边框(在方向为horizontal的情况下)。

先是在官网上看到了slidesPerView配置项

https://www.swiper.com.cn/api/carousel/24.html

// vue
data() {
  return {
    swiperOPtion: {
      loop: true,
      spaceBetween: 10,
      slidesPerView: 'auto'
    }
  }
}

这么加上之后边框其实是是生效的,但是没有实现查看下一张图片的效果

参考官网案例

本来还想着自己写一个轮播图,但是在官网的案例中看到了 carousel 里的案例,发现是有这种情况的。
https://www.swiper.com.cn/demo/120-slides-per-view-auto.html
在这里插入图片描述

注意这个网址,直接右键查看源代码
在这里插入图片描述
关键点就在这里的 swiper-slide这里 ,设置了width: 80%,再搭配上前文的配置项slidesPerView: 'auto'即可实现。

总结解决方法

引入swiper之后
在这里插入图片描述

  • js配置
export default {
  data() {
    return {
      // swiper配置
        swiperOption: {
            loop: true, 
            speed: 1000, 
            // autoplay: true,
            autoplay: {
                delay: 3000, 
            },
            direction: 'horizontal', // 方向
            slidesPerView: 'auto', // 必须写
            spaceBetween: 10,
            // 分页配置
            pagination: {
                el: '.swiper-pagination',
                bulletClass: 'swiper-pagination-bullet',
                bulletActiveClass: 'my-bullet',
                clickable: true
            },
        },
        // swiper数据
        swipeData: [
            { url: require('@/assets/xxxxxxxxxxxxxx.png')},
            { url: require('@/assets/xxxxxxxxxxxxxx2.png')},
            { url: require('@/assets/xxxxxxxxxxxxxx3.png')}
        ],
    }
  }
}
  • css
.booking-swiper {
        width: 800px;
        height: 800px;
        .swipe-box {
            width: 100%;
            height: 800px;
            img {
                width: 100%;
                height: auto;
            }
            .swiper-slide{
                width: 80%;
            }
        }

        .swiper-pagination {
            width: 100%;
            height: setRem(100);
        }

        
    }

gpt手写

可以参考一下,如果时间来不及研究官网api、或者公司需求要求更精细的话

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>轮播图显示下一张边缘</title>
  <style>
    .carousel {
      width: 80%;
      margin: 50px auto;
      overflow: hidden;
      padding: 0 40px; /* 左右内边距让下一张图边缘露出 */
      position: relative;
    }

    .carousel-track {
      display: flex;
      transition: transform 0.5s ease;
    }

    .carousel-item {
      flex: 0 0 80%; /* 图片占轮播宽度的80%,剩下20%用于显示下一张边缘 */
      margin-right: 20px; /* 图片间距 */
      border-radius: 12px;
      overflow: hidden;
      box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    }

    .carousel-item img {
      width: 100%;
      height: auto;
      display: block;
    }

    .buttons {
      text-align: center;
      margin-top: 20px;
    }

    .buttons button {
      padding: 10px 20px;
      margin: 0 10px;
      border: none;
      background-color: #3498db;
      color: white;
      border-radius: 6px;
      cursor: pointer;
    }

    .buttons button:hover {
      background-color: #2980b9;
    }
  </style>
</head>
<body>

<div class="carousel">
  <div class="carousel-track">
    <div class="carousel-item"><img src="https://via.placeholder.com/600x300/FF5733/FFFFFF?text=1" alt="1"></div>
    <div class="carousel-item"><img src="https://via.placeholder.com/600x300/33FF57/FFFFFF?text=2" alt="2"></div>
    <div class="carousel-item"><img src="https://via.placeholder.com/600x300/3357FF/FFFFFF?text=3" alt="3"></div>
    <div class="carousel-item"><img src="https://via.placeholder.com/600x300/FF33A6/FFFFFF?text=4" alt="4"></div>
  </div>
</div>

<div class="buttons">
  <button onclick="moveSlide(-1)">上一张</button>
  <button onclick="moveSlide(1)">下一张</button>
</div>

<script>
  const track = document.querySelector('.carousel-track');
  const items = document.querySelectorAll('.carousel-item');
  const itemWidth = items[0].offsetWidth + 20; // 图片宽度 + margin-right
  let currentIndex = 0;

  function moveSlide(direction) {
    currentIndex += direction;
    if (currentIndex < 0) currentIndex = 0;
    if (currentIndex >= items.length - 1) currentIndex = items.length - 1;

    track.style.transform = `translateX(-${itemWidth * currentIndex}px)`;
  }
</script>

</body>
</html>

参考文章

官网demo:https://www.swiper.com.cn/demo/120-slides-per-view-auto.html

官网slidesPerView: https://www.swiper.com.cn/api/carousel/24.html

需求2 自定义跳转函数

我在官网找到 函数 slidePrev 和 slideNext 后,百度搜索,直接用ai的代码,成功。
在这里插入图片描述

  • 其实就是直接通过this.$refs操作方法
<swiper :options="swiperOption" ref="mySwiper">
   <swiper-slide v-for="(item, index) in list" :key="index">
     <img :src="item.img" />
   </swiper-slide>
 </swiper>
<button @click="swiperPrev">上一张</button>
<button @click="swiperNext">下一张</button>
methods: {
  swiperPrev() {
    this.$refs.mySwiper.$swiper.slidePrev();
  },
  swiperNext() {
    this.$refs.mySwiper.$swiper.swiperNext();
  }
}
### 实现 Vue.jsvue-awesome-swiper 的一页三列布局与无限循环 要在 Vue.js 使用 `vue-awesome-swiper` 插件实现 Swiper 一页显示三列布局并支持无限循环效果,可以通过配置 Swiper 参数来完成。以下是具体方法: #### 配置参数说明 为了实现一页三列布局以及开启无限循环功能,需要设置以下关键参数: - **slidesPerView**: 设置每页展示的滑动项目数量为 3[^1]。 - **spaceBetween**: 控制幻灯片之间的间距[^1]。 - **loop**: 开启无限滚动模式[^1]。 这些参数可以直接通过 `swiper-options` 属性传递给组件。 #### 安装依赖 如果尚未安装插件及其样式文件,则需先执行如下命令进行安装: ```bash npm install vue-awesome-swiper swiper --save ``` #### 组件代码示例 下面是一个完整的代码示例,展示了如何在 Vue.js 中使用 `vue-awesome-swiper` 来创建一个具有一页三列布局和无限循环特性的轮播图。 ```html <template> <div class="swiper-container"> <!-- 轮播容器 --> <swiper :options="swiperOption" ref="mySwiper"> <!-- 幻灯片内容 --> <swiper-slide v-for="(item, index) in items" :key="index">{{ item }}</swiper-slide> <!-- 如果需要分页器 --> <div class="swiper-pagination" slot="pagination"></div> <!-- 如果需要导航按钮 --> <div class="swiper-button-prev" slot="button-prev"></div> <div class="swiper-button-next" slot="button-next"></div> </swiper> </div> </template> <script> import { Swiper, SwiperSlide } from 'vue-awesome-swiper' import 'swiper/swiper-bundle.css' export default { components: { Swiper, SwiperSlide }, data() { return { items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'], // 数据源 swiperOption: { slidesPerView: 3, // 每页显示三个slide spaceBetween: 30, // slide之间间隔像素数 loop: true, // 启用无限循环模式 pagination: { // 分页器选项 el: '.swiper-pagination', clickable: true }, navigation: { // 导航箭头选项 nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' } } } } } </script> <style scoped> .swiper-container { width: 80%; margin: auto; } .swiper-slide { text-align: center; /* 可选 */ font-size: 18px; /* 可选 */ background-color: #f7f7f7; /* 可选 */ height: 150px; /* 自定义高度 */ display: flex; align-items: center; justify-content: center; } </style> ``` 以上代码实现了基本的一屏三列布局,并且开启了无限循环的功能。需要注意的是,实际应用中可以根据需求调整 `slidesPerView`, `spaceBetween` 和其他相关属性以适应不同的屏幕尺寸或设计风格。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值