5

有没有人找到添加tooltips到 dat.gui 条目的方法?

似乎没有class分配给他们,所以我该如何选择它们?

4

1 回答 1

6

您需要自己将此功能添加到 dat.GUI。以下是一些指南和示例解决方案。

条目用从类继承的Controller类表示。每个控制器都有一个私有成员__li,代表它<li>在 gui 中的元素。您可以扩展Controller原型并添加一个title函数,该函数将添加、更新或删除__li's title 属性。

这是片段:

/* dat.GUI copies the prototype of superclass Controller to all other controllers, so it is not enough to add it only to 
the super class as the reference is not maintained */
var eachController = function(fnc) {
  for (var controllerName in dat.controllers) {
    if (dat.controllers.hasOwnProperty(controllerName)) {
      fnc(dat.controllers[controllerName]);
    }
  }
}

var setTitle = function(v) {
  // __li is the root dom element of each controller
  if (v) {
    this.__li.setAttribute('title', v);
  } else {
    this.__li.removeAttribute('title')
  }
  return this;
};

eachController(function(controller) {
  if (!controller.prototype.hasOwnProperty('title')) {
    controller.prototype.title = setTitle;
  }
});


// demo
var Dummy = function() {
  this.foo = 12
};
Dummy.prototype.bar = function() {
  console.log('1');
};

var gui = new dat.GUI();
var d = new Dummy();
gui.add(d, 'foo').name('Foo').title('Foo\'s title');
gui.add(d, 'bar', 1, 13, 1).name('Bar').title('Bar\'s title');
<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
</head>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
</body>

</html>

于 2015-04-10T14:17:10.283 回答