2

I have modified jQuery UI Slider Scrollbar for my needs. There is the original version of slider http://jqueryui.com/slider/#side-scroll1

I have html

<div class="ui-slide-range">
    <div id="sliderContent">        
        <div class="viewer ui-corner-all">
            <div class="content-conveyor ui-helper-clearfix">
                <div class="item">
                    <img src="img/item4.jpg" height="152" width="205" alt="">   
                </div>
            </div>
       </div>
       <div id="slider"></div>
    </div>
</div> 

And jQuery code

$(function() {

//vars
var conveyor = $(".content-conveyor", $("#sliderContent")),
item = $(".item", $("#sliderContent"));

//set length of conveyor
conveyor.css("width", item.length * parseInt(item.css("width")));

    //config
    var sliderOpts = {
  max: (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $("#sliderContent")).css("width")),
      slide: function(e, ui) { 
        conveyor.css("left", "-" + ui.value + "px");
      }
    };

    //create slider
    $("#slider").slider(sliderOpts);
  });

$( "#slider" ).slider({
            range: "min",
            value: 1,
            slide: function() {
                update();
            }
}); 

How can I add mousewheel action for this slider? I used search, and find a lot of questions, but answers dont help me. Here are links to some questions. Question1 Question2

I've been working on this for a whole day, I can not in any way solve the problem, please help.

The JsFiddle link is here

Community
  • 1
  • 1
CroaToa
  • 900
  • 2
  • 14
  • 36
  • Im a bit against changing jQuery code. It will really get in the way when you want to update your jQuery version for bug fixes and all that. A scrolling div without the custom scrollbar is not hard work anyway as the scrolling can be done by the DOM. Consider doing this bit without using jQuery plugins. – woverton Jul 23 '14 at 16:39
  • This may be useful http://www.javascriptkit.com/javatutors/onmousewheel.shtml – woverton Jul 23 '14 at 16:40
  • @woverton there is just such a plugin mousewheel.js http://plugins.jquery.com/mousewheel/ I want to make it work.. – CroaToa Jul 23 '14 at 19:25

3 Answers3

2

DEMO jquery ui slider with mousewheel support using jquery mousewheel plugin

code:

$(function() {
    var value;
    $( "#slider" ).slider({
        slide: function( event, ui ) {
            value = $( "#slider" ).slider( "value" );
            $('#slider_val').html(value);
        }
    });

    value = $( "#slider" ).slider( "value" );
    $('#slider_val').html(value);

    $('#slider').on('mousewheel', function(event) {
        event.preventDefault();
        value = $( "#slider" ).slider( "value" );

        //console.log(event.deltaX, event.deltaY, event.deltaFactor);

        //Mousewheel Scrolled up
        if(event.deltaY == -1){
            //alert("scrolled down");
            value = value+1;
            $( "#slider" ).slider("value", value);
            $('#slider_val').html(value);
        }
        //Mousewheel Scrolled down
        else if(event.deltaY == 1){
            //alert("scrolled up");
            value = value-1;
            $( "#slider" ).slider( "value", value );
            $('#slider_val').html(value);
        }
    });

});     
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
  • On which browser are you checking ? – Rahul Gupta Jul 24 '14 at 09:11
  • Test it in firefox ! It works in that ! I don't have chrome lastes version so haven't tested with that ! – Rahul Gupta Jul 24 '14 at 09:13
  • The reason for not working on chrome is that in fiddle the mousewheel plugin file is not included because of some error, so in you local you will have to download that file from the live link to your local and include it accordingly and then it will work ! Try it and let me know if you have any problem ! – Rahul Gupta Jul 24 '14 at 09:18
  • I found the problem in your code. It was link to script) See an update http://jsfiddle.net/knY4N/1/ . Thanks! – CroaToa Jul 24 '14 at 09:28
  • 1
    That is what I was trying to say in my previous comment ! The code works great if the plugin scrip is included properly ! Please consider accepting the answer ! – Rahul Gupta Jul 24 '14 at 09:30
  • However, I cant bind it to my html markup( I dont know why( – CroaToa Jul 28 '14 at 09:09
1

( WORKING version of this post )

  1. you forgot "move" content (div with class "scroll-content" in slider)
  2. changed ( scroll down = look new photos)
  3. fixed values for content and slider

    setTimeout( sizeScrollbar, 10 );//safari wants a timeout
    
    $( ".scroll-pane" ).mousewheel(function (event, delta, deltaX, deltaY) {    
        event.preventDefault();
    
        var 
            value = scrollbar.slider( "option", "value" ),  // Get position of slider (% of 100%)
            value2 = scrollbar.slider( "option", "value" ), // Get position of slider (% of 100%)
    
            scrollSpeed = 75,
            widthFull = 2000;   // variable (for help: getimagesize, widthNow += ImgWidth, widthFull += widthNow)
            scrollLimit = (-1) * widthFull + innerWidth;    // innerWidth - browser's width
    
    
        value = (-1) * (value / 100) * widthFull  + delta * scrollSpeed;
        value > 0 && (value = 0);                           // 'left' limit
        value < scrollLimit && (value = scrollLimit);       // 'right' limit
    
        $(".scroll-content").css('margin-left', value );    // Setter for content
    
    
        value2 += -1 * delta * ( 100 / (widthFull / scrollSpeed) ); //100% / (count of times want to scroll)
        value2 < 0 && (value2 = 0);
        value2 > 100 && (value2 = 100);
    
        scrollbar.slider( "option", "value", value2 );   // Setter for slider
    });
    
0

I found a solution to this problem, may be useful to someone.

setTimeout( sizeScrollbar, 10 );//safari wants a timeout
$( ".scroll-pane" ).mousewheel(function (event, delta, deltaX, deltaY) {event.preventDefault();
   var value = scrollbar.slider( "option", "value" );
   value -= delta;
   value > 100 && (value = 100);
   value < 0 && (value = 0);
scrollbar.slider( "option", "value", value );
        });

 });

Just needed to add wrapper blocks to my code. Thank all.

JsFiddle link

CroaToa
  • 900
  • 2
  • 14
  • 36