Currently, I am attempting to dynamically adjust the eventsTarget within the mousewheel object in Swiper based on changes to a data property using Vue.js. Specifically, when disableBodyScroll is set to true, the carousel should scroll via eventsTarget:html; otherwise, it should utilize the default swiper container:
data() {
return {
disableBodyScroll: false,
}
},
async mounted() {
await this.$nextTick()
this.initSwiper()
},
methods: {
initSwiper() {
const vm = this
if (this.$refs?.carousel) {
this.carousel = new SwiperCore(this.$refs.carousel, {
mousewheel: {
eventsTarget: this.disableBodyScroll ? 'html' : 'container',
releaseOnEdges: true,
thresholdDelta: 1,
},
freeMode: this.mousewheelScroll,
freeModeSticky: this.mousewheelScroll,
on: {
reachEnd: (swiper) => {
vm.disableBodyScroll = false
swiper.params.mousewheel.eventsTarget = 'container'
swiper.update()
},
reachBeginning: (swiper) => {
vm.disableBodyScroll = false
swiper.params.mousewheel.eventsTarget = 'container'
swiper.update()
},
},
})
}
},
},
I have tried various approaches to change the value of the disableBodyScroll data property, but unfortunately, none of them have been successful. Despite my efforts, the behavior remains unchanged. One workaround I have implemented involves utilizing a watcher that destroys and reinitializes the carousel upon detecting a change in the disableBodyScroll property.
watch: {
disableBodyScroll(newVal) {
if (this.carousel) {
this.carousel.destroy(true, false);
this.initSwiper();
}
},
However, this method resets the Swiper carousel to its initial state and does not provide an ideal user experience. Additionally, explicitly setting the eventsTarget and invoking update() when reaching the end or beginning of the slides has also proven ineffective.
Do you have any insights on a potential solution for achieving this dynamic behavior?