YUI3 scrollView和鼠标滚轮

时间:2011-11-07 18:41:58

标签: yui scrollview mousewheel yui3

我开始只使用YUI3。我包含组件scrollView,但它没有工作mousewheel事件,在我没有找到如何打开它的选项。我将不胜感激任何帮助。

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

1 个答案:

答案 0 :(得分:1)

我偶然发现了这一点,经过一些试验和错误,我设法让它工作(请注意,它只是简单的滚动,没有缓和)。

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));
    }
};

这是YUI鼠标滚轮事件,但它有点改变了。最大的问题是,最初是窗口或文档元素,这是没有意义的(例如,当您将#myelement鼠标移动到希望它作为返回目标时...)

Bellow是用于初始化ScrollView和处理鼠标滚轮事件的函数的代码:

// 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();
        };
    });

所以基本上我就是这样做的,但我的下一个挑战是让滚动条像常规滚动条一样工作(当你拖动它时,容器应该相应地移动......)

希望这有助于任何人:)