0
我想在圖像內部創建onmousemove事件,然後在右側顯示放大的圖像。純Javascript如何放大圖片在mousemove?
問題:
- 當我嘗試在圖像內鼠標移動,黑匣子和縮放圖像的閃爍。
- 放大的圖像在黑匣子中不匹配。
我該怎麼做?可能計算錯誤。
這是我的代碼。
<div>
<div id="cursor" style="background:black; opacity:0.5; width:100px; height:100px; position:absolute; display:none">
</div>
<div style="float:left;">
<img id="img" src="http://www.animenewsnetwork.com/thumbnails/cover400x200/cms/news/102950/26902290903_3c8f2db0ea_b.jpg" onmousemove="getPos(event)" onmouseout="stopTracking()"/>
</div>
<div id="zoom" style="width:300px; height:300px; zoom:1; float:left;">
qwe
</div>
</div>
<script>
function getPos(e){
var x = e.clientX;
var y = e.clientY;
var width = document.getElementById("img").clientWidth;
var height = document.getElementById("img").clientHeight;
document.getElementById("cursor").style.left = x - 50;
document.getElementById("cursor").style.top = y - 50;
document.getElementById("cursor").style.display = "inline-block";
document.getElementById("zoom").style.background = "url('http://www.animenewsnetwork.com/thumbnails/cover400x200/cms/news/102950/26902290903_3c8f2db0ea_b.jpg') "+x/width * 100+"% "+y/width * 100+"%";
}
function stopTracking(){
document.getElementById("cursor").style.display = "none";
document.getElementById("zoom").style.background = "transparent";
}
</script>
Thanks in Advanced。
謝謝Shah :) – Enalds
@Enalds你必須接受答案作爲正確的答案。也許你對這個主題感興趣,以專業的方式放大圖像:http://stackoverflow.com/questions/38703966/zoom-on-an-image-on-mouse-move-reaching-all-corners –
@MarcosPérezGude好吧謝謝。 – Enalds