Comments (7)
Hi, sorry but i don't undertand what you are asking for and what you want to achieve?)) Can you please explain more detailed so i can help you?
Also what do you mean with "transform coordinates are left (0, 0)"?
from swiper.
I have an application with several slides where each slide has its own unique ID.
I want to be able to change slides by changing the URL (e.g. index.html#slide1, index.html#slide2, etc.)
If I open my application, not from the first slide (e.g. index.html#slide3), I want to see the relevant slide (in this case, slide3), but still be able to swipe back and forth between all slides.
Currently, while the first part is OK (the relevant slide is shown), the second part isn't - meaning that I can't swipe back (slide2).
I've noticed that my swiper-wrapper element has the following style: -webkit-transform: translate(0px, 0px) and I think this is the reason why I can't swipe back (to slide2) - since the x-coordinate is considered 0 while it should have been (-4605px, 0) (each slide is 1535 wide)
If I'm doing something wrong, I would love to know what. Otherwise, I would love to know how to bypass this issue.
Thanks,
Ofir
from swiper.
Closed the issue by mistake ...
from swiper.
That is really crazy, see this problem too)) So the point is when you use hash equal to some elements ID then as you know browsers will scroll page to this element. The same in your case, browser just scrolls swiper-container to hidden slide, this is not by swipeTo function. So you need to use not direct hash, somethink like "#!/slide1" will work here. Here is working code:
function moveSwiper () { var id = window.location.hash.split('#!/')[1]; var index = $(".swiper-slide").index($("#" + id)); if (index != swiper.activeSlide) { swiper.swipeTo(index, 500, false); } } //On hashchange. This event not fired when page just loaded $(window).bind('hashchange', function(event) { moveSwiper () }); //Main Swiper swiper = new Swiper('.swiper1', { pagination : '.pagination1', onSlideChangeEnd : function () { history.pushState(null, null, "#!/" + $(".swiper-slide").get(swiper.activeSlide).id) } }); //On Page Load: moveSwiper ()
Also use "history", not "History"
from swiper.
Hi Vladimir,
You are right, the code isn't executed unless you use the back button (meaning that the entire code is irrelevant).
However, I was not able to make it work with a non-direct hash. Is there any way to make it work with a normal hash?
BTW: I'm using a history plugin for jquery (hence the "History" instead of "history")
from swiper.
My code works fine for me. About to make it work with normal hash, not sure, but try to add event.preventDefault() inside of haschange handler
from swiper.
The event.preventDefault did not solve the issue. However, I have found that the problem is in the following code (in init function #411):
if(params.initialSlide > 0 && params.initialSlide < numOfSlides) {
_this.realIndex = _this.activeSlide = params.initialSlide;
if (_this.params.loop) {
_this.activeSlide = _this.realIndex-params.slidesPerSlide
}
if (isHorizontal) {
_this.positions.current = -params.initialSlide * slideWidth;
_this.setTransform( _this.positions.current, 0, 0);
}
else {
_this.positions.current = -params.initialSlide * slideHeight;
_this.setTransform( 0, _this.positions.current, 0);
}
}
There are 2 problems here:
- the setTransform is called only if swiper got the initialSlide parameter (although the initialSlide could be set by the hash)
- the setTransform is using only the initialSlide parameter in order to set the initial slide (although, again, this could be set by the hash)
By not calling the setTransform, the initial x-coordinate is presumed to be 0 although it might not be.
Bottom line, I managed to get around the issue by supplying the initialSlide parameter (not sure it's a bug though ... maybe a known-issue for folks working with the hash):
var hash = window.location.hash.slice(1);
var index = $(".page").index($("#" + hash));
swiper = new Swiper('.pages.swiper-container', {
initialSlide: index,
speed: 250,
freeMode: false,
onSlideChangeEnd: function() {
History.pushState(null, null, "#" + $(".page").get(swiper.activeSlide).id);
}
});
Thanks,
Ofir
from swiper.
Related Issues (20)
- Thumbs Module not working on Strict Mode
- Can't swipe when swiper slides are clickable elements HOT 1
- react__WEBPACK_IMPORTED_MODULE_0__.createContext is not a function in Next.js 14.2.3 HOT 4
- Direction vertical, web components and it's fix HOT 1
- Ability to disable image lazy loading listeners HOT 6
- When using the horizontal mode of the scroll container, there are problems that are inconsistent with expectations. HOT 1
- swiper.slideTo(5) Jumps to Penultimate Image Instead of Last Image HOT 4
- Weird behavior of Swiper.js in Next.js 13 & 14 HOT 1
- slideTo animation gets cancelled when resizing browser window HOT 1
- Swiper appendSlide method doesn't work properly when virtual slides are enabled. (web-component) HOT 2
- Change the slide with the URL search query like hash-navigation
- Loop mode leaves empty space after the last slide HOT 5
- I can't swipe on a swiper slide with button. HOT 2
- Swiper incorrectly warns about a wrong number of slides for loop mode HOT 4
- swiper container flex issues
- Swiper Dynamic bullets pagination width issue HOT 6
- Swiper slider issue when autoplay and loop true.
- In the Vue3 project, in loop mode, slideChange will be triggered twice
- Updating `slidesPerView` and `breakpoints.*.slidesPerView` does not correctly refresh the slider HOT 1
- `observer: true` not working for custom element HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiper.