gpt4 book ai didi

javascript - 如何更改自定义模板 JointJS 元素的 CSS?

转载 作者:行者123 更新时间:2023-11-28 06:16:49 25 4
gpt4 key购买 nike

我已经使用 JointJS 一段时间了,我正在尝试为我的元素创建一个 HTML 模板。所以我一直在使用 tutorial但它并没有放弃为我做这件事。我想要完成的事情是在执行某个操作时调整 HTML 元素的颜色,例如双击该元素。我确实注意到教程中文本的更改方式,但没有更改任何颜色的示例。

编辑

我试过这个来获得元素的起始颜色:

joint.shapes.html = {};
joint.shapes.html.OdinElement = joint.shapes.basic.Rect.extend({
defaults: joint.util.deepSupplement({
type: 'html.Element',
attrs: {
rect: { stroke: 'none', 'fill-opacity': 0 }
}
}, joint.shapes.basic.Rect.prototype.defaults)
});

// Create a custom view for that element that displays an HTML div above it.
// -------------------------------------------------------------------------

joint.shapes.html.OdinElementView = joint.dia.ElementView.extend({

template: [
'<div class="html-element">',
'<button class="delete">x</button>',
'<label></label>',
'<span></span>', '<br/>',
'</div>'
].join(''),

initialize: function() {
_.bindAll(this, 'updateBox');
joint.dia.ElementView.prototype.initialize.apply(this, arguments);

this.$box = $(_.template(this.template)());

this.$box.find('.delete').on('click', _.bind(this.model.remove, this.model));
// Update the box position whenever the underlying model changes.
this.model.on('change', this.updateBox, this);
// Remove the box when the model gets removed from the graph.
this.model.on('remove', this.removeBox, this);

this.updateBox();
},
render: function() {
joint.dia.ElementView.prototype.render.apply(this, arguments);
this.paper.$el.prepend(this.$box);
this.updateBox();
return this;
},
updateBox: function() {
// Set the position and dimension of the box so that it covers the JointJS element.
var bbox = this.model.getBBox();
// Example of updating the HTML with a data stored in the cell model.
this.$box.find('label').text(this.model.get('label'));

this.$box.css({ width: bbox.width, height: bbox.height, left: bbox.x, top: bbox.y, transform: 'rotate(' + (this.model.get('angle') || 0) + 'deg)', background: this.model.get('color')}); // I've tried to add it like a background
},
removeBox: function(evt) {
this.$box.remove();
}
});

//add a new element like this
new joint.shapes.html.OdinElement({
position: { x: 80, y: 80 },
size: { width: 200, height: 50 },
label: 'label',
color: '#ff0000'
});

我也试过将其设置为在标签中设置文本,但我不知道是否有相应的功能。

有人知道怎么做吗?

非常感谢!

蒂姆

最佳答案

我自己找到了答案!

显然不允许在 html 变量中创建与给定元素不同的元素。所以我不得不将 joint.shapes.html.OdinElement 更改为 joint.shapes.html.Elementjoint.shapes.html.OdinElementViewjoint.shapes.html.ElementView

现在一切正常:)

感谢您的帮助!

关于javascript - 如何更改自定义模板 JointJS 元素的 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com