2016-12-24 69 views
0

說,我想在mousewheeldown上增加一個SVG圓的半徑,並在mousewheeldown上減少,我該如何實現它?如何處理Angular2中的鼠標滾輪/停止事件?

<svg width="800" height="400"> 
    <circle 
    [attr.r]   ="cRadius" 
    (mousewheelup) ="cRadius = cRadius + 5" 
    (mousewheeldown) ="cRadius = cRadius - 5" 
    /> 
</svg> 

回答

2
@Directive({ selector: '[circle]' }) 
export class MouseWheelDirective { 
    @Output() mouseWheelUp = new EventEmitter(); 
    @Output() mouseWheelDown = new EventEmitter(); 

    @HostListener('mousewheel', ['$event']) onMouseWheelChrome(event: any) { 
    this.mouseWheelFunc(event); 
    } 

    @HostListener('DOMMouseScroll', ['$event']) onMouseWheelFirefox(event: any) { 
    this.mouseWheelFunc(event); 
    } 

    @HostListener('onmousewheel', ['$event']) onMouseWheelIE(event: any) { 
    this.mouseWheelFunc(event); 
    } 

    mouseWheelFunc(event: any) { 
    var event = window.event ; 
    var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail))); 
    if(delta > 0) { 
     this.mouseWheelUp.emit(event); 
    } else if(delta < 0) { 
     this.mouseWheelDown.emit(event); 
    } 
    } 
} 
+0

雖然,你可以刪除這些'老IE support'部分,因爲相關的版本不支持'svg'可言。 – naeramarth7