亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 中的圖像放大鏡

Angular 中的圖像放大鏡

慕森王 2021-06-02 13:25:15
我正在嘗試在懸停時實現圖像放大鏡。我試圖復制 w3schools 中的代碼,這純粹是 Javascript。我正在嘗試在 Angular 中實現以下代碼https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_image_magnifier_glass我在打字稿中使用了上述方法,并從 Angular 中的 ngOnInit 調用它,但我無法從該方法中獲得任何結果。我確保正確傳遞了 id 并驗證了正在調用的方法。但仍然無法獲得任何結果結果。我不希望將任何 npm 包用于放大鏡,因為它們中的大多數都有錯誤。組件.tsngOnInit(){ 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;    }}
查看完整描述

2 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

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


查看完整回答
反對 回復 2021-06-03
  • 2 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號