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とmousewheelイベントを処理する関数を初期化するために使用されるコードです。

// 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 に答える