2017-08-30 65 views
0

如何確保當鼠標滾輪滾動時,圖表線的畫布可以用鼠標放大和縮小?如何在放大和縮小時處理當前數據

在圖表行的畫布中,我沒有使用chart.js或d3.js或其他庫。我期待它可以放大和縮小鼠標位置,但是這些值會隨着放大和縮小而改變。如何確保中心點 - 鼠標位置 - 不會受到座標系變化的影響。

+1

您使用'變換:規模()'來放大和縮小? – Ivan

回答

1

如果您使用CSS transform屬性scale(),則可以設置另一個稱爲transform-origin的規則。如果在應用scale()之前將其設置爲屏幕中心,則頁面將正確放大。

// performs the change of origin and scales the body element 
zoom(scale, mouse) { 

    return $('body').css('transformOrigin', getMouse(mouse)) 
     .css('transform', 'scale('+scale+')'); 

} 

// returns a string of the current center of screen 
getMouse() { 
    let midX = window.innerWidth/2; 
    let midY = window.innerHeight/2; 
    let x = $('body').css('left').split('p')[0]; 
    let y = $('body').css('top').split('p')[0]; 

    return String(midX - x + 'px ') + String(midY - y + 'px'); 
} 

然後你就可以實現滾動激活與bind()方法:

let scale = 1; 
let scrollFactor = -0.1; 
$(window).bind('wheel mousewheel', function(e){ 

    let scroll = e.originalEvent.deltaY; 
    let sign = Math.sign(scroll); 
    let newScale = Math.round((scale + scrollFactor * sign)*100)/100; 

    // you could set an if statement here before calling zoom() to 
    // set a lower and higher zoom limit 
    zoom(newScale, [e.pageX, e.pageY]); 


}); 
+0

我沒有使用CSS和變換。 我正在做圖表行的歷史數據,方法是更改​​圖表行中兩點之間的間距,並重新繪製它以獲取放大和縮小的結果。我想做一個這樣的演示:https://app.expertoption.com/ 我幾乎完成我的演示,除了這一點 - 我不知道如何處理當前的數據,可以使其放大和縮小鼠標位置。我只是讓它在屏幕左側放大和縮小。 –

+0

那麼如果你不給出一些詳細的信息**和一些代碼**,我們不能幫你 – Ivan

+0

好吧!謝謝反正:) –