diff --git a/src/modules/hash-navigation/hash-navigation.js b/src/modules/hash-navigation/hash-navigation.js index 8791daa98..e259f6f46 100644 --- a/src/modules/hash-navigation/hash-navigation.js +++ b/src/modules/hash-navigation/hash-navigation.js @@ -31,9 +31,10 @@ export default function HashNavigation({ swiper, extendParams, emit, on }) { const onHashChange = () => { emit('hashChange'); const newHash = document.location.hash.replace('#', ''); - const activeSlideEl = swiper.slidesEl.querySelector( - `[data-swiper-slide-index="${swiper.activeIndex}"]`, - ); + const activeSlideEl = + swiper.virtual && swiper.params.virtual.enabled + ? swiper.slidesEl.querySelector(`[data-swiper-slide-index="${swiper.activeIndex}"]`) + : swiper.slides[swiper.activeIndex]; const activeSlideHash = activeSlideEl ? activeSlideEl.getAttribute('data-hash') : ''; if (newHash !== activeSlideHash) { const newIndex = swiper.params.hashNavigation.getSlideIndex(swiper, newHash); @@ -43,9 +44,10 @@ export default function HashNavigation({ swiper, extendParams, emit, on }) { }; const setHash = () => { if (!initialized || !swiper.params.hashNavigation.enabled) return; - const activeSlideEl = swiper.slidesEl.querySelector( - `[data-swiper-slide-index="${swiper.activeIndex}"]`, - ); + const activeSlideEl = + swiper.virtual && swiper.params.virtual.enabled + ? swiper.slidesEl.querySelector(`[data-swiper-slide-index="${swiper.activeIndex}"]`) + : swiper.slides[swiper.activeIndex]; const activeSlideHash = activeSlideEl ? activeSlideEl.getAttribute('data-hash') || activeSlideEl.getAttribute('data-history') : '';