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

时间:2011-11-21 12:51:21

标签: javascript events javascript-events dojo mousewheel

我在基于dojo的应用程序中使用了一些dijit.form.NumberSpinner wigets,所有这些都与onChange个动作相关联。

当一个区域有大量NumberSpinner s区域时会出现问题:用户在页面上滚动,并在整个区域内滚动鼠标滚轮时意外填充NumberSpinner字段时出现意外值。 / p>

是否有可能在dijit.form.NumberSpinner wigets上禁用鼠标滚轮事件?

2 个答案:

答案 0 :(得分:6)

如果您从不需要它,并且您可以访问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);
    });

});

答案 1 :(得分:1)

只有在窗口小部件具有焦点时才能使鼠标滚轮工作的快速而肮脏的方法是将if (!this.focused) return;添加到_mouseWheeleddijit/form/_Spinner.js的顶部。

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