关于我在vue3中使用swiper的使用碰到swiper-slide的width特别大的这件事儿

一. 环境

  "vue": "^3.3.8",
  "swiper": "^10.0.4",

二. 问题描述

原代码:

<template>
	<swiper
		class="wq-swiper"
		:space-betwee="spaceBetween"
		:pagination="{ clickable: true }"
		:modules="modules"
		@swiper="onSwiper"
		@slide-change="onSlideChange"
	>
		<swiper-slide>
			<div class="mh2" style="font-size: 36px; height: 100px; background: #96ccff; color: blue">hello-world</div>
		</swiper-slide>
		<swiper-slide>
			<div class="mh2" style="font-size: 36px; height: 100px; background: #96ccff; color: blue">hello-卡开心</div>
		</swiper-slide>
	</swiper>
</template>
<script setup lang="ts">

import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper/modules';

import { WqSwiperItem } from '@/components/WqSwiper/type';
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';

type Props = {
	spaceBetween?: number;
	loop?: boolean;
	swiperItems: WqSwiperItem[];

	onSwiper?: (swiper: any) => void;
	onSlideChange?: (swiper: any) => void;
};

const props = withDefaults(defineProps<Props>(), {
	slidesPerView: 'auto',
	spaceBetween: 10,
	loop: false,
});

const modules = [Navigation, Pagination, Scrollbar, A11y];

</script>

在这里插入图片描述
发现这里的width非常大, 奇怪的是我没有设置任何的行内样式啊

三. 问题的解决

	<swiper
		class="wq-swiper"
		slides-pre-view="auto"
		:space-betwee="spaceBetween"
		:pagination="{ clickable: true }"
		:modules="modules"
		@swiper="onSwiper"
		@slide-change="onSlideChange"
	> ... </swiper>

在这里需要添加上slides-pre-view="auto" 或者 :slides-pre-view="n" n> = 2)
n = 1时需要为swiper 添加 width属性

四. 原因

swiper大小可变, 内部的slides的width是根据swiper的大小进行计算的,因为内部的slide自己在写样式的时候设置为100%在加上其他的一些样式会将swiper撑开,swiper大小变化后影响后面slide的width的计算.

所以在使用swiper的时候除了注意loopslides-pre-views`的一起使用还要注意swiper可变的情况,要么对slide大小进行控制,要么对swiper进行控制, 可以使用max-width属性对最大宽度进行限制

相关推荐

  1. vueswiper使用

    2024-07-18 09:32:01       46 阅读
  2. 初识微信小程序之swiperswiper-item基本使用

    2024-07-18 09:32:01       32 阅读
  3. Vuevue3使用swipe竖直方向上滚动

    2024-07-18 09:32:01       19 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 09:32:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 09:32:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 09:32:01       58 阅读
  4. Python语言-面向对象

    2024-07-18 09:32:01       69 阅读

热门阅读

  1. 详细说一下axios的特点

    2024-07-18 09:32:01       22 阅读
  2. log4j.appender.Logfile.File=./logs/its_log

    2024-07-18 09:32:01       20 阅读
  3. 七、python函数基础

    2024-07-18 09:32:01       20 阅读
  4. Junit单元测试常用断言

    2024-07-18 09:32:01       25 阅读
  5. app自动化测试缓存问题如何解决?

    2024-07-18 09:32:01       21 阅读
  6. 【Go系列】Go语言的测试

    2024-07-18 09:32:01       21 阅读
  7. OPPO 2024届校招正式批笔试题-后端(C卷)

    2024-07-18 09:32:01       23 阅读
  8. Caffeine缓存

    2024-07-18 09:32:01       22 阅读