Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Swiper collapses on route change #317

Closed
nataliawww opened this issue Apr 17, 2018 · 22 comments
Closed

Swiper collapses on route change #317

nataliawww opened this issue Apr 17, 2018 · 22 comments

Comments

@nataliawww
Copy link

Using swiper version 3.1.3 with vue & nuxt

When rendering a new route, you can see the swiper collapse, I guess because it's being destroyed on beforeDestroy?

Here's a video showing the issue
https://streamable.com/mthnx

@aligajani
Copy link

Interesting

@bgondy
Copy link

bgondy commented Apr 24, 2018

I have a similar issue with <transition> component in a parent component. Removing beforeDestroy seems to do the trick

@hoektoe
Copy link

hoektoe commented Jun 13, 2018

Is this the same issue as above? How to resolve this?
Check the bottom image which is also in slider become as big as whole container

https://streamable.com/fp3fa

@bgondy could you elaborate what you did to resolve this issue? or is it something which needs to be fixed within the swiper codebase?

@rubenmeines
Copy link

rubenmeines commented Jun 20, 2018

I am having the same issue. I have links inside the swiper, so it will start routing when I click on a swiper-item. Then before my page transition, the swiper resets and goes back to the start.

I am using nuxtjs.

@rubenmeines
Copy link

@nataliawojtkowska
I think you can fix that issue by not using the "spaceBetween" option but use margins in your css. If not you can also try the option below which will not destroy your slider and keep the settings.

@hoektoe @bgondy
I fixed it by using the mySwiper.destroy(deleteInstance, cleanStyles); and setting the "deleteInstance" and "cleanStyles" to false on mount and then initialising it again.

HTML:
screen shot 2018-06-24 at 19 46 51

JS:
screen shot 2018-06-24 at 19 45 45

Check also http://idangero.us/swiper/api/
and search for: mySwiper.destroy(deleteInstance, cleanStyles);

@rubenmeines
Copy link

rubenmeines commented Jun 24, 2018

If you want to keep your slide on the same position as when you leave the page, you can check out:
mySwiper.progress and mySwiper.update();

You could use mySwiper.progress to save the progress in your store before you leave the page and then when you come back on the page where your swiper is you can use mySwiper.updateProgress() to update the position in the mounted() function

@bitpunk
Copy link

bitpunk commented Jul 19, 2018

I have the same issue and tried to fix it with the destory/init solution mentioned before.
I seems to work but the navigation stops working. The navigation classes in the props are just fine.

@guins
Copy link

guins commented Jul 23, 2018

I created a pull request to fix this issue. Let me know if it works for you !

@hjujah
Copy link

hjujah commented Oct 4, 2018

Having the same issue on nuxtjs... Any updates on this?
Btw, I solved it by adjusting the swiper.js file, but still looking for a batter / more proof solution...

@lockieRichter
Copy link

@guins I've implemented your fix and it works for me, the pull request has failed checks because the CI job failed.

@nonlinearcom
Copy link

I had the same problem using page transitions on nuxt.
My temporary fix was commenting swiper.destroy && swiper.destroy(); on dist/ssr.js

@or2e
Copy link

or2e commented Dec 23, 2018

.swiper-container{ opacity: 0 }

mounted() { this.$nextTick(() => { this.swiper.el.style.opacity = 1 }) }

@ilero
Copy link

ilero commented Apr 3, 2019

I had the same problem when using SPA.

i used this: vue-awesome-swiper

i tried add an event like this:

option: {
on: {
beforeDestroy: function(){
this.params.scrollbar.el = "";
}
}
}
Problem may be solved, but I don't know if there are any other bugs.

@leopoldkristjansson
Copy link

Is this the same issue as above? How to resolve this?
Check the bottom image which is also in slider become as big as whole container

https://streamable.com/fp3fa

@bgondy could you elaborate what you did to resolve this issue? or is it something which needs to be fixed within the swiper codebase?

Having the same issue as this, using Nuxt.

@kyalha
Copy link

kyalha commented May 16, 2019

@leopoldkristjansson I have the same issue and the PR has not been merged so in the meantime, you can use my colleague' s dirty solution by adding to the Swiper option to overwrite the destroy function:

init: (function() { // @ts-ignore const prev = this.destroy if (prev != null) { this.destroy = function() { prev.apply(this, [true, false]) } } })

@travbus
Copy link

travbus commented May 27, 2019

I find keep alive works good for keeping presentation between changes routes. If you populate slides when the component is created. However if you populate when the component is activated. So updated everytime you go back to that route.. issues happen like first slide is visible but the rest of the slides are not but are still in the dom.

@arcs-
Copy link

arcs- commented Aug 20, 2019

The temporary fix from nonlinearcom seems to work for me too, however, instead of commenting out the part I just delayed it, so the swiper gets destroyed but only after the transition

in /node_modules/vue-awesome-swiper/dist/ssr.js => unbind()

setTimeout(function() {
  swiper.destroy && swiper.destroy();
  delete vnode.context[instanceName];
}, 1000);

@abeleev
Copy link

abeleev commented Dec 3, 2019

Solution for me

swiper.fixedDestroy = swiper.destroy;
swiper.destroy = () => swiper.fixedDestroy(true, false);

@jqweryjson
Copy link

jqweryjson commented Mar 2, 2020

mounted() {
const swiperContext = _.get(this.$refs, 'slider.swiper'); //lodash get ref

  if (swiperContext) swiperContext.destroy = swiperContext.destroy.bind(swiperContext, true, false);

},

In my case i use bind - because context was lost

@sebastianjung
Copy link

sebastianjung commented Mar 4, 2020

This seems to work for me as it overrides the default destroy function:

swiper.destroy = () => {}
or
this.swiper.destroy = () => {}
depending on your implementation

@leopoldkristjansson
Copy link

Using the solution from @sebastianjung works for me:

  mounted() {
    this.mySwiper.destroy = () => {};
  }

<div v-swiper:mySwiper="swiperOptions">...</div>

@surmon-china
Copy link
Owner

Silence v4.x, you can control the destory params by props.

<swiper
  :options="swiperOptionsObject"
  :auto-update="true"
  :auto-destroy="true"
  :delete-instance-on-destroy="true"
  :cleanup-styles-on-destroy="true"
  @ready="handleSwiperReadied"
  @click-slide="handleClickSlide"
/>

Upgrade to v4.x please!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests