var that = this; function DragObj(selector) { //保存节点 this.ele = $(selector).get(0); //调用startDrag this.startDrag(); } /*封装startDrag方法*/ DragObj.prototype.startDrag = function () { var self = this; //给当前对象的节点添加mousedown事件,鼠标按下事件后执行滑动事件 $(self.ele).on({ mousedown: function () { self.drag(); } }) } /*封装滑动事件*/ DragObj.prototype.drag = function () { var self = this; /*初始化self.newDisance*/ self.newDisance = { X:that.vtkObj['imageActorI'].getProperty().getColorLevel(), Y:that.vtkObj['imageActorI'].getProperty().getColorWindow() } //给X窗口对象添加移动和结束事件 $('#contentX').on({ /*根据移动的距离来判断器是增加灰度还是减小灰度,左右为wl,上下为ww*/ mousemove: function (oEvent) { if(self.disance && self.disance.X){ self.newDisance.X = that.vtkObj['imageActorI'].getProperty().getColorLevel() + (oEvent.pageX - self.disance.X); self.newDisance.Y = that.vtkObj['imageActorI'].getProperty().getColorWindow() + (oEvent.pageY - self.disance.Y); } self.disance = { X: oEvent.pageX, Y: oEvent.pageY }; //矢状面,冠状面,轴向面同时改变窗位和窗宽 ['X','Y','Z'].forEach((type) => { that.vtkObj['imageActor' + that.typeMap[type]].getProperty().setColorLevel(self.newDisance.X); that.vtkObj['imageActor' + that.typeMap[type]].getProperty().setColorWindow(self.newDisance.Y); }) $(".colorLevel").val(self.newDisance.X); $(".colorWindow").val(self.newDisance.Y); }, mouseup: function () { /*放开鼠标后重置距离,让其重新计算*/ self.disance = {}; self.newDisance = { X:that.vtkObj['imageActorI'].getProperty().getColorLevel(), Y:that.vtkObj['imageActorI'].getProperty().getColorWindow() }; $("#contentX").off('mousemove mouseup'); } }) } new DragObj('#contentX');