如何在dijit / form / Horizo​​ntalSlider上检测onMoveStart和onMoveStop?

时间:2016-03-23 08:41:24

标签: javascript dojo dijit.form

我正在使用dojo 1.10.4,我需要检测"内部"滑块元素中的事件onMouseStartonMouseStop。在我的dijit/form/HorizontalSlider内。

目前我正在尝试检测对象_movable(它是dojo/dnd/Moveable),但没有成功,收到错误:

Moveable.js:11 Uncaught TypeError: Cannot read property 'style' of undefined
  • 如何在Horizo​​ntalSlider上收听onMouseStartonMouseStop
  • 或者您知道在用户开始结束时滑动的更好方法。

实例: https://jsfiddle.net/9uo1doan/

require([
  "dojo/dom",
  "dijit/form/HorizontalSlider",
  "dijit/form/TextBox",
  "dojo/domReady!"
], function(dom, HorizontalSlider, TextBox) {
  var slider = new HorizontalSlider({
    name: "slider",
    value: 5,
    minimum: -10,
    maximum: 10,
    intermediateChanges: true,
    style: "width:300px;",
    onChange: function(value) {
      dom.byId("sliderValue").value = value;
    }
  }, "slider");

  slider._movable.onMoveStart(function(event) {
    console.log('onMoveStart');
  });
  slider._movable.onMoveStop(function(event) {
    console.log('onMoveStop');
  });
  slider._movable.onFirstMove(function(event) {
    console.log('onFirstMove');
  });
  slider._movable.onMove(function(event) {
    console.log('onMove');
  });
  slider._movable.onMoving(function(event) {
    console.log('onMoving');
  });
  slider._movable.onMoved(function(event) {
    console.log('onMoved');
  });

});
<div id="slider"></div>
<p>
  <input type="text" id="sliderValue" data-dojo-type="dijit/form/TextBox" />

参考:https://dojotoolkit.org/reference-guide/1.10/dojo/dnd/Moveable.html

我也尝试过没有成功:

  on(slider._movable, 'moveStart', function(e){});

1 个答案:

答案 0 :(得分:1)

我能够使用dojo/aspect解决此问题。

基本上我找到的解决方案是在onMoveStart上执行方法dojo/dnd/Moveable后运行我的函数。

我仍然有兴趣了解为什么使用事件监听器的更传统方法无效。

现场演示:https://jsfiddle.net/8z6sm14j/

require([
  "dojo/dom",
  "dojo/on",
  "dojo/aspect",
  "dijit/form/HorizontalSlider",
  "dijit/form/TextBox",
  "dojo/domReady!"
], function(dom, on, aspect, HorizontalSlider, TextBox) {
  debugger
  var slider = new HorizontalSlider({
    name: "slider",
    value: 5,
    minimum: -10,
    maximum: 10,
    intermediateChanges: true,
    style: "width:300px;",
    onChange: function(value) {
      dom.byId("sliderValue").value = value;
    }
  }, "slider");
    var result = document.getElementById('result');

  aspect.after(slider._movable, 'onMoveStart', function(event) {
    console.log('onMoveStart');
    result.innerHTML = 'onMoveStart';
  });

  aspect.after(slider._movable, 'onMoveStop', function(event) {
    console.log('onMoveStop');
    result.innerHTML = 'onMoveStop';
  });

});
<div id="slider"></div>
<input type="text" id="sliderValue" data-dojo-type="dijit/form/TextBox" />
<div id="result"></div>

仅供参考,在查看this module之后,我能够提出这个解决方案。