asyadiqin
asyadiqin

Reputation: 1667

Bind Mouse Wheel To Jquery UI Slider

I did searches and there were a few similar posts but I can't seem to get it to work. I know that its a cliche but I am quite new to JQuery and JQuery UI as my core skills is PHP, so any help is greatly appreciated. Below are the codes that I have for the JQuery vertical slider.

$("#VerticalScrollBar").slider({    
    orientation: "vertical",
    change: VerticalHandleChange,
    slide: VerticalHandleSlide,
    min: -100,
    max: 0
}); 

and the functions

function VerticalHandleChange(e, ui) {
var maxScroll = $(".VerticalScroll").attr("scrollHeight") - $(".VerticalScroll").height();
$(".VerticalScroll").animate({ 
    scrollTop: -ui.value * (maxScroll / 100)
}, 1000);

function VerticalHandleSlide(e, ui) {
var maxScroll = $(".VerticalScroll").attr("scrollHeight") - $(".VerticalScroll").height();
$(".VerticalScroll").attr({ 
    scrollTop: -ui.value * (maxScroll / 100)   
});

The vertical slider works fine but now I need to integrate mouse wheel support. I have downloaded the mouse wheel plugin by Brandon Aaron (jquery-mousewheel ver. 3.0.4) but I have no idea how to use it with my codes above. Anyone can help me with this? Thanks again.

Upvotes: 3

Views: 6552

Answers (1)

Romuald Brunet
Romuald Brunet

Reputation: 5831

After doing some tests I've come up with this solution:

$('#sliderid').on('mousewheel DOMMouseScroll', function(e) {
    var o = e.originalEvent;
    var delta = o && (o.wheelDelta || (o.detail && -o.detail));

    if ( delta ) {
        e.preventDefault();

        var step = $(this).slider("option", "step");
            step *= delta < 0 ? 1 : -1;
        $(this).slider("value", $(this).slider("value") + step);
    }
});

Seems to work fine on Chrome/Firefox/Opera (using jQuery 1.10.1)

Upvotes: 5

Related Questions