Skip to content

Commit

Permalink
fix(hash-navigation): get active slide element using the old method i…
Browse files Browse the repository at this point in the history
…f Virtual is not being… (#6704)

* get active slide element using the old method if Virtual is not being used

* Fix linting issues

* Improve solution
  • Loading branch information
jur-ng authored May 25, 2023
1 parent a8fc1df commit db7e72f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/modules/hash-navigation/hash-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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')
: '';
Expand Down

0 comments on commit db7e72f

Please sign in to comment.