why swiper progress event doesn't reflect slide movement? - javascript

Is there a way in swiperjs to know the real event of slide progress?
I set my slide speed to six seconds (very slow).
And I try to listen to progress event. but when I click on the arrow, it fire only once. and when I swipe using touch it fire correct but when I release the touch, the slide is still moving but the progress doesn't fire.
So, the progress bar doesn't work as expected.
I looking for event that tell me "the slide is moving". I try all the events in the docs. but none of them works. maybe another techniuqe?
var swiperOptions = {
speed: 5000,
grabCursor: true,
watchSlidesProgress: true,
mousewheelControl: true,
keyboardControl: true,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
},
on: {
progress: function(data) {
console.log('event progress', data);
}
}
};
var swiper = new Swiper(".swiper-container", swiperOptions);
body {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
background: #404449;
margin: 0;
padding: 0;
}
.swiper-container {
height: calc(100vh - 120px);
margin: 60px;
}
.swiper-slide {
overflow: hidden;
}
.slide-inner {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
background-size: cover;
background-position: center;
}
a {
color: white;
}
.wrap-footer { position:absolute;bottom:0;height:100px;background:#000;width:100%;overflow:hidden; }
.footer { height:100px;/*transform:translateY(-100px);*/}
.red { background:red; }
.blue { background: blue;}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/css/swiper.css" integrity="sha256-eN7gD6kRzzeXS87cycVGlO3smXA9o+yeN0BDkTVaOc0=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/js/swiper.js" integrity="sha256-2AzmZuC/JWSxd9zvzxqNIBQIoB/uSRsSYtXJBhAkfjY=" crossorigin="anonymous"></script>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="slide-inner" style="background-image:url(https://pp.userapi.com/c836139/v836139003/63ce1/QThPx7qPzvU.jpg)"></div>
</div>
<div class="swiper-slide">
<div class="slide-inner" style="background-image:url(https://pp.userapi.com/c412624/v412624691/4136/_da_uAA6ha8.jpg)"></div>
</div>
<div class="swiper-slide">
<div class="slide-inner" style="background-image:url(https://pp.userapi.com/c637331/v637331691/48f5f/spHnV42iYVw.jpg)"></div>
</div>
<div class="swiper-slide">
<div class="slide-inner" style="background-image:url(https://pp.userapi.com/c837139/v837139407/67f52/fFqjq4U2mEk.jpg)"></div>
</div>
</div>
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white"></div>
</div>

I think that, you can do like this.
onProgress: (swiper, progress) => {...}

Related

Adding Images Dynamically into Swiper JS not working

I have a page where images need to showed in slider.
// Initializing the Swiper
var swiper = new Swiper(".mySwiper", {
loop: true,
spaceBetween: 10,
slidesPerView: 4,
freeMode: true,
autoplay: {
delay: 1000,
disableOnInteraction: false,
},
watchSlidesProgress: true,
});
var swiper2 = new Swiper(".mySwiper2", {
loop: true,
spaceBetween: 10,
autoplay: {
delay: 1000,
disableOnInteraction: false,
},
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
thumbs: {
swiper: swiper,
},
});
$("body").keydown(function (e) {
if (e.keyCode == 37) { // top
swiper.slidePrev();
}
else if (e.keyCode == 39) { // bottom
swiper.slideNext();
}
});
let image_list = ["https://swiperjs.com/demos/images/nature-1.jpg", "https://swiperjs.com/demos/images/nature-2.jpg", "https://swiperjs.com/demos/images/nature-3.jpg", "https://swiperjs.com/demos/images/nature-4.jpg", "https://swiperjs.com/demos/images/nature-5.jpg", "https://swiperjs.com/demos/images/nature-6.jpg", "https://swiperjs.com/demos/images/nature-7.jpg",
"https://swiperjs.com/demos/images/nature-8.jpg",
"https://swiperjs.com/demos/images/nature-9.jpg",
"https://swiperjs.com/demos/images/nature-10.jpg"]
// Adding Images
for (i = 0; i < image_list.length; i++) {
let div = '<div class="swiper-slide"><img src="' + image_list[i] + '" alt=""/></div>';
$("#large-img").append(div);
$("#small-img").append(div);
}
/* SWIPER JS */
.swiper {
width: 100%;
height: 100%;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
.swiper-slide img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
.swiper {
width: 100%;
height: 10rem;
margin-left: auto;
margin-right: auto;
}
.swiper-slide {
background-size: cover;
background-position: center;
}
.mySwiper2 {
height: 27rem;
width: 100%;
}
.mySwiper {
height: 20%;
box-sizing: border-box;
padding: 10px 0;
}
.mySwiper .swiper-slide {
width: 25%;
height: 100%;
opacity: 0.4;
}
.mySwiper .swiper-slide-thumb-active {
opacity: 1;
}
.swiper-slide img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.css" />
<div class="container-fluid" style="padding: 0;">
<div class="row g-0 justify-content-center mt-5">
<div class="col-md-12">
<div style="--swiper-navigation-color: #fff; --swiper-pagination-color: #fff" class="swiper mySwiper2">
<div class="swiper-wrapper" id="large-img">
<!-- Images coming from the backend through javascript -->
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
<div thumbsSlider="" class="swiper mySwiper">
<div class="swiper-wrapper" id="small-img">
<!-- Images coming from the backend through javascript -->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
When I am adding the Images Dynamically through the javascript into the HTML template, the SWIPER doesnt work. I am actually passing the media url of the image in the img.src attribute as I am using Django as the backend. No matter from where you pass the image url, it doesnt work.
**UPDATE** : In running this code, it works on the full screen mode (Only the autoplay and next and previous button. Clicking on the thumbnail isnt working) and in the small window which opens when pressing the Run Snippet, the Swiper.js doesnt work. The problem in the small window is similar to what I am seeing in my website.
You should load the Swiper after loading the images, this way it can use the images when initializing. Maybe it would be a good way to check the Swipper documentation, I saw it has some lazy loading parameters which could possibly help solve this differently.
doc:
https://swiperjs.com/swiper-api#lazy-loading

After clicking a hamburger icon, the menu does not display above slide-show images on the main page

I do not know how to show the menu above the slide-show images after clicking the hamburger icon. I used z-index and .addClass in order to add the diplay:none property to the #slideshow. This did not work. I do not know what I can do next.
https://jsfiddle.net/ft31scgw/
main.js
<script>
$(document).ready(() => {
$('#slideshow .slick').slick({
autoplay: true,
autoplaySpeed: 2000,
dots: true,
});
});
$(document).ready(() => {
$('#userReview .slick').slick({
autoplay: true,
autoplaySpeed: 8000,
dots: true,
});
});
function hMenu() {
var menu = document.getElementById("toggle");
if(menu) {
var hide = $("#slideshow").hide();
} else {
var show = $("#slideshow").show();
}
}
</script>
style.css
enter image description here
#media only screen and (max-width: 736px) {
#slideshow {
position: relative;
top: 0px;
left: 0px;
}
#slideshow {
div {
width: 100%;
height: 300px;
img {
width: 100%;
height: auto;
}
}
}
button {
text-transform: uppercase;
font-weight: bold;
}
.logo img {
width: 80%;
max-width: 473px;
height: 50px;
}
label{
display: block;
cursor: pointer;
z-index: 10;
}
.menu {
text-align: center;
width: 100%;
display: none;
background: black;
}
.menu a {
display: block;
border-bottom: 1px solid #EAEAEB;
margin: 1;
}
#toggle:checked + .menu {
display: block;
z-index: 10;
}
#slideshow {
z-index: 0;
}
.disappear {
display:none;
z-index: -2;
}
}/* #media min-width 736px */
index.html
<div id="header">
<div class="logo">
<h1><img src="img/logo.png" widht="473px" height="50px"></h1>
</div>
<div class="nav">
<label for="toggle">☰</label>
<input type="checkbox" id="toggle" onclick="hMenu()" />
<div class="menu">
Work
About Us
Services
Blog
<span>Contact Us</span>
</div>
</div>
</div><!-- /#header -->
<section id="slideshow">
<div class="slick">
<div><img src="img/image1.jpg" alt=""></div>
<div><img src="img/image2.jpg" alt=""></div>
<div><img src="img/image3.jpg" alt=""></div>
</div>
</section>
Good to know, that z-index specifies the stack order of siblings elements. In your case <div class="header"> needs z-index greater than <section id="slideshow">. z-index only works on positioned elements, so both of them should have position:relative. I made a working example for you on jsFiddle.

Making Swiper Slider's Transition Unidirectional

I have put together an image carousel with the help of Swiper.
My carousel uses background images, bullets and vertical transition. I want this vertical transition to be unidirectional. In other words, the carousel transition direction should be upwards, regardless of the succession of the bullets clicked.
var swiper = new Swiper('.swiper-container', {
direction: 'vertical',
auto: true,
speed: 1000,
loop: true,
autoplay: {
delay: 9000,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
controller: {
inverse: true,
}
});
.swiper-container {
position: relative;
max-width: 1200px;
margin: 0 auto;
height: 265px;
}
.swiper-wrapper {
height: 265px;
}
.swiper-slide {
transition: 1s ease-in-out;
top: 0;
width: 100%;
height: 265px;
background-repeat: no-repeat;
background-position: center center;
}
.swiper-slide img {
margin: 0 auto;
width: 620px;
height: 265px;
}
.swiper-container-vertical>.swiper-pagination-bullets {
bottom: 0;
top: auto !important;
}
.swiper-container-vertical>.swiper-pagination-bullets .swiper-pagination-bullet {
margin: 0 !important;
outline: none;
opacity: 1;
float: left;
width: 18px;
height: 18px;
background: url("https://grgs.ro/1/i/sprite.png") no-repeat -528px -502px;
}
.swiper-container-vertical>.swiper-pagination-bullets .swiper-pagination-bullet.swiper-pagination-bullet-active {
background-position: -528px -524px;
}
<link href="https://necolas.github.io/normalize.css/8.0.0/normalize.css" rel="stylesheet"/>
<link href="https://idangero.us/swiper/dist/css/swiper.css" rel="stylesheet" />
<script src="https://idangero.us/swiper/dist/js/swiper.min.js"></script>
<div class="swiper-container">
<div class="swiper-wrapper">
<a href="#" class="swiper-slide" style="background-image: url('https://picsum.photos/1200/265/?gravity=east');">
<img src="https://5.grgs.ro/images/b/c2/303073784cd9d0ee3aea0e039629ce5b.png" alt="">
</a>
<a href="#" class="swiper-slide" style="background-image: url('https://picsum.photos/1200/265/?gravity=south');">
<img src="https://5.grgs.ro/images/b/c2/303073784cd9d0ee3aea0e039629ce5b.png" alt="">
</a>
<a href="#" class="swiper-slide" style="background-image: url('https://picsum.photos/1200/265/?gravity=west');">
<img src="https://5.grgs.ro/images/b/c2/303073784cd9d0ee3aea0e039629ce5b.png" alt="">
</a>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
I have been looking for information on the Swiper website, including the forum but could not find the way to make it unidirectional. How could I do that?
Maybe this will help you, pass the parameter allowSlidePrev: false;.
I have never used Swiper but and I have no way to test so i don't know if this will solve your bullets problem but in theory this will make it unidirectional .

Reload Owl Carousel 2 when side navigation opens or closes

I am working on a website, and it is built off a theme. In the body of the site is structured to have a left-panel div which houses the navigation and a right-panel div which houses the body content.
When the left-panel opens from a collapsed view (showing only icons) to the full view (showing the nav text), owl-carousel, since it's loaded on page load, doesn't readjust the width.
I've tried a few methods to try and reload the carousel, following their API, but am not successful. The body doesn't have a set width, such as inline styles, but instead the class left-menu-open is set, when the left menu is open.
I've also looked at several other cases of people trying to do the same thing, but none of their code examples work.
Below is my code. I am running this in a .php file, so I am able to load multiple sliders in the body content, without them rotating in relation. The carousel loads and functions fine, it's just that it begins to clip if the page is loaded with the nav open and one closes the nav, or the 3rd slide shows if the page is loaded with the nav closed and is opened.
One method I've tried is
if ( $( 'body' ).resize() { }
if ( $( 'body' ).hasClass( 'left-nav-open' ) { } else if ( !$( 'body ').resize() { }
(function($) {
$(function() {
var $owl = $('.owl-<?php echo $owl_widget_title; ?>');
$owl.owlCarousel({
// your initial option here, again.
loop:true,
nav:true,
navText: ["<i class=\"fa fa-chevron-left\"></i>","<i class=\"fa fa-chevron-right\"></i>"],
dots: false,
lazyLoad: true,
lazyContent: true,
autoplaySpeed: 1000,
autoplayTimeout: 7000,
autoplayHoverPause: true,
responsive : {
0 : {
items: 1,
slideBy: 1,
autoHeight:true,
},
992 : {
items: <?php echo $num_of_items; ?>,
slideBy: <?php echo $num_of_items; ?>,
}
}
});
});
})(jQuery)
I've tried destroy.owl.carousel, and then initialize.owl.carousel but neither of those seem to work or run at all.
Any and all help is appreciated! Thank you
To update owl after resize of container one would need to call .onResize() _handler on its data. The updateOwl() function should look like this:
updateOwl = function(){
$(".owl-carousel").each(function() {
$(this).data('owl.carousel').onResize();
});
};
The only thing to watch for is when exactly to call this function. I assume the sidebar is not jumping into position, but rather smoothly animating. The call to .onResize() needs to be delayed by the duration of animation, so the size of the carousel is calculated based on final content size. Delay the execution of updateOwl() by wrapping it into a setTimeout() (equal or slightly longer than the sidebar animation duration):
$(document).on('click', '.sidebarToggle', function(){
setTimeout(function(){
updateOwl();
}, 321)
});
...where .sidebarToggle would be your sidebar opener.
Working example:
(function($) {
var $owl = $('.owl-carousel'),
updateOwl = function(){
$owl.each(function() {
$(this).data('owl.carousel').onResize();
});
};
$owl.owlCarousel({
loop: true,
nav: true,
navText: ['<i class="fa fa-chevron-left"></i>', '<i class="fa fa-chevron-right"></i>'],
dots: false,
lazyLoad: true,
autoplaySpeed: 1000,
autoplayTimeout: 7000,
autoplayHoverPause: true,
responsive: {
0: {
items: 1,
slideBy: 1,
autoHeight: true,
},
992: {
items: 3,
slideBy: 3,
}
}
});
$(document).on('click', '.sidebarToggle', function(){
$('body').toggleClass('sidebarOpen');
setTimeout(function(){
updateOwl();
}, 321)
});
$(window).on('resize', updateOwl);
})(jQuery)
body {
margin: 0;
transition: padding-left .3s cubic-bezier(.4,0,.2,1);
}
.sidebar {
height: 100vh;
position: absolute;
width: 200px;
background-color: #888;
left: -200px;
top:0;
transition: left .3s cubic-bezier(.4,0,.2,1);
box-sizing: border-box;
}
.sidebarOpen .sidebar {
left: 0;
}
body.sidebarOpen {
padding-left: 200px;
}
.owl-carousel .owl-item {
padding: 0 45px;
box-sizing: border-box;
}
.owl-carousel .owl-item > div{
min-height: 135px;
width: 100%;
border: 1px solid #ccc;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: Gainsboro;
border-radius: 3px;
}
button {
margin: 15px;
}
.owl-carousel {
position: relative;
margin: 15px 0 0;
}
.owl-nav >div {
position: absolute;
top: 50%;
width: 20px;
transform: translate(-50%, -50%);
text-align: center;
}
.owl-prev {left: 20px}
.owl-next {left: calc(100% - 20px);}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.theme.default.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/owl.carousel.min.js"></script>
<div class="sidebar"></div>
<div class="owl-carousel">
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
<div> Your Content </div>
</div>
<button class="sidebarToggle">SidebarToggle</button>
If the above is not working for you, I'll need to have a look at your implementation to be able to debug it.
Side note: lazyContent is currently unavailable. According to plugin author:
...lazyContent was introduced during beta tests but i removed it from the final release due to bad implementation. It is a nice options so i will work on it in the nearest feature.
I use destroy.owl.carousel to destroy carousel. Carousel reinitialize on click of left navigation menu
var $owl = $('.owl-carousel');
/*inital on load */
$owl.owlCarousel({
items: 6,
lazyLoad: true,
loop: true,
margin: 10,
});
/*on click of navigation menu */
function resizeCarosel(obj) {
if (obj.classList.contains('is-open')) {
$owl.trigger('destroy.owl.carousel'); /*destroy Carousel*/
$owl.owlCarousel({ /*reinitialize Carousel*/
items: 6,
lazyLoad: true,
loop: true,
margin: 10,
});
}
if (obj.classList.contains('is-closed')) {
$owl.trigger('destroy.owl.carousel'); /*destroy Carousel*/
$owl.owlCarousel({ /*reinitialize Carousel*/
items: 2,
lazyLoad: true,
loop: true,
margin: 10,
/*for responsive
items: 4,
responsiveClass: true,
responsive: {
0: {
items: 1,
nav: true
},
600: {
items: 3,
nav: false
},
1000: {
items: 5,
nav: true,
loop: false
}
}
*/
});
}
}
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://rawgit.com/FragCoder/bootstrap-left-slide-menu/master/bootstrap-left-slide-menu.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/FragCoder/bootstrap-left-slide-menu/master/bootstrap-left-slide-menu.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/smashingboxes/OwlCarousel2/2.0.0-beta.3/dist/assets/owl.carousel.css">
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/smashingboxes/OwlCarousel2/2.0.0-beta.3/dist/assets/owl.theme.default.css">
<script type="text/javascript" src="https://cdn.rawgit.com/smashingboxes/OwlCarousel2/2.0.0-beta.3/dist/owl.carousel.js"></script>
<div id="wrapper" class="">
<div class="overlay" style="display: none;"></div>
<nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
<ul class="nav sidebar-nav">
<li class="sidebar-brand">
BLESM
</li>
<li>
<i class="glyphicon glyphicon-camera"></i> Photo
</li>
<li>
<i class="glyphicon glyphicon-facetime-video"></i> Video
</li>
<li>
<i class="glyphicon glyphicon-headphones"></i> Music
</li>
<li>
<i class="glyphicon glyphicon-cloud"></i> Cloud
</li>
<li>
<i class="glyphicon glyphicon-th"></i> Apps
</li>
<li>
<i class="glyphicon glyphicon-cog"></i> Settings
</li>
</ul>
</nav>
<div id="page-content-wrapper">
<button type="button" class="hamburger animated fadeInLeft is-closed" data-toggle="offcanvas" onclick="resizeCarosel(this)">
<span class="hamb-top"></span>
<span class="hamb-middle"></span>
<span class="hamb-bottom"></span>
</button>
<div class="container">
<div class="owl-carousel owl-theme">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=1" data-src-retina="https://placehold.it/350x250&text=1-retina" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=2" data-src-retina="https://placehold.it/350x250&text=2-retina" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=3" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=4" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=5" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=6" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=7" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=8" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=9" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=10" alt="">
<img class="owl-lazy" data-src="https://placehold.it/350x250&text=11" alt="">
</div>
</div>
</div>
</div>
You should be able to get the job done by simple deleting the element from the DOM (jquery has a function .remove()) and then recreating exactly as you did with the first one. Notice that you will have to store the variables that come from the server in the client.
I propose to trigger the refresh.owl.carousel event.
My sidebar uses transition so I need to detect the transitionend event.
I've set the .main block as the responsiveBaseElement. Now the carousel adjusts to the width of its container, and not to the width of the entire page.
And I've added a few more jump points to make the responsive changes more
visible.
Please check the result. Is it what you want to achieve?
https://codepen.io/glebkema/pen/zwozRx
var $owl = $('#myCarousel');
var owl = $owl.owlCarousel({
autoplay: true,
autoplayHoverPause: true,
dots: false,
loop: true,
nav: true,
navText: [ "<i class=\"fa fa-chevron-left\"></i>",
"<i class=\"fa fa-chevron-right\"></i>" ],
responsiveBaseElement: '.main',
responsive : {
0 : {
items: 1,
slideBy: 1,
},
400 : {
items: 2,
slideBy: 1,
},
768 : {
items: 3,
slideBy: 2,
},
992 : {
items: 4,
slideBy: 2,
},
1200 : {
items: 5,
slideBy: 2,
},
},
});
$('.sidebar-switcher').click(function(){
$('body').toggleClass( 'body-open' );
$('.main').one("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend", function(event) {
owl.trigger('refresh.owl.carousel');
});
});
* { box-sizing: border-box; }
body {
margin: 0;
}
/** sidebar closed **/
.main,
.sidebar {
-webkit-transition: margin .4s ease-out;
-moz-transition: margin .4s ease-out;
-ms-transition: margin .4s ease-out;
-o-transition: margin .4s ease-out;
transition: margin .4s ease-out;
}
.main {
padding: 0 36px;
margin-left: 70px;
}
.sidebar {
background: #ccc;
float: left;
height: 100vh;
margin-left: -230px;
position: relative;
width: 300px;
}
.sidebar-switcher {
position: absolute; top: 12px; right: 12px;
}
.sidebar-switcher:before {
content: '\f0c9';
cursor: pointer;
font-family: 'FontAwesome';
font-size: 30px;
line-height: 1;
position: absolute; top: 12px; right: 12px;
}
/** sidebar opened **/
.body-open .main {
margin-left: 300px;
}
.body-open .sidebar {
margin-left: 0;
}
.body-open .sidebar-switcher:before {
content: '\f00d';
}
/** owl carousel **/
.owl-item > div {
margin: 12px;
}
.owl-next,
.owl-prev {
position: absolute;
top: 0;
width: 36px;
bottom: 0;
}
.owl-next {
left: 100%;
margin-left: -12px;
}
.owl-prev {
right: 100%;
margin-right: -12px;
}
.owl-next i,
.owl-prev i {
font-size: 30px;
line-height: 24px;
margin-top: -12px;
position: absolute;
top: 50%;
}
.owl-next i {
left: 12px;
}
.owl-prev i {
right: 12px;
}
.owl-next:hover i,
.owl-prev:hover i {
color: red;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="sidebar">
<div class="sidebar-switcher"></div>
</div>
<div class="main">
<div class="owl-carousel" id="myCarousel">
<div><img src="https://via.placeholder.com/400x200/fc3/fff/?text=1" alt=""></div>
<div><img src="https://via.placeholder.com/400x200/693/fff/?text=2" alt=""></div>
<div><img src="https://via.placeholder.com/400x200/369/fff/?text=3" alt=""></div>
<div><img src="https://via.placeholder.com/400x200/f63/fff/?text=4" alt=""></div>
<div><img src="https://via.placeholder.com/400x200/936/fff/?text=5" alt=""></div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/owl.carousel.min.js"></script>

Why is my jquery code not working or throwing any errors?

You can checkout my project here: http://jsfiddle.net/raj4dev/2o4uapc9/1/
Summary: I am making a jquery image slider. You can change the image in a box by pressing next and previous arrows. Currently, I have added code only for the next arrow.
Issue: Nothing happens when I click the next arrow. I don't see any errors in the chrome developer tools.
Please tell me how I can debug and fix this issue ?
Code here.
HTML:
<!DOCTYPE html>
<body>
<div id="container">
<header>
<h1>jQuery content slider</h1>
</header>
<img src="http://icongal.com/gallery/image/337589/small_arrow_left.png" alt="Prev" id="prev">
<div id="slider">
<div class="slide">
<div class="slide-copy">
<h2>Slide 1</h2>
<p>Dance and slide!</p>
</div>
<img src="http://m.rgbimg.com/cache1ny0NN/users/j/ja/jana_koll/600/mfOAUfa.jpg" alt="an image">
</div>
<div class="slide">
<div class="slide-copy">
<h2>Slide 2</h2>
<p>Dance and slide!</p>
</div>
<img src="https://s-media-cache-ak0.pinimg.com/736x/f9/2e/c0/f92ec0238d508e029be8b7163205d24e.jpg" alt="an image">
</div>
<div class="slide">
<div class="slide-copy">
<h2>Slide 3</h2>
<p>Dance and slide!</p>
</div>
<img src="http://www.pptbackgroundstemplates.com/backgrounds/abstract-red-light-wave-ppt-backgrounds-powerpoint.jpg" alt="an image">
</div>
</div>
<img src="http://icons.iconarchive.com/icons/saki/nuoveXT/128/Small-arrow-right-icon.png" alt="Next" id="next">
</div>
</body>
JS:
$(document).ready(function() {
var sliding_speed = 500;
var autoswitch = true;
var autoswitch_speed = 4000;
$('.slide').first().addClass('active');
//Hide all slides, but...
$('.slide').hide();
//...show only the first slide
$('.active').show();
$('#next').on('click', function(){
$('.active').removeClass('active').addClass('oldActive'); /*'Hide the currently active slide and mark it as oldActive so that you can go back to it using the previous button.'*/
if($('.oldActive').is(':last-child')) {
$('.slide').first().addClass('active');
}else {
$('.oldActive').next().addClass('active');
}
$('.oldActive').removeClass('oldActive');
});
});
CSS:
* {
margin: 0;
padding: 0;
}
body {
font-family: 'Arial', sans-serif;
font-size: 14px;
color: #fff;
background: #333;
line-height: 1.6em;
}
a {
color: #fff;
text-decoration: none;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
#container {
width: 980px;
margin: 40px auto;
overflow: hidden;
}
#slider {
width: 940px;
height: 350px;
position: relative;
overflow: hidden;
float: left;
padding: 3px;
border: #666 solid 2px;
border-radius: 5px;
}
#silder img {
width: 940px;
height: 350px;
}
.slide {
position: absolute;
}
.slide-copy {
position: absolute;
bottom: 0px;
left:0;
padding: 20px;
background: #7f7f7f;
background: rgba(0,0,0,0.5);
width: 100%;
}
#prev, #next {
float: left;
margin-top: 130px;
cursor: pointer;
position: relative;
z-index: 100;
}
#prev {
margin-right: -45px;
}
#next {
margin-left: -45px;
}
You need to again hide all slide and then show only the active one. You are changing the classes to properly set your desired active slide to have the active class, but nothing says that all active slides are shown always and all other slides are hidden always.
http://jsfiddle.net/v1au2d57/
$('#next').on('click', function(){
$('.active').removeClass('active').addClass('oldActive'); /*'Hide the currently active slide and mark it as oldActive so that you can go back to it using the previous button.'*/
if($('.oldActive').is(':last-child')) {
$('.slide').first().addClass('active');
}else {
$('.oldActive').next().addClass('active');
}
$('.oldActive').removeClass('oldActive');
//Hide all slides, but...
$('.slide').hide();
//...show only the first slide
$('.active').show();
});

Categories