首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的反应-滑动组件滑动按钮点击?

为什么我的反应-滑动组件滑动按钮点击?
EN

Stack Overflow用户
提问于 2021-06-18 09:41:46
回答 1查看 675关注 0票数 1

我在另一个FlashDealsProducts.js文件中重用了FlashDealsProducts.js刷代码,但没有任何错误,但当我单击幻灯片按钮时,这两个组件都朝着相同的方向滑动。我试着改变身份,但没起作用。已删除两个文件的虚拟数据

升级-Slider.js文件

代码语言:javascript
复制
import { ArrowNext, ArrowPrev } from "@components/icons";
import SwiperCore, { Navigation } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/swiper-bundle.css";
     

const offerSliderBreakpoints = {
    320: {
        slidesPerView: 1,
        spaceBetween: 0,
    },
    580: {
        slidesPerView: 2,
        spaceBetween: 16,
    },
    1024: {
        slidesPerView: 3,
        spaceBetween: 16,
    },
    1920: {
        slidesPerView: 4,
        spaceBetween: 24,
    },
};

SwiperCore.use([Navigation]);

export default function PromotionSlider() {
    return (
        <div className="px-6 py-5 md:px-8 xl:px-12 md:py-10 border-t border-gray-200">
            <div className="relative">
                <Swiper
                    // id="offer"
                    loop={true}
                    breakpoints={offerSliderBreakpoints}
                    navigation={{
                        nextEl: ".next",
                        prevEl: ".prev",
                    }}
                >
                    {data?.map((d) => (
                        <SwiperSlide key={d.id}>
                            <img
                                className="w-full h-auto"
                                src={d.bannerUrl}
                                alt={d.title}
                                width="430"
                                height="200"
                            />
                        </SwiperSlide>
                    ))}
                </Swiper>
                <div 
                    className="prev cursor-pointer absolute top-2/4 -left-4 md:-left-5 z-10 -mt-4 md:-mt-5 w-8 h-8 md:w-9 md:h-9 rounded-full bg-white shadow-xl border border-gray-200 border-opacity-70 flex items-center justify-center text-gray-800 transition-all duration-200 hover:bg-primary hover:text-white hover:border-primary" 
                    role="button"
                >
                    <span className="sr-only">previous</span>
                    <ArrowPrev width={18} height={18} />
                </div>
                <div
                    className="next cursor-pointer absolute top-2/4 -right-4 md:-right-5 z-10 -mt-4 md:-mt-5 w-8 h-8 md:w-9 md:h-9 rounded-full bg-white shadow-xl border border-gray-200 border-opacity-70 flex items-center justify-center text-gray-800 transition-all duration-200 hover:bg-primary hover:text-white hover:border-primary"
                    role="button"
                >
                    <span className="sr-only">next</span>
                    <ArrowNext width={18} height={18} />
                </div>
            </div>
        </div>
    );
}

FlashDealsProducts.js文件

代码语言:javascript
复制
import Products from './Products.js';
import ProductCard from './ProductCard.js';
import { ArrowNext, ArrowPrev } from "@components/icons";
import SwiperCore, { Navigation } from "swiper";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/swiper-bundle.css";

const SliderBreakpoints = {
    320: {
        slidesPerView: 1,
        spaceBetween: 0,
    },
    580: {
        slidesPerView: 2,
        spaceBetween: 16,
    },
    1024: {
        slidesPerView: 3,
        spaceBetween: 16,
    },
    1920: {
        slidesPerView: 4,
        spaceBetween: 24,
    },
};

SwiperCore.use([Navigation]);

export default function FlashDealProducts() {
    return (
        <div className='flex mt-14 flex-col'>
            <div className='flex pl-1 flex-nowrap  bg-white p-4'>
                <h2 className=' flex-nowrap ml-7 font-bold text-lg'> Flash Deals </h2>
                <span className='text-sm  ml-20 font-light text-gray-800 '> Ends in </span>
                <span className='px-1 ml-4 bg-red-700 text-white font-medium'> 10 hours</span>
            </div>
            <div className="px-6 py-5 md:px-8 xl:px-12 md:py-10 border-t bg-white border-gray-200">
                <div className="relative">
                    <Swiper
                        id="flash-deals"
                        loop={true}
                        breakpoints={SliderBreakpoints}
                        navigation={{
                            nextEl: ".next",
                            prevEl: ".prev",
                        }}
                    >
                        {Products?.map((d) => (
                            <SwiperSlide key={d.id}>
                                <ProductCard key={d.id}
                                    title={d.title}
                                    image={d.image}
                                    basePrice={d.basePrice}
                                    flashHeight={200}
                                    flashWidth={200}
                                    discount={d.discount}
                                    currentPrice={d.currentPrice} />
                            </SwiperSlide>
                        ))}
                    </Swiper>
                    <div 
                        className="prev cursor-pointer absolute top-2/4 -left-4 md:-left-5 z-10 -mt-4 md:-mt-5 w-8 h-8 md:w-9 md:h-9 rounded-full bg-white shadow-xl border border-gray-200 border-opacity-70 flex items-center justify-center text-gray-800 transition-all duration-200 hover:bg-primary hover:text-white hover:border-primary"
                        role="button"
                    >
                        <span className="sr-only">previous</span>
                        <ArrowPrev width={18} height={18} />
                    </div>
                    <div 
                        className="next cursor-pointer absolute top-2/4 -right-4 md:-right-5 z-10 -mt-4 md:-mt-5 w-8 h-8 md:w-9 md:h-9 rounded-full bg-white shadow-xl border border-gray-200 border-opacity-70 flex items-center justify-center text-gray-800 transition-all duration-200 hover:bg-primary hover:text-white hover:border-primary"
                        role="button"
                    >
                        <span className="sr-only">next</span>
                        <ArrowNext width={18} height={18} />
                    </div>
                </div>
            </div>
            {/* </div> */}
        </div>
    )
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-23 10:44:21

检查,两个文件中导航对象的两个值都是相同的。为在项目中使用的每个滑动组件为prevEl和nextEl属性分配不同的值。

代码语言:javascript
复制
navigation={{        
                nextEl: ".next", // both should be unique in every swiper 
                                 // component
                prevEl: ".prev",
            }}

Either you change both values or one of them in your file only if you 

在项目中使用两个swiper组件,否则必须为每个swiper组件中的导航属性分配不同的值。确保在下面的类中用新值替换它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68032875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档