【问题标题】:Image magnifier in AngularAngular中的图像放大镜
【发布时间】:2019-10-04 10:12:03
【问题描述】:

我正在尝试在悬停时实现图像放大镜。我尝试像在 w3schools 中一样复制代码,这纯粹是 Javascript。我正在尝试在 Angular 中实现以下代码

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

我在 typescript 中使用了上述方法并在 Angular 中从 ngOnInit 调用它,但我无法从该方法中获得任何结果。我已确保正确传递了 id 并验证了该方法正在被调用。但仍然无法得到任何结果。我不希望将任何 npm 包用于放大镜,因为它们中的大多数都有错误。

component.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};
  }
}

【问题讨论】:

    标签: javascript css angular typescript


    【解决方案1】:

    根据您的要求,这是一个有效的堆栈闪电战。它显示了 w3school 中提到的图像缩放功能的实现。 https://stackblitz.com/edit/angular-w3school-image-magnification

    您尚未显示您的 html 和 css 文件。所以,我不完全确定,但以下可能是缩放不适合您的原因。

    问题是 img-magnifier-glass div 元素是使用经典 DOM 方法“document.createElement”创建的。然后,再次使用经典的 DOM 方法 (setAttribute) 对其应用“img-magnifier-glass”类。但是,在角度,样式被封装。因此,如果您在 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 将负责正确封装应用于使用它创建的元素的类。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但对于具有相同放大镜实现的图像缩放器。我通过调整 css 和 ts 代码(取自放大镜并与缩放器相结合)使其工作。上面的答案不是我要找的,因为我需要将缩放后的 img 放在另一个盒子里,而不是放在 img 本身的顶部。这是我修改的stackblitz 代码以满足我的需要。

      【讨论】:

        【解决方案3】:

        要在 Angular 中实现 W3School 和 Amazon 等图像放大功能,您可以使用 npm 包ng-img-magnifier。 这是工作的DEMO

         <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>
        

        此软件包带有完整的自定义选项。

        希望这能解决您的问题。

        【讨论】:

          猜你喜欢
          • 2011-08-29
          • 2014-11-29
          • 1970-01-01
          • 2020-12-02
          • 2012-10-21
          • 2012-03-18
          • 1970-01-01
          • 2013-02-25
          相关资源
          最近更新 更多