Swiper clicking next or prev slide items, moves slider along - swiper.js

In my swiper settings, I have more than 1 slide in view. You can see the current and the previous and next slide.
I'm having trouble making it so that when you click either the next slide or the previous slide it moves the slide in the clicked direction. Currently, it's attached to the classes but this seems to be updating in the html but not in the JS.
const swiper = new Swiper('.swiper', {
// Optional parameters
direction: 'horizontal',
loop: true,
slidesPerView: 1.8,
centeredSlides: true,
spaceBetween: 10,
watchSlidesProgress: true,
// If we need pagination
pagination: {
el: '.swiper-pagination',
dynamicBullets: true,
},
// Navigation arrows
navigation: {
nextEl: '.swiper-slide-next',
prevEl: '.swiper-slide-prev',
},
// And if we need scrollbar
scrollbar: {
el: '.swiper-scrollbar',
},
});

Related

I have synced two swiper sliders but i want main slider to not scroll when thumb slider is been swiped with touch,

I have synced two swiper sliders but I want the main slider to not scroll when the thumb slider is swiped with touch but only change with the main slider when clicked on the thumbnail. I also don't want to compromise the free mode of the thumbnail. i am using swiper 8.4.5. i have this code
var galleryThumbs = new Swiper('.gallery-thumbs', {
loop: true,
loopedSlides: 10,
centeredSlides: true,
spaceBetween: 10,
slideToClickedSlide: true,
slidesPerView: 5,
watchSlidesVisibility: true,
watchSlidesProgress: true,
});
var galleryTop = new Swiper('.gallery-top', {
loop: true,
loopedSlides: 10,
spaceBetween: 10,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
galleryTop.controller.control = galleryThumbs;
// galleryThumbs.controller.control = galleryTop;
I have only synced the top slider the thumbnail but how can able click the thumbnail and change the top slider?
here the sample code. (updated example of code)
https://codepen.io/mossawir/pen/BaPxPRz
i got the solution my self. in case any one run into this case. all you need to do is. move the thumbnail code above the main slider code and add thumb: {}
here is the code
var galleryThumbs = new Swiper('.gallery-thumbs', {
loop: true,
loopedSlides: 10,
centeredSlides: true,
spaceBetween: 10,
slideToClickedSlide: true,
slidesPerView: 5,
watchSlidesVisibility: true,
watchSlidesProgress: true,
});
var galleryTop = new Swiper('.gallery-top', {
loop: true,
loopedSlides: 10,
spaceBetween: 10,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// USING THE THUMBS COMPONENT
thumbs: {
swiper: galleryThumbs
}
});
galleryTop.controller.control = galleryThumbs;
//galleryThumbs.controller.control = galleryTop;

Setting the correct height in vertical mode in Swiper

It's been days that I'm working on setting the swiper in my application but I still no luck and found no working sample anywhere from here to the Github issues or elsewhere on the net.
I can not set a dynamic height for the swiper in vertical mode via none of the suggested solutions to this problem.
I have several images as slides that give a height of thousands of pixels but if I switch it to a text it is fine as the height of the text is like 10 px. Any div above a certain height will result in abnormal height.
I found that the suggested CSS does not work as it just limits the height of the window and is very unpleasant as the pictures are cut off.
The js solutions are the correct way to do it but I can not find a working solution and the ones that I have tried all result in half of the job solutions.
It seems the correct way uses the onSlideChangeStart to correct the next slide height like below but this does not work as the swiper of the vertical is not recognized in its own settings.
onSlideChangeStart: (swiper)->
$(swiper.slides).each ->
$(this).css height: if $(this).index() != swiper.activeIndex then 0 else 'auto'
I tried to use the active class instead of the API as it is not working when calling even the swiperV.activeIndex in its own onSlideChangeStart like below but swiper-slide-active is not defined.
This is my settings for the 3 nested sliders.
var swiperVV = new Swiper('.swiper-container-vv', {
direction: 'vertical',
updateOnWindowResize: true,
grabCursor: true,
loop: true,
mousewheel: true,
lazy: true,
zoom: true,
effect: 'slide',
spaceBetween: 0,
pagination: {
el: '.swiper-pagination-v',
clickable: true,
},
});
var swiperH = new Swiper('.swiper-container-h', {
initialSlide:0,
spaceBetween: 0,
autoHeight: true,
updateOnWindowResize: true,
grabCursor: true,
loop: true,
parallax: true,
lazy: true,
effect: 'slide',
pagination: {
el: '.swiper-pagination-h',
clickable: true,
},
scrollbar: {
el: '.swiper-scrollbar',
hide: true,
},
});
var swiperV = new Swiper('.swiper-container-v', {
direction: 'vertical',
autoplay: {
delay: 5000,
},
autoHeight: true,
updateOnWindowResize: true,
grabCursor: true,
loop: true,
mousewheel: true,
lazy: true,
zoom: true,
effect: 'slide',
spaceBetween: 0,
pagination: {
el: '.swiper-pagination-v',
clickable: true,
},
slideChange: resizeSwiper()
});
function resizeSwiper() {
console.log($('.swiper-container-v .swiper-slide swiper-slide-active').height());
$('.swiper-container-v, .swiper-container-v .swiper-slide').css({ height: $('.swiper-container-v .swiper-slide .swiper-slide-active').height() });
}
i have gone through all the samples of suggested users like the ones listed below in github, stackoverflow and codepen.
https://github.com/nolimits4web/swiper/issues/48
https://github.com/nolimits4web/Swiper/issues/153
This is the workaround worked for me
if (typeof Swiper != "undefined"){
var popularSwiper = new Swiper(".popularSwiper", {
direction: "vertical",
autoHeight: true,
autoplay: {
delay: 5000,
},
pagination: {
el: ".swiper-pagination",
clickable: true,
},
on:{
init:function(){
setSlideHeight(this);
},
slideChangeTransitionEnd:function(){
setSlideHeight(this);
}
}
});
function setSlideHeight(that){
$('.swiper-slide').css({height:'auto'});
var currentSlide = that.activeIndex;
var newHeight = $(that.slides[currentSlide]).height();
$('.swiper-wrapper,.swiper-slide').css({ height : newHeight })
that.update();
}
}
It's working for me.
calculate the total height of the swiper container including gap between slides.
then set the swiper container height to the previously calculated height
let pdpThumb = new Swiper('#pdp-thumbCarousel', {
slidesPerView: 4,
direction: 'vertical',
spaceBetween: 10,
freeMode: true,
loop: true,
on: {
init: (swiper) => {
let totalGap = swiper.passedParams.spaceBetween * (swiper.passedParams.slidesPerView - 1);
let containerHeight = swiper.passedParams.slidesPerView * swiper.slides[0].clientHeight + totalGap;
swiper.el.style.height = containerHeight + 'px';
},
},
});

How to fix Slick Slider (Slick.JS) if it breaks on click event within the div?

I added Slick slider for any device under 1024px, however every time a button within the div is clicked and the page is refreshed, the slider is gone and it leaves a mess. It fixes itself if I change the device width from the console. I tried adding a function to reinitialize it every time a button is clicked, but for some reason 'reInit' does not work. I can't provide Jsfiddle, if someone could help based on my description, I would appreciate it.
function slickify(){
event.preventDefault();
$('.cart-list').not('.slick-initialized').slick({
infinite: false,
responsive: [
{
breakpoint: 1024,
autoplay: false,
dots: true,
infinite: false,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
dots: true
}
},
{
breakpoint: 786,
autoplay: false,
dots: true,
infinite: false,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
dots: true
}
},
{
breakpoint: 550,
autoplay: false,
dots: true,
infinite: false,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
dots: true
}
}
]
});
}
$(window).resize(function(){
event.preventDefault();
var $windowWidth = $(window).width();
if ($windowWidth < 1024) {
slickify();
$(window).resize(function() {
$('.cart-list').slick('resize');
event.preventDefault();
});
$(window).on('orientationchange', function() {
$('.cart-list').slick('resize');
event.preventDefault();
});
}
});
$('.cart-list').on('click', function() {
$('.cart-list').slick('reInit');
});
});
I've tried adding a simple function such as :
('body').on('click', function() {
('.slick-slider-div').slick('reInit');
});
where reInit is reinitialization as per Slick.JS documentation. However, it breaks any click event on the page. I also tried to make the click event only withing the div, still no result. The '.slick-initialized' class just disappears on click event.

Destroy flexslider on mobiles? (with enquire.js)

I'm using http://wicky.nillia.ms/enquire.js for hiding the flexslider on mobiles however the destroy method doesn't seem to work while I resize my browser.
/* hide/show slideshow based on viewport size*/
enquire.register("screen and (min-width: 48em)", {
deferSetup : true,
setup : function() {
/*home test slideshow slideshow*/
$(window).load(function() {
jQuery('#home-slider').flexslider({
slideshow: false,
directionNav: true,
controlNav: false,
animation: "fade",
useCSS: true,
});
});
},
match : function() {
/* add slider specific class */
jQuery("#home-slider").addClass("flexslider");
jQuery("#home-slider ul.slides-init").addClass("slides");
},
unmatch : function() {
/* hide slider specific class */
jQuery("#home-slider").removeClass("flexslider");
jQuery("#home-slider ul.slides-init").removeClass("slides");
},
destroy : function() {
jQuery("#home-slider").flexslider('destroy');
}
});
Adding/removing classes in match/unmatch functions work ok.
Any insights?

sencha-touch using ActionSheet

I am trying to use an ActionSheet to present the user several options to continue. I want to add a simple text box in the middle of the screen to describe the problem to the user.
Is there a simple/standard way to put such a prompt in Sencha-Touch?
You'll need to do something like this on your overlay ...
if (!this.popup) {
this.popup = new Ext.Panel({
hideOnMaskTap: false,
floating: true,
modal: true,
centered: true,
hideOnMaskTap: false,
width: 300,
height: 200,
styleHtmlContent: true,
scroll: 'vertical',
html: '<p>msg here</p>'
});
}
this.popup.show('pop');
The key here being hideOnMaskTap: false, this will prevent the overlay from disappearing when you click in it's masked area.
You can then display your action sheet but you must remember to hide the overlay in the handler(s) for your action sheet buttons since it will no longer go away when you tap in the masked area.
if (!this.actions) {
this.actions = new Ext.ActionSheet({
items: [{
text: 'decline',
ui: 'decline',
handler: function() {
this.doWhateverYouNeedToDo();
this.actions.hide();
this.popup.hide();
}
}, {
text: 'save',
handler: function() {
this.doWhateverYouNeedToDo();
this.actions.hide();
this.popup.hide();
}
}, {
text: 'cancel',
scope: this,
handler: function() {
this.doWhateverYouNeedToDo();
this.actions.hide();
this.popup.hide();
}
}]
});
}
this.actions.show();
note the hiding of the overlay in the sheet handlers.. (i don't do it above but you'll more than likely want to destroy the overlay and the action sheet after they are hidden as well just to keep things tidey)
You can use an Overlay popup to display a message after the ActionSheet renders.