簡體   English   中英

如何將背景圖片放在前面?

[英]How to bring the background image in front?

這是我的產品圖像查看器組件。html。 在這個組件中,模板引用變量#divZoomed 用於顯示縮放。

<div style="position: sticky; top: 56px;">
  <div class="row no-gutters">
    <div class="col-md-2">
      <div class="flex flex-column justify-content-center">
        <div class="image-border" *ngFor="let image of images">
          <div
            [style.backgroundImage]="'url(' + image + ')'"
            style="
              padding: 5px;
              width: 100%;
              height: 100%;
              background-repeat: no-repeat;
              background-position: 50%;
              background-size: contain;
              height: 64px;
            "
            (click)="getCurrentImage(image)"
          ></div>
        </div>
      </div>
    </div>

    <div class="col-md-10">
      <div style="border: 1px solid #f0f0f0;">
        <app-zoom [img]="currentImage" [zoom]="2" [lenSize]="50" [divZoomed]="divZoomed"></app-zoom>

        <div
          #divZoomed
          class="img-zoom-result"
          style="left: 405.341px; width: 736.673px; height: 425.105px;"
        ></div>

        <div class="mt-3"></div>

        <button class="add-to-card">
          <i class="fas fa-shopping-cart"></i>
          Add To Basket
        </button>
      </div>
    </div>
  </div>
</div>

這是我的產品圖片查看器組件。css



.image-border {
  border-right: none !important;
  border: 1px solid #f0f0f0;
  border-bottom: none;
  cursor: pointer;
}

.add-to-card {
  padding: 18px 8px;
  border-radius: 2px;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);
  width: 60%;
  border: none;
  float: left;
  background: #ff9f00;
  color: #fff;
  text-transform: uppercase;
  font-size: 16px;
  font-weight: 500;
  transition: box-shadow 0.2s ease;
  vertical-align: super;
  cursor: pointer;
}

.img-zoom-result {
  border: 1px solid #d4d4d4;
  left: 405.341px;
  width: 736.673px;
  height: 234.009px;
  z-index: 1000000000;
  position: absolute;
  top: 0;
  overflow: hidden;
  background: #fff;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
  box-shadow: 0 4px 20px 2px rgba(0, 0, 0, 0.2);
  pointer-events: none;
  opacity: 0;
}

這是我的產品圖片查看器 component.ts

import { Component } from '@angular/core'

@Component({
  selector: 'app-product-image-viewer',
  templateUrl: './product-image-viewer.component.html',
  styleUrls: ['./product-image-viewer.component.scss'],
})
export class ProductImageViewerComponent {
  images = [
    'https://www.bigbasket.com/media/uploads/p/l/20000979_10-fresho-palak.jpg',
    'https://www.bigbasket.com/media/uploads/p/l/10000148_28-fresho-onion.jpg',
    'https://www.bigbasket.com/media/uploads/p/l/10000293_12-fresho-amla.jpg',
  ]

  currentImage: string = this.images[0]

  getCurrentImage(imageUrl: string) {
    this.currentImage = imageUrl
  }
}

這是我的縮放組件。html



<div class="img-zoom-container">
  <img
    #image
    [style.width]="yet && imgWidth ? imgWidth + 'px' : null"
    [style.heigth]="yet && imgHeigth ? imgHeigth + 'px' : null"
    id="myimage"
    [src]="imagen"
    (load)="onLoad()"
  />

  <div
    #lens
    [style.width]="lenSize + 'px'"
    [style.height]="lenSize + 'px'"
    [style.left]="posX + 'px'"
    [style.top]="posY + 'px'"
    class="img-zoom-lens"
  ></div>
</div>

這是我的縮放組件。css

.img-zoom-container {
  position: relative;
}

.img-zoom-lens {
  position: absolute;
  border: 1px solid #d4d4d4;
}

這是我的縮放組件.ts。 在這個組件中,渲染器用於設置樣式。 我將 viewchild 用於#lens 和 #image



import {
  Component,
  OnInit,
  Input,
  ElementRef,
  ViewChild,
  HostListener,
  Renderer2,
} from '@angular/core'

@Component({
  selector: 'app-zoom',
  templateUrl: './zoom.component.html',
  styleUrls: ['./zoom.component.scss'],
})
export class ZoomComponent implements OnInit {
  ngOnInit() {}

  imageTag: Element

  @Input('img') imagen: string
  @Input() zoom = 2
  @Input() lenSize = 40
  @Input() imgWidth
  @Input() imgHeigth
  @Input() divZoomed: ElementRef
  //@Input() imageResult: ElementRef

  result

  posX: number = 0
  posY: number = 0
  cx: number = 1
  cy: number = 1
  yet: boolean = false
  factorX: number
  factorY: number

  @ViewChild('image', { static: false, read: ElementRef }) image
  @ViewChild('lens', { static: false, read: ElementRef }) lens

  @HostListener('mousemove', ['$event'])
  mouseMove(event: any) {
    const result = this.moveLens(event)

    this.render.setStyle(this.divZoomed, 'background-position', result)

    this.render.setStyle(this.divZoomed, 'opacity', 1)
  }

  @HostListener('mouseleave', ['$event'])
  mouseLeave(event: any) {
    this.render.setStyle(this.divZoomed, 'opacity', 0)
  }

  constructor(private render: Renderer2) {}

  onLoad() {
    this.render.setStyle(this.divZoomed, 'background-image', "url('" + this.imagen + "')")

    this.render.setStyle(
      this.divZoomed,
      'background-size',
      this.image.nativeElement.width * this.zoom +
        'px ' +
        this.image.nativeElement.height * this.zoom +
        'px'
    )

    this.render.setStyle(this.divZoomed, 'background-repeat', 'no-repeat')
    this.render.setStyle(this.divZoomed, 'transition', 'background-position .2s ease-out')

    this.factorX = this.image.nativeElement.width
    this.factorY = this.image.nativeElement.height
    this.yet = true

    setTimeout(() => {
      this.factorX =
        this.imgWidth || this.imgHeigth ? this.factorX / this.image.nativeElement.width : 1

      this.factorY =
        this.imgWidth || this.imgHeigth ? this.factorY / this.image.nativeElement.height : 1

      const dim = (this.divZoomed as any).getBoundingClientRect()

      this.cx =
        (dim.width - this.image.nativeElement.width * this.zoom * this.factorX) /
        (this.image.nativeElement.width - this.lens.nativeElement.offsetWidth)

      this.cy =
        (dim.height - this.image.nativeElement.height * this.zoom * this.factorY) /
        (this.image.nativeElement.height - this.lens.nativeElement.offsetHeight)
    })
  }

  moveLens(e: any) {
    let pos
    let x
    let y

    e.preventDefault()
    pos = this.getCursorPos(e)
    x = pos.x - this.lens.nativeElement.offsetWidth / 2
    y = pos.y - this.lens.nativeElement.offsetHeight / 2

    if (x > this.image.nativeElement.width - this.lens.nativeElement.offsetWidth) {
      x = this.image.nativeElement.width - this.lens.nativeElement.offsetWidth
    }

    if (x < 0) {
      x = 0
    }

    if (y > this.image.nativeElement.height - this.lens.nativeElement.offsetHeight) {
      y = this.image.nativeElement.height - this.lens.nativeElement.offsetHeight
    }

    if (y < 0) {
      y = 0
    }

    this.posX = x
    this.posY = y
    let result = x * this.cx + 'px ' + y * this.cy + 'px'

    return result
  }

  getCursorPos(e: any) {
    let a
    let x = 0
    let y = 0

    e = e || window.event
    a = this.image.nativeElement.getBoundingClientRect()
    x = e.pageX - a.left
    y = e.pageY - a.top
    x = x - window.pageXOffset
    y = y - window.pageYOffset

    return { x: x, y: y }
  }
}

這很好用。我無法將背景圖像放在前面。 我使用了zindex,它不起作用。有沒有其他方法可以做到這一點?

這是產品詳細信息圖片

我為這個問題創建了 stackblitz。

https://stackblitz.com/edit/angular-ivy-tuw2xg?file=src%2Fapp%2Fapp.component.html

您不能直接在縮放組件上設置z-index ,因為它在 DOM 中的級別比文本更深。 您需要在 DOM 中將 z-index 設置得更高。

我只是將它設置在col-md-5容器中

  <div class="col-md-5" style=";z-index:10">
   <app-a ></app-a>
  </div>

  <div class="col-md-7">
    Your product details

Stackblitz 演示

暫無
暫無

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

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