From 9e0d525efe81282cfff3f26fdfbc12f3aee5f40a Mon Sep 17 00:00:00 2001 From: capdiem Date: Thu, 15 Aug 2024 09:37:53 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(Swiper):=20Initial=20index?= =?UTF-8?q?=20doesn't=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Swiper/MSwiper.razor.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Masa.Blazor/Components/Swiper/MSwiper.razor.cs b/src/Masa.Blazor/Components/Swiper/MSwiper.razor.cs index 6eb4b7622f..2fd9b26b3f 100644 --- a/src/Masa.Blazor/Components/Swiper/MSwiper.razor.cs +++ b/src/Masa.Blazor/Components/Swiper/MSwiper.razor.cs @@ -81,18 +81,7 @@ protected override IEnumerable BuildComponentStyle() protected override async Task OnParametersSetAsync() { await base.OnParametersSetAsync(); - - if (_prevIndex != Index) - { - _prevIndex = Index; - - if (_swiperProxy is null) - { - return; - } - - await _swiperProxy.SlideToAsync(Index, Speed); - } + await SliderToIndexAsync(Speed); } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -154,9 +143,23 @@ await RunTaskInMicrosecondsAsync(async () => }; _swiperProxy = await SwiperJsModule.Init(Ref, options, _swiperInteropHandle); + + await SliderToIndexAsync(0); }, 16, _ctsForInit.Token); } + private async Task SliderToIndexAsync(int speed) + { + if (_swiperProxy is null || _prevIndex == Index) + { + return; + } + + _prevIndex = Index; + + await _swiperProxy.SlideToAsync(Index, speed); + } + internal async Task UpdateIndexAsync(int index) { Index = index;