Search code examples
yuiscrollviewmousewheelyui3

YUI3 scrollView and mousewheel


I'm starting to work only with YUI3. I include component scrollView, but it did not work mousewheel event, in the options I have not found how to turn on it. I would appreciate any help.

var scrollView = new Y.ScrollView({
    id: "scrollview",
    srcNode: '.scrollview-item',
    height: 375,
    flick: {
        minDistance: 10,
        minVelocity: 0.3,
        axis: "y"
    }
});
scrollView.render();

Solution

  • I stumbled upon this as well, after some trial and error, I managed to get that working(note, that it is just plain scrolling, without an easing).

    var DOM_MOUSE_SCROLL = 'DOMMouseScroll',
        fixArgs = function(args) {
            var a = Y.Array(args, 0, true), target;
            if (Y.UA.gecko) {
                a[0] = DOM_MOUSE_SCROLL;
                // target = Y.config.win;
            } else {
                // target = Y.config.doc;
            }
    
            if (a.length < 3) {
                // a[2] = target;
            } else {
                // a.splice(2, 0, target);
            }
    
            return a;
        };
    
    Y.Env.evt.plugins.mousewheel = {
        on: function() {
            return Y.Event._attach(fixArgs(arguments));
        },
    
        detach: function() {
            return Y.Event.detach.apply(Y.Event, fixArgs(arguments));
        }
    };
    

    This is the YUI mousewheel event, but it's changed a bit. The biggest issue was, that originally, either the window or document elements, which makes no sense(for example when you mousewheel over the #myelement you want that to be the returned target..)

    Bellow is the code used to initialize the ScrollView and the function that handles the mousewheel event:

    // ScrollView
        var scrollView = new Y.ScrollView({
            id: "scrollview",
            srcNode: '#mycontainer',
            height: 490,
            flick: {
                minDistance:10,
                minVelocity:0.3,
                axis: "y"
            }
        });
    
        scrollView.render();
    
        var content = scrollView.get("contentBox"); 
        var scroll_modifier = 10; // 10px per Delta
        var current_scroll_y, scroll_to;
    
        content.on("mousewheel", function(e) {
            // check whether this is the scrollview container
            if ( e.currentTarget.hasClass('container') ) {
                current_scroll_y = scrollView.get('scrollY');
                scroll_to = current_scroll_y - ( scroll_modifier * e.wheelDelta );
    
                // trying to scroll above top of the container - scroll to start
                if ( scroll_to <= scrollView._minScrollY ) {
                    // in my case, this made the scrollbars plugin to move, but I'm quite sure it's important for other stuff as well :)
                    scrollView._uiDimensionsChange();
                    scrollView.scrollTo(0, scrollView._minScrollY);
                } else if ( scroll_to >= scrollView._maxScrollY ) { // trying to scroll beneath the end of the container - scroll to end
                    scrollView._uiDimensionsChange();
                    scrollView.scrollTo(0, scrollView._maxScrollY);
                } else { // otherwise just scroll to the calculated Y
                    scrollView._uiDimensionsChange();
                    scrollView.scrollTo(0, scroll_to);
                };
                // if we have scrollbars plugin, flash the scrollbar
                if ( scrollView.scrollbars ) {
                    scrollView.scrollbars.flash();
                };
    
                // prevent browser default behavior on mouse scroll
                e.preventDefault();
            };
        });
    

    So basically that's how I managed to that, but my next challenge is to get the scrollbar work like regular scrollbar(when you drag it, the container should move correspondingly...)

    Hope this helps anyone :)