gpt4 book ai didi

javascript - Angular中的图像放大镜

转载 作者:行者123 更新时间:2023-12-04 20:27:30 24 4
gpt4 key购买 nike

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

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

我在 typescript 中使用了上述方法,并在 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};
}
}

最佳答案

这是根据您的要求进行的工作堆栈 Blitz 。它显示了 w3school 中提到的图像缩放功能的实现。
https://stackblitz.com/edit/angular-w3school-image-magnification

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

问题是 img-magnifier-glass div 元素是使用经典 DOM 方法“document.createElement”创建的。然后,再次使用经典的 DOM 方法 (setAttribute) 对其应用“img-magnifier-glass”类。但是,在 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 规则会完全禁用该规则的 View 封装。应用::ng-deep 的任何样式都将成为全局样式。

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

或者您可以通过指定来停止组件的样式封装
@Component({
// ...
encapsulation: ViewEncapsulation.None, //<<<<< this one!
styles: [
// ...
]
})

如果您使用 Renderer2 ( https://angular.io/api/core/Renderer2 ) 代替在这里创建动态元素(如玻璃元素)会更好。 Renderer2 将负责正确封装应用于使用它创建的元素的类。

关于javascript - Angular中的图像放大镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56180297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com