1

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

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

1 回答 1

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 上时,您希望它成为返回的目标..)

下面是用于初始化 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();
        };
    });

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

希望这对任何人都有帮助:)

于 2011-12-09T08:39:58.897 回答