欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)淘寶瀏覽商品放大鏡功能

 更新時(shí)間:2020年10月28日 11:31:42   作者:h03580  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)淘寶瀏覽商品放大鏡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)淘寶瀏覽商品放大鏡的具體代碼,供大家參考,具體內(nèi)容如下

1、準(zhǔn)備兩張圖片,其中一張圖片分辨率為另一張圖片的二倍。

2、前端頁(yè)面布局

//box1位左側(cè)原圖,box2為右側(cè)放大圖額顯示框,son為iv class="box1">
 <div class="son"></div>
 <div class="ceng"></div>
</div>
<div class="box2"></div>

3、js寫(xiě)邏輯

let box1=document.querySelector(".box1");
 let box2=document.querySelector(".box2");
 let son=document.querySelector(".son");
 let ceng=document.querySelector(".ceng");
 //鼠標(biāo)移入:son,box2出現(xiàn)
 ceng.onmouseenter=function(){
  son.style.display="block"
  box2.style.display="block"
 }
 //鼠標(biāo)移出:son,box2消失
 ceng.onmouseleave=function(){
  son.style.display="none"
  box2.style.display="none"
 }
 //鼠標(biāo)移動(dòng):son的位置隨鼠標(biāo)移動(dòng),box2背景圖的位置變化
 ceng.onmousemove=function(e){
  let sw=e.clientX-son.offsetWidth/2
  let sh=e.clientY-son.offsetHeight/2  
  if(sw>ceng.offsetWidth-son.offsetWidth){
   sw=ceng.offsetWidth-son.offsetWidth
  }
  else if(sw<=0){
   sw=0;
  }
  if(sh>ceng.offsetHeight-son.offsetHeight){
   sh=ceng.offsetHeight-son.offHeight
  }
  else if(sh<=0){
   sh=0;
  }
  son.style.left=sw+"px"
  son.style.top=sh+"px"
  box2.style.backgroundPosition=`${-2*sw}px ${-2*sh}px`
 }

完整代碼

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>放大鏡</title>
</head>
<style>
 *{
  margin: 0;
  padding: 0;
 }
 .box1{
  width: 500px;
  height:333px;
  /* border:1px solid red; */
  background: url('shatan.jpg') no-repeat;
  position: relative;
  float: left;
  box-sizing: border-box
 }
 .son{
  width: 100px;
  height: 100px;
  background-color: #fff;
  position: fixed;
  left:0;
  top:0;
  opacity: 0.7;
  display: none;
 }
 .ceng{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 100;
 }
 .box2{
  width: 200px;
  height:200px;
  border:1px solid red;
  background: url('shatan1.jpg') no-repeat;
  float: left;
  display: none;
 }
</style>
<body>
 <div class="box1">
  <div class="son"></div>
  <div class="ceng"></div>
 </div>
 <div class="box2"></div>
</body>
</html>
<script>
 let box1=document.querySelector(".box1");
 let box2=document.querySelector(".box2");
 let son=document.querySelector(".son");
 let ceng=document.querySelector(".ceng");
 ceng.onmouseenter=function(){
  son.style.display="block"
  box2.style.display="block"
 }
 ceng.onmouseleave=function(){
  son.style.display="none"
  box2.style.display="none"
 }
 ceng.onmousemove=function(e){
  let sw=e.clientX-son.offsetWidth/2
  let sh=e.clientY-son.offsetHeight/2  
  if(sw>ceng.offsetWidth-son.offsetWidth){
   sw=ceng.offsetWidth-son.offsetWidth
  }
  else if(sw<=0){
   sw=0;
  }
  if(sh>ceng.offsetHeight-son.offsetHeight){
   sh=ceng.offsetHeight-son.offHeight
  }
  else if(sh<=0){
   sh=0;
  }
  son.style.left=sw+"px"
  son.style.top=sh+"px"
  box2.style.backgroundPosition=`${-2*sw}px ${-2*sh}px`
 }
</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論