Mousewheel scrolling
-
Hi Sayful,
I’m wondering if it is possible to have the carousel scroll by using the mousewheel/trackpad? Owl Carousel JS mentions you can simply include the jquery.mousewheel.js plugin by using the following code:
owl.on('mousewheel', '.owl-stage', function (e) { if (e.deltaY>0) { owl.trigger('next.owl'); } else { owl.trigger('prev.owl'); } e.preventDefault(); });Can I simply include this in one of the Carousel Slider files? And if so, where?
Any help and pointers are much appreciated.Thanks π
-
This topic was modified 8 years, 6 months ago by
malosch.
-
This topic was modified 8 years, 6 months ago by
-
Hi π
So I tried to add the following code (as outlined here) inside the class-carousel-slider-script.php file:
_this.on('mousewheel', '.owl-stage', function(e) { //console.log("x: "+e.originalEvent.deltaX); //console.log("y: "+e.originalEvent.deltaY); if(e.originalEvent.deltaX > 100) { _this.trigger('next.owl'); } if(e.originalEvent.deltaX < 100) { _this.trigger('prev.owl'); } e.preventDefault(); });I added the code right after the
if (jQuery().owlCarousel) {statement inside thejQuery(document).ready(function ($) {function.Using
console.log(), I can see the mouse wheel values coming in steadily, so the function triggers properly and the values look good.
However, triggering next.owl and prev.owl doesn’t seem to work. The carousel always moves to the left, despite trying out different threshold values, or only using either next.owl or prev.owl in the code above. Sometimes, the carousel makes a single motion to the right but immediately moves to the left afterward. Based on that behavior, I assume something is overwriting my command or I am not using the correct command to control the carousel.Any idea what I need to change to make the carousel respond properly to the input of a mouse wheel?
Any help or pointers are much appreciated!
Thanks π
MarkusHi,
I was able to implement jquery.mousewheel.js to enable mouse wheel/trackpad navigation of the carousel.
Before sharing the code and what I have done, beware that there is still a bug. When scrolling horizontally on the trackpad to navigate between items, browsers interpret that as a gesture and go to the prev/next page. I am aware that this is outside of the scope of this thread but I will report back when I have found a solution to this.
Here’s what I’ve done to add mouse wheel/trackpad functionality to the carousel.
- Added jquery.mousewheel.js
- Added http://underscorejs.org/ to be able to prevent scrolling events from firing too often.
- Added the following code to the class-carousel-slider-script.php file inside the
if (jQuery().owlCarousel) {statement
_this.on('mousewheel wheel DOMMouseScroll MozMousePixelScroll', '.owl-stage', _.throttle(function(e) { if(e.originalEvent.deltaY < -4 || e.originalEvent.deltaX > 4) { // condition to trigger carousel to go to next item console.log("trigger NEXT"); // log in console for debugging _this.trigger('next.owl'); // trigger carousel to go to next item } else if (e.originalEvent.deltaY > 4 || e.originalEvent.deltaX < -4 ) { // condition to trigger carousel to go to prev item console.log("trigger PREV"); // log in console for debugging _this.trigger('prev.owl'); // trigger carousel to go to prev item } else { // do nothing } e.preventDefault(); }, 150)); // set throttling to 150msHi all,
Found a solution to prevent the browser from triggering navigating to the next/previous sides when using the touchpad. The solution is the position of
e.preventDefault();(see below)._this.on('mousewheel wheel DOMMouseScroll MozMousePixelScroll', '.owl-stage', _.throttle(function(e) { // attaching mousewheel to .owl-stage and using throttling from underscore.js to prevent scrolling events from firing too often //console.log(e.originalEvent.deltaX + " | " + e.originalEvent.deltaY); // debugging scroll distances if(e.originalEvent.deltaY < -35 || e.originalEvent.deltaX > 35) { // condition to trigger carousel to go to next item console.log("trigger NEXT"); // log in console for debugging _this.trigger('next.owl'); // trigger carousel to go to next item } else if (e.originalEvent.deltaY > 35 || e.originalEvent.deltaX < -35 ) { // condition to trigger carousel to go to prev item console.log("trigger PREV"); // log in console for debugging _this.trigger('prev.owl'); // trigger carousel to go to prev item } else { // do nothing } }, 150)); // set throttling to 150ms _this.on('mousewheel wheel DOMMouseScroll MozMousePixelScroll', '.owl-stage', function(e) { // e.preventDefault(); // prevent default event from being triggered -> prev/next screen in browser });Essentially, the
_throttle()function from underscore.js was preventinge.preventDefault()from triggering properly, which is needed to prevent the browser to interpret swipe gestures on the carousel as an attempt to navigate pages. Therefore, we can excludee.preventDefault()in the function navigating the carousel, and create a new function specifically fore.preventDefault()right after the carousel navigation has been completed.Hope that makes sense π
Hi Malosch,
I’ve postet a question that you maybe already solved with your solution. Can you have a look at it? You can find it here: https://wordpress.org/support/topic/mobile-touch-navigation/#post-10332189
maybe you can help me solve my problem. To be honest: I am at padawan level of coding and maybe thats the reason I don’t fully understand your code. If that is the solution to my problem, could you maybe give me a step by step walkthrough of how I can implement that? Keywords can be enough, I am ready to use google π I’d love to hear from you!
best
Georg
The topic ‘Mousewheel scrolling’ is closed to new replies.