Home › Forums › JavaScript › Disable mousewheel events
- This topic is empty.
-
AuthorPosts
-
May 17, 2016 at 7:59 am #241789
amanda_
ParticipantHi,
I’m working on a site that has unusual navigation. It is laid out as one big page with 9 sections, and they can only see one section at a time. The user has two options to navigate the site
- They can mouse back and forth to the next “section” this involves slowly navigating the entire page.
- They can quickly tween to which ever section they choose via the standard off-canvas menu on the side.
I’ve got it working as it’s supposed to, but it breaks down in one situation – when a user chooses to tween directly to a section and if they use their mouse before the tween has finished. Then things get jumbled and jump all over the place.
Is there a way to disable the event listener?
Thanks!
Here is my very simplified codepen example:
http://codepen.io/ald603/pen/oxRGKEMay 17, 2016 at 8:16 am #241794GMK Hussain
Participantadd this jQuery code mousewheel will disable.
$('body').bind("mousewheel", function() { return false; });
May 17, 2016 at 9:36 am #241799amanda_
ParticipantThank you so much for your response, GMK Hussain. It is working as I hoped to disable the mousescroll.
Here’s my followup question – I just want to temporarily disable the mousewheel function, just during the tween.
I have created a function:
function disable(){ $('body').bind("mousewheel", function() { return false; }); }
which I run onclick on opening the menu, so the tweens occur, no issues of jumping around.
My logic tells me I should be able to create a function that starts the mousewheel functionality back up again, which I created as such:
function enable(){ $('body').bind("mousewheel", function() { return true; }); }
I’m them calling this function onComplete of the tweens:
$("#campusButton").click(function() { menuClose(); TweenLite.to($row, 2, { y: '-100%', x: '0%', onComplete:function(){ tl1.seek('slide2'); enable(); } }); });
This, alas, does not work. Anything pointing in the right direction would be much appreciated!
May 17, 2016 at 9:41 am #241800Shikkediel
ParticipantThat’s not really cross browser compatible (and
.bind
a bit old), I’d go with :$(window).on('wheel', function() { return false; });
Or for deeper browser support :
$(window).on('mousewheel DOMMouseScroll', function() { return false; });
May 17, 2016 at 4:34 pm #241810Shikkediel
ParticipantThe other event listener is still in place and “blocking” the
return true
. Give this a try, removing the event listener :function disable() { $(window).on('wheel.impair', function() { return false; }); } function enable() { $(window).off('wheel.impair'); }
Using a namespace (
impair
here) makes it easier to unbind only a particular listener instead of disabling all events …May 18, 2016 at 6:42 am #241828amanda_
ParticipantHHmm. None of these are altogether working. Thank you for your help so far, still trying to figure this one out.
May 18, 2016 at 10:24 am #241840Shikkediel
ParticipantMaybe take the plain JS route then…
function disable() { document.removeEventListener('mousewheel', Go); document.removeEventListener('DOMMouseScroll', Go); } function enable() { document.addEventListener('mousewheel', Go); document.addEventListener('DOMMouseScroll', Go); }
May 18, 2016 at 11:43 am #241844amanda_
ParticipantAwesome!!! This works. It makes total sense, now that I look at it.
Thank you!!
May 18, 2016 at 2:04 pm #241848Shikkediel
ParticipantGlad that did the trick. :-)
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.