第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

鼠標(biāo)懸??s放圖像僅在鼠標(biāo)懸停時顯示

鼠標(biāo)懸??s放圖像僅在鼠標(biāo)懸停時顯示

紅糖糍粑 2022-12-09 13:53:53
我制作了這個鼠標(biāo)懸停圖像縮放頁面。但我希望輸出顯示在左側(cè),并且僅當(dāng)有人將鼠標(biāo)懸停在其上時。在此代碼中,輸出窗口不斷打開。這是我的代碼:function imageZoom(imgID, resultID) {  var img, lens, result, cx, cy;  img = document.getElementById(imgID);  result = document.getElementById(resultID);  /*create lens:*/  lens = document.createElement("DIV");  lens.setAttribute("class", "img-zoom-lens");  /*insert lens:*/  img.parentElement.insertBefore(lens, img);  /*calculate the ratio between result DIV and lens:*/  cx = result.offsetWidth / lens.offsetWidth;  cy = result.offsetHeight / lens.offsetHeight;  /*set background properties for the result DIV:*/  result.style.backgroundImage = "url('" + img.src + "')";  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";  /*execute a function when someone moves the cursor over the image, or the lens:*/  lens.addEventListener("mousemove", moveLens);  img.addEventListener("mousemove", moveLens);  /*and also for touch screens:*/  lens.addEventListener("touchmove", moveLens);  img.addEventListener("touchmove", moveLens);  function moveLens(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);    /*calculate the position of the lens:*/    x = pos.x - (lens.offsetWidth / 2);    y = pos.y - (lens.offsetHeight / 2);    /*prevent the lens from being positioned outside the image:*/    if (x > img.width - lens.offsetWidth) {      x = img.width - lens.offsetWidth;    }    if (x < 0) {      x = 0;    }    if (y > img.height - lens.offsetHeight) {      y = img.height - lens.offsetHeight;    }    if (y < 0) {      y = 0;    }    /*set the position of the lens:*/    lens.style.left = x + "px";    lens.style.top = y + "px";    /*display what the lens "sees":*/    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";  }該代碼運行良好,但結(jié)果應(yīng)該在右側(cè),并且僅當(dāng)用戶將鼠標(biāo)懸停在其上時才顯示。
查看完整描述

2 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗 獲得超5個贊

下面我修改了你的 CSS。display: flex是你所追求的。


然后我讓你的代碼隱藏result元素并只在mouseover. 然后我讓它隱藏在mouseout.


您可以調(diào)整 CSS 等以匹配您的風(fēng)格,但這是我為您準(zhǔn)備的片段;


function imageZoom(imgID, resultID) {

  var img, lens, result, cx, cy;

  img = document.getElementById(imgID);

  result = document.getElementById(resultID);

  /*create lens:*/

  lens = document.createElement("DIV");

  lens.setAttribute("class", "img-zoom-lens");

  /*insert lens:*/

  img.parentElement.insertBefore(lens, img);

  /*calculate the ratio between result DIV and lens:*/

  cx = result.offsetWidth / lens.offsetWidth;

  cy = result.offsetHeight / lens.offsetHeight;

  /*set background properties for the result DIV:*/

  result.style.backgroundImage = "url('" + img.src + "')";

  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";

  /*execute a function when someone moves the cursor over the image, or the lens:*/

  lens.addEventListener("mousemove", moveLens);

  img.addEventListener("mousemove", moveLens);

  /*and also for touch screens:*/

  lens.addEventListener("touchmove", moveLens);

  img.addEventListener("touchmove", moveLens);

  

  /*initialise and hide lens result*/

  result.style.display = "none";

  /*Reveal and hide on mouseover or out*/

  lens.onmouseover = function(){result.style.display = "block";};

  lens.onmouseout = function(){result.style.display = "none";};

  

  function moveLens(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);

    /*calculate the position of the lens:*/

    x = pos.x - (lens.offsetWidth / 2);

    y = pos.y - (lens.offsetHeight / 2);

    /*prevent the lens from being positioned outside the image:*/

    if (x > img.width - lens.offsetWidth) {x = img.width - lens.offsetWidth;}

    if (x < 0) {x = 0;}

    if (y > img.height - lens.offsetHeight) {y = img.height - lens.offsetHeight;}

    if (y < 0) {y = 0;}

    /*set the position of the lens:*/

    lens.style.left = x + "px";

    lens.style.top = y + "px";

    /*display what the lens "sees":*/

    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "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};

  }

};


imageZoom("myimage", "myresult");

* {box-sizing: border-box;}


.img-zoom-container {

  position: relative;

  display: flex;

}


.img-zoom-lens {

  position: absolute;

  border: 1px solid #d4d4d4;

  /*set the size of the lens:*/

  width: 40px;

  height: 40px;

}


.img-zoom-result {

  border: 1px solid #d4d4d4;

  position: absolute;

  left: 300px; /*match width of #myimage*/

  /*set the size of the result div:*/

  width: 300px;

  height: 300px;

}

<h1>Image Zoom</h1>


<p>Mouse over the image:</p>


<div class="img-zoom-container">

  <img id="myimage" src="https://hatrabbits.com/wp-content/uploads/2018/10/risky-assumptions.jpg" width="300" height="240">

  <div id="myresult" class="img-zoom-result" style=""></div>

</div>


<p>The image must be placed inside a container with relative positioning.</p>

<p>The result can be put anywhere on the page, but must have the class name "img-zoom-result".</p>

<p>Make sure both the image and the result have IDs. These IDs are used when a javaScript initiates the zoom effect.</p>


如果您希望我這樣做,我可以努力使鏡頭結(jié)果絕對定位并對齊,這樣您就不會在底部看到文字跳動。除非你知道如何做所有這些——希望這就足夠了?


查看完整回答
反對 回復(fù) 2022-12-09
?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗 獲得超8個贊

默認(rèn)情況下不透明度:0;并在 mouseenter 上通過設(shè)置不透明度使其可見:1;


function imageZoom(imgID, resultID) {

  var img, lens, result, cx, cy;

  img = document.getElementById(imgID);

  result = document.getElementById(resultID);

  /*create lens:*/

  lens = document.createElement("DIV");

  lens.setAttribute("class", "img-zoom-lens");

  /*insert lens:*/

  img.parentElement.insertBefore(lens, img);

  /*calculate the ratio between result DIV and lens:*/

  cx = result.offsetWidth / lens.offsetWidth;

  cy = result.offsetHeight / lens.offsetHeight;

  /*set background properties for the result DIV:*/

  result.style.backgroundImage = "url('" + img.src + "')";

  result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";

  /*execute a function when someone moves the cursor over the image, or the lens:*/

  lens.addEventListener("mousemove", moveLens);

  img.addEventListener("mousemove", moveLens);

  /*and also for touch screens:*/

  lens.addEventListener("touchmove", moveLens);

  img.addEventListener("touchmove", moveLens);


  function moveLens(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);

    /*calculate the position of the lens:*/

    x = pos.x - (lens.offsetWidth / 2);

    y = pos.y - (lens.offsetHeight / 2);

    /*prevent the lens from being positioned outside the image:*/

    if (x > img.width - lens.offsetWidth) {

      x = img.width - lens.offsetWidth;

    }

    if (x < 0) {

      x = 0;

    }

    if (y > img.height - lens.offsetHeight) {

      y = img.height - lens.offsetHeight;

    }

    if (y < 0) {

      y = 0;

    }

    /*set the position of the lens:*/

    lens.style.left = x + "px";

    lens.style.top = y + "px";

    /*display what the lens "sees":*/

    result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "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

    };

  }

}

imageZoom("myimage", "myresult");


function show(x) {

  document.getElementById('myresult').style.opacity = 1;

}

* {

  box-sizing: border-box;

}


.img-zoom-container {

  position: relative;

  display: flex;

}


.img-zoom-lens {

  position: absolute;

  border: 1px solid #d4d4d4;

  /*set the size of the lens:*/

  width: 70px;

  height: 70px;

}


#myresult {

  opacity: 0;

}


.img-zoom-result {

  border: 1px solid #d4d4d4;

  /*set the size of the result div:*/

  width: 300px;

  height: auto;

}

<h1>Image Zoom</h1>


<p>Mouse over the image:</p>


<div class="img-zoom-container">

  <img onmouseenter="show()" id="myimage" src="https://i.pinimg.com/originals/f8/b6/9e/f8b69e6156999b84137f3f0a23701b75.jpg" width="300" height="240">

  <div id="myresult" class="img-zoom-result"></div>

</div>


<p>The image must be placed inside a container with relative positioning.</p>

<p>The result can be put anywhere on the page, but must have the class name "img-zoom-result".</p>

<p>Make sure both the image and the result have IDs. These IDs are used when a javaScript initiates the zoom effect.</p>

// Initiate zoom effect:


查看完整回答
反對 回復(fù) 2022-12-09
  • 2 回答
  • 0 關(guān)注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號