Skip to content

Commit

Permalink
Merge pull request #137 from IBEC-BOX/fix/slider
Browse files Browse the repository at this point in the history
fix variable undefined in slider when page reload[2]
  • Loading branch information
Zh3nyaZh3nya authored Nov 7, 2024
2 parents 8212811 + b1662ad commit cc7f0f5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ibecsystems/nuxt3-ui-kit",
"version": "1.0.51",
"version": "1.0.52",
"description": "Ibec Nuxt3 UI components",
"repository": {
"type": "git",
Expand Down
97 changes: 41 additions & 56 deletions src/runtime/components/Parts/Slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -432,25 +432,24 @@ const props = defineProps({
});
const slider = ref(null);
const activeSlide = ref(0);
const pagination = ref(props.slides);
const pagination = ref(props.slides || []);
const slideTo = (index) => {
if (slider.value !== null) {
slider.value.swiper.slideTo(index);
}
};
const slidePrev = () => {
if (slider.value && slider.value.swiper) {
slider.value.swiper.slidePrev(500);
const slideNext = () => {
if (slider.value?.swiper) {
slider.value.swiper.slideNext(500);
}
};
const slideNext = () => {
if (slider.value && slider.value.swiper) {
slider.value.swiper.slideNext(500);
const slidePrev = () => {
if (slider.value?.swiper) {
slider.value.swiper.slidePrev(500);
}
};
Expand All @@ -465,70 +464,56 @@ const updatePagination = () => {
};
const handleWheel = (event) => {
if (!slider.value || !props.controlScroll) return;
const isAtStart = slider.value.swiper.isBeginning;
const isAtEnd = slider.value.swiper.isEnd;
if (!slider.value?.swiper || !props.controlScroll) return;
if ((!isAtStart && event.deltaY < 0) || (!isAtEnd && event.deltaY > 0)) {
const swiperInstance = slider.value.swiper;
if ((!swiperInstance.isBeginning && event.deltaY < 0) || (!swiperInstance.isEnd && event.deltaY > 0)) {
event.preventDefault();
if (event.deltaY > 0) {
slider.value.swiper.slideNext();
} else {
slider.value.swiper.slidePrev();
}
event.deltaY > 0 ? swiperInstance.slideNext(500) : swiperInstance.slidePrev(500);
}
};
let lastScrollPosition = 0;
if(typeof window !== 'undefined') {
lastScrollPosition = window.pageYOffset
}
const handleScroll = () => {
if (!slider.value || typeof window === 'undefined' || !props.controlScroll) return;
if (!slider.value?.swiper || !props.controlScroll) return;
const currentScrollPosition = window.pageYOffset;
const direction = currentScrollPosition > lastScrollPosition ? 'down' : 'up';
const direction = window.pageYOffset > lastScrollPosition ? 'down' : 'up';
direction === 'down' ? slider.value.swiper.slideNext() : slider.value.swiper.slidePrev();
lastScrollPosition = window.pageYOffset;
};
if (direction === 'down') {
slider.value.swiper.slideNext();
const manageEventListeners = (addListeners) => {
if (addListeners) {
window.addEventListener('wheel', handleWheel, { passive: false });
window.addEventListener('scroll', handleScroll, { passive: true });
} else {
slider.value.swiper.slidePrev();
window.removeEventListener('wheel', handleWheel);
window.removeEventListener('scroll', handleScroll);
}
lastScrollPosition = currentScrollPosition;
};
watch(() => props.controlScroll, (newValue) => {
if(props.controlScroll) {
if (newValue) {
window.addEventListener('wheel', handleWheel, { passive: false });
window.addEventListener('scroll', handleScroll, { passive: true });
} else {
window.removeEventListener('wheel', handleWheel);
window.removeEventListener('scroll', handleScroll);
}
}
}, { immediate: true });
const zoomStyle = computed(() => ({
transform: activeSlide.value === 1 ? `scale(${props.sliderBackgroundImageZoomScale})` : 'scale(1)',
transition: 'transform 0.3s ease-in-out',
}));
onMounted(() => {
if(!slider.value) return
activeSlide.value = slider?.value?.swiper?.realIndex;
if (pagination.value.length > 0) {
pagination.value[0].active = true;
let lastScrollPosition = window.pageYOffset;
watch(
() => props.controlScroll,
(newValue) => manageEventListeners(newValue),
{ immediate: true }
);
watch(() => props.slides, (newSlides) => {
if (slider.value?.swiper) {
slider.value.swiper.update();
}
});
onMounted(async () => {
activeSlide.value = slider.value?.swiper?.realIndex;
if (pagination.value.length > 0) pagination.value[0].active = true;
});
onUnmounted(() => {
if (props.controlScroll) {
window.removeEventListener('wheel', handleScroll);
manageEventListeners(false);
if (slider.value?.swiper && !slider.value.swiper.destroyed) {
slider.value.swiper.destroy();
}
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.use(vuetifyInstance);
nuxtApp.vueApp.directive("maska", vMaska);

if (!process.server) console.log('❤️ Initialized Vuetify 3', vuetifyInstance);
if (!process.server && process.env.NODE_ENV !== 'production') console.log('❤️ Initialized Vuetify 3', vuetifyInstance);
});

0 comments on commit cc7f0f5

Please sign in to comment.