我正在嘗試在懸停時實現圖像放大鏡。我試圖復制 w3schools 中的代碼,這純粹是 Javascript。我正在嘗試在 Angular 中實現以下代碼https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_image_magnifier_glass我在打字稿中使用了上述方法,并從 Angular 中的 ngOnInit 調用它,但我無法從該方法中獲得任何結果。我確保正確傳遞了 id 并驗證了正在調用的方法。但仍然無法獲得任何結果結果。我不希望將任何 npm 包用于放大鏡,因為它們中的大多數都有錯誤。組件.tsngOnInit(){ this.magnify(imgID, zoom) }magnify(imgID, zoom) { var img, glass, w, h, bw; img = document.getElementById(imgID); /*create magnifier glass:*/ glass = document.createElement("DIV"); glass.setAttribute("class", "img-magnifier-glass"); /*insert magnifier glass:*/ img.parentElement.insertBefore(glass, img); /*set background properties for the magnifier glass:*/ glass.style.backgroundImage = "url('" + img.src + "')"; glass.style.backgroundRepeat = "no-repeat"; glass.style.backgroundSize = (img.width * zoom) + "px " + (img.height * zoom) + "px"; bw = 3; w = glass.offsetWidth / 2; h = glass.offsetHeight / 2; /*execute a function when someone moves the magnifier glass over the image:*/ glass.addEventListener("mousemove", moveMagnifier); img.addEventListener("mousemove", moveMagnifier); /*and also for touch screens:*/ glass.addEventListener("touchmove", moveMagnifier); img.addEventListener("touchmove", moveMagnifier); function moveMagnifier(e) { var pos, x, y; /*prevent any other actions that may occur when moving over the image*/ e.preventDefault(); /*get the cursor's x and y positions:*/ pos = getCursorPos(e); x = pos.x; y = pos.y; }}
Angular 中的圖像放大鏡
慕森王
2021-06-02 13:25:15