为什么我的放大镜元素中的图像没有更新?

时间:2018-10-19 18:30:04

标签: javascript angular

我的component.ts是:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BusquedaService } from '../../services/busqueda.service';
import { UsuarioService } from '../../services/usuario.service';
// import { ZoomService } from '../../services/zoom.service';

// declare function magnify();

@Component({
  selector: 'app-adds-single-view',
  templateUrl: './adds-single-view.component.html',
  styles: []
})
export class AddsSingleViewComponent implements OnInit {

  postId: string;
  category: any;
  postItem: any;
  articulo: any;
  imgID = 'myimage';

  constructor(private route: ActivatedRoute,
              public busqueda: BusquedaService,
              public _usuario: UsuarioService) {

    this.postId = this.route.snapshot.queryParams['postId'];
    this.category = this.route.snapshot.queryParams['cat'];

  }

  ngOnInit() {
    setTimeout(function() {
      this.magnify();
    }, 2000);

    this.busqueda.busquedaEspecifica('ads-single-view', this.category, this.postId )
                 .subscribe((resp: any) => {
                    this.articulo = resp.articulos;
                    console.log(this.articulo);
                 });
  }

  changeFirstImage(data: any, data2: any) {
    const firstImgId = data;
    firstImgId.src = data2.src;
    this.imgID = data2.id;
    // const imgNewId = data.target.src;
    // let myImageSrc = document.getElementById('myimage');
  }

  magnify() {

    const zoom = 3;

    let img, glass, w, h, bw;
    img = document.getElementById(this.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};
    }
  }
}

该插件可以工作,但是我有一个问题,当我更改图像时,变焦镜头会一直显示第一张图像: image

image

在执行更改图像的同一功能内执行执行创建放大镜的功能的探针,但仅复制显示第一张图像和当前图像的放大镜:

image

0 个答案:

没有答案