What's the best way to implement scroll with slick.js so that when you scroll with your mouse, it switches to the next slide? Or is there a setting I am missing?
Here is what I'm referring to, just in case. http://kenwheeler.github.io/slick/
What's the best way to implement scroll with slick.js so that when you scroll with your mouse, it switches to the next slide? Or is there a setting I am missing?
Here is what I'm referring to, just in case. http://kenwheeler.github.io/slick/
You can implement scroll by using the wheel
event which is fired when a wheel button of a pointing device (usually a mouse) is rotated.
const slider = $(".slider-item");
slider
.slick({
dots: true
});
slider.on('wheel', (function(e) {
e.preventDefault();
if (e.originalEvent.deltaY < 0) {
$(this).slick('slickNext');
} else {
$(this).slick('slickPrev');
}
}));
.container {
margin: 0 auto;
padding: 40px;
width: 80%;
color: #333;
background: #419be0;
}
.slick-slide {
text-align: center;
color: #419be0;
background: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/kenwheeler/[email protected]/slick/slick.css" />
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/[email protected]/slick/slick-theme.css" />
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/kenwheeler/[email protected]/slick/slick.min.js"></script>
<div class='container'>
<div class='slider-item'>
<div>
<h3>1</h3>
</div>
<div>
<h3>2</h3>
</div>
<div>
<h3>3</h3>
</div>
<div>
<h3>4</h3>
</div>
<div>
<h3>5</h3>
</div>
<div>
<h3>6</h3>
</div>
</div>
</div>
I little modified previous answer for Mac. Because there scroll triggering multiple times.
var slider = $(".slider-item");
var scrollCount = null;
var scroll= null;
slider
.slick({
dots: true
});
slider.on('wheel', (function(e) {
e.preventDefault();
clearTimeout(scroll);
scroll = setTimeout(function(){scrollCount=0;}, 200);
if(scrollCount) return 0;
scrollCount=1;
if (e.originalEvent.deltaY < 0) {
$(this).slick('slickNext');
} else {
$(this).slick('slickPrev');
}
}));
I came up with a perfect implementation of @Maxym Kopych answer this way:
let blocked = false;
let blockTimeout = null;
let prevDeltaY = 0;
$("#slick").on('mousewheel DOMMouseScroll wheel', (function(e) {
let deltaY = e.originalEvent.deltaY;
e.preventDefault();
e.stopPropagation();
clearTimeout(blockTimeout);
blockTimeout = setTimeout(function(){
blocked = false;
}, 50);
if (deltaY > 0 && deltaY > prevDeltaY || deltaY < 0 && deltaY < prevDeltaY || !blocked) {
blocked = true;
prevDeltaY = deltaY;
if (deltaY > 0) {
$(this).slick('slickNext');
} else {
$(this).slick('slickPrev');
}
}
}));
It will prevent macOS multiple scroll events from firing, but it will allow the user to rescroll manually without waiting for the events to completely stop.
Is it possible to change the current behavior so that no matter how fast or slow the user does the mouse scrolling action, he can only go to the next slide (and not jump slides )?
To me, it is unexpected behavior to go from slide 1 to say slide 6 with a single and normal scroll action.
slider.on('wheel', (function(e) {
if ( e.originalEvent.deltaX !== 0 ) {
e.preventDefault();
if (e.originalEvent.deltaX >= 10) {
$(this).slick('slickPrev');
}
if (e.originalEvent.deltaX <= -10) {
$(this).slick('slickNext');
}
}
}));
Scroll slick horizontally, allow page scroll vertically:
let blocked = false;
let blockTimeout = null;
let prevDeltaY = 0;
$("#slick").on('mousewheel DOMMouseScroll wheel', (function(e) {
let deltaX = e.originalEvent.deltaX;
let deltaY = e.originalEvent.deltaY;
if(typeof deltaY != 'undefined') {
clearTimeout(blockTimeout);
blockTimeout = setTimeout(function(){
blocked = false;
}, 50);
if ((deltaY < 1 && deltaY > -1) && ((deltaX > 10 && deltaX > prevDeltaX) || (deltaX < -10 && deltaX < prevDeltaX) || !blocked)) {
e.preventDefault();
e.stopPropagation();
blocked = true;
prevDeltaX = deltaX;
if (deltaX > 0) {
$(this).slick('slickNext');
} else {
$(this).slick('slickPrev');
}
}
}
}));
© 2022 - 2024 — McMap. All rights reserved.