dojo 如何禁用dijit.form.NumberSpinner小部件上的鼠标滚轮事件?

7z5jn7bk  于 2022-12-16  发布在  Dojo
关注(0)|答案(2)|浏览(150)

我在基于dojo的应用程序中使用了一些dijit.form.NumberSpinner小部件,它们都连接到onChange操作。
当一个区域中有许多NumberSpinner时,就会出现问题:用户在页面上滚动,并在使用鼠标滚轮滚动整个区域时意外地用意外值填充NumberSpinner字段。
是否可以禁用dijit.form.NumberSpinner小部件上的鼠标滚轮事件?

dz6r00yl

dz6r00yl1#

如果你从来不需要它,并且你可以访问dojo源代码,并且能够自己构建,请在dijit/form/_Spinner. js上注解这一行:

postCreate: function(){
    // [...]
    // this.connect(this.domNode, !has("mozilla") ? "onmousewheel" : 'DOMMouseScroll', "_mouseWheeled");
    // [...]
}

或者,您可以在小部件上将intermediateChanges属性设置为true,并执行如下操作:
在您的html中:

<input id="spinner1" name="someNumber" data-dojo-type="dijit.form.NumberSpinner" data-dojo-props="value:'1000',smallDelta:'10',constraints:{min:9,max:1550,places:0}, intermediateChanges:'true'"/>

在您的javascript块中:

dojo.ready(function(){
    var spinner = dijit.byId("spinner1");
    var currentValue = spinner.get('value');
    dojo.connect(spinner, "onChange", function(value){
        currentValue = value;
    });
    dojo.connect(spinner.domNode, (!dojo.isMozilla ? "onmousewheel" : "DOMMouseScroll"), function(e){
        spinner.set('value',currentValue);
    });

});
vojdkbi0

vojdkbi02#

一个快速而肮脏的方法是,将if (!this.focused) return;添加到dijit/form/_Spinner.js_mouseWheeled的顶部,以使鼠标滚轮仅在小部件具有焦点时工作。

_mouseWheeled: function(/*Event*/ evt){
    // summary:
    //      Mouse wheel listener where supported
    if (!this.focused) return;
    ...

不过,正确的方法是扩展小部件。

// Disable _mouseWheeled when not in focus.
require(
    [
        "dojo/_base/lang"
        , "dojo/_base/event"
        , "dijit/form/_Spinner"
    ]
    , function(
        lang
        , event
        , _Spinner
    ){
        lang.extend(_Spinner, {
            _mouseWheeled: function(/*Event*/ evt){
                // summary:
                //      Mouse wheel listener where supported

                if (!this.focused) return;

                event.stop(evt);
                // FIXME: Safari bubbles

                // be nice to DOH and scroll as much as the event says to
                var wheelDelta = evt.wheelDelta / 120;
                if(Math.floor(wheelDelta) != wheelDelta){
                    // If not an int multiple of 120, then its touchpad scrolling.
                    // This can change very fast so just assume 1 wheel click to make it more manageable.
                    wheelDelta = evt.wheelDelta > 0 ? 1 : -1;
                }
                var scrollAmount = evt.detail ? (evt.detail * -1) : wheelDelta;
                if(scrollAmount !== 0){
                    var node = this[(scrollAmount > 0 ? "upArrowNode" : "downArrowNode" )];

                    this._arrowPressed(node, scrollAmount, this.smallDelta);

                    if(!this._wheelTimer){
                        clearTimeout(this._wheelTimer);
                    }
                    this._wheelTimer = setTimeout(lang.hitch(this,"_arrowReleased",node), 50);
                }
            }
        });
    }
);

相关问题