您应该使用计算的 observables 来计算旋转,如下所示(小提琴:http: //jsfiddle.net/6f4X3/6/):
html:
height: <input type="text" data-bind="value:actualData, valueUpdate:'afterkeydown'" /><br />
rotationAngle: <input type="text" data-bind="value:rotationAngle, valueUpdate:'afterkeydown'" /><br />
rotationPointX: <input type="text" data-bind="value:rotationPointX, valueUpdate:'afterkeydown'" /><br />
rotationPointY: <input type="text" data-bind="value:rotationPointY, valueUpdate:'afterkeydown'" /><br />
<svg>
<rect data-bind='attr:{ transform:rotation, height:data() < 0 ? 0 : data()}'
width="20" x="70" y="40" fill="#38ACEC"></rect>
</svg>
js:
var VM = function(){
var self = this;
self.actualData = ko.observable(100);
self.data = ko.computed(function(){
return parseInt(self.actualData(), 10);
}, self);
self.rotationAngle = ko.observable(20);
self.rotationPointX = ko.observable(10);
self.rotationPointY = ko.observable(10);
self.rotation = ko.computed(function(){
if (self.data() <= 0) {
return "rotate(180 0 0)";
} else {
var r = "rotate(" + self.rotationAngle() + " " + self.rotationPointX() + " " + self.rotationPointY() +")";
console.log(r);
return r;
}
}, self);
}
ko.applyBindings(new VM());