簡體   English   中英

Angular 中的圖像放大鏡

[英]Image magnifier in Angular

我正在嘗試在懸停時實現圖像放大鏡。我試圖復制 w3schools 中的代碼,這純粹是 Javascript。我正在嘗試在 Angular 中實現以下代碼

https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_image_magnifier_glass

我在打字稿中使用了上述方法,並從 Angular 中的 ngOnInit 調用它,但我無法從該方法中獲得任何結果。我確保正確傳遞了 id 並驗證了正在調用的方法。但仍然無法獲得任何結果結果。我不希望將任何 npm 包用於放大鏡,因為它們中的大多數都有錯誤。

組件.ts

ngOnInit(){

 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;
    /*prevent the magnifier glass from being positioned outside the image:*/
    if (x > img.width - (w / zoom)) {x = img.width - (w / zoom);}
    if (x < w / zoom) {x = w / zoom;}
    if (y > img.height - (h / zoom)) {y = img.height - (h / zoom);}
    if (y < h / zoom) {y = h / zoom;}
    /*set the position of the magnifier glass:*/
    glass.style.left = (x - w) + "px";
    glass.style.top = (y - h) + "px";
    /*display what the magnifier glass "sees":*/
    glass.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
  }
  function getCursorPos(e) {
    var a, x = 0, y = 0;
    e = e || window.event;
    /*get the x and y positions of the image:*/
    a = img.getBoundingClientRect();
    /*calculate the cursor's x and y coordinates, relative to the image:*/
    x = e.pageX - a.left;
    y = e.pageY - a.top;
    /*consider any page scrolling:*/
    x = x - window.pageXOffset;
    y = y - window.pageYOffset;
    return {x : x, y : y};
  }
}

這是根據您的要求進行的有效堆棧閃電戰。 它顯示了 w3school 上提到的圖像縮放功能的實現。 https://stackblitz.com/edit/angular-w3school-image-magnification

您還沒有顯示您的 html 和 css 文件。 所以,我不完全確定,但以下可能是縮放對您不起作用的原因。

問題是 img-magnifier-glass div 元素是使用經典的 DOM 方法“document.createElement”創建的。 然后,類 'img-magnifier-glass' 應用於它,再次使用經典的 DOM 方法(setAttribute)。 但是,在 angular 中,樣式是被封裝的。 因此,如果您在 app.component.css 中添加了 '.img-magnifier-glass' 的類定義,那么該類將不會應用於 glass div,因為模板 (app.component.html) 中未提及它. 請參閱此了解更多信息 - https://github.com/angular/angular/issues/7845

要解決此問題,您可以將類 '.img-magnifier-glass' 的定義移動到 styles.css(定義全局樣式的位置)

或者您可以將類保留在 app.component.css 中,但使用偽選擇器 ::ng-deep 。 將 ::ng-deep 偽類應用於任何 CSS 規則會完全禁用該規則的視圖封裝。 任何應用了 ::ng-deep 的樣式都會成為全局樣式。

::ng-deep .img-magnifier-glass {
}

或者您可以通過指定來停止組件的樣式封裝

@Component({
// ...
encapsulation: ViewEncapsulation.None, //<<<<< this one!
styles: [
  // ...
]
})

如果您使用 Renderer2 ( https://angular.io/api/core/Renderer2 ) 而不是在這里創建玻璃元素之類的動態元素會更好。 Renderer2 將負責正確封裝應用於使用它創建的元素的類。

我遇到了同樣的問題,但對於具有相同放大鏡實現的圖像縮放器。 我通過調整 css 和 ts 代碼(取自放大鏡並與縮放器結合)使其工作。 上面的答案不是我正在尋找的,因為我需要將放大的 img 放在不同的框中,而不是在 img 本身的頂部。 這是我修改后適合我需要的stackblitz代碼。

要在 Angular 中實現 W3School 和 Amazon 等圖像放大功能,您可以使用 npm 包ng-img-magnifier

 <ng-img-magnifier
        [thumbImage]='img' [fullImage]='img2'
        [top]='top' [right]='right'
        [lensWidth]='lensewidth' [lensHeight]='lensheight'
        [imgWidth]='imgWidth' [imgHeight]='imgheight'
        [resultWidth]='resultWidth' [resultHeight]='resultheight'
        >
 </ng-img-magnifier>

該軟件包帶有完整的自定義選項。

希望這將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM