Jquery 기술을 통해 돋보기 구현

亚连
풀어 주다: 2018-06-05 15:14:47
원래의
1242명이 탐색했습니다.

이 글에서는 jquery를 사용하여 돋보기 효과를 얻는 방법과 코드 공유에 대한 자세한 분석을 제공합니다. 관심이 있다면 배워보세요.

jquery로 작성된 두 가지 돋보기 효과는 플러그인을 사용하지 않습니다. 생각의 조절과 명확성. 객체지향적으로 작성되지 않았기 때문에 초보자도 이해하기 쉽습니다. 더 이상 고민하지 말고 코드를 살펴보겠습니다. 사진은 여기에 업로드되지 않으며 직접 찾을 수 있습니다. 비율을 찾는 것이 가장 좋으므로 효과가 더 좋습니다.

<body> 
  <p id="father"> 
    <p id="container"> 
      <img src="img/400_1.jpg" style="display: block;"> 
      <img src="img/400_2.jpg" > 
      <p class="shade"></p> 
    </p> 
    <p class="small first"><img src="img/50_1.jpg"></p> 
    <p class="small second"><img src="img/50_2.jpg"></p> 
  </p> 
   
  <p class="big"> 
    <img src="img/800_1.jpg" style="display: block;"> 
    <img src="img/800_2.jpg"> 
  </p> 
</body>
로그인 후 복사

css code

*{padding: 0; margin: 0;} 
  #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} 
  #father .second{left: 70px;} 
  .third{left: 140px;} 
  #father{position: relative; top: 100px; left: 50px; height: 460px;} 
  #container{position: absolute; width: 400px; height: 400px;} 
  #container img{position: absolute; display: none;} 
 
  .shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0; 
    left: 0; display: none;} 
  .big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;} 
  .big img{width: 800px; height: 800px; position: absolute; display: none;}
로그인 후 복사

js code

<script type="text/javascript" src=&#39;js/jquery-1.12.4.min.js&#39;></script> 
  <script type="text/javascript"> 
    $(function () { 
 
      changePic(&#39;.first&#39;,0); 
      changePic(&#39;.second&#39;,1); 
 
      var shadeWidth = $(&#39;.shade&#39;).width(),//阴影的宽度 
        shadeHeight = $(&#39;.shade&#39;).height(),//阴影的高度 
        middleWidth = $(&#39;#container&#39;).width(),//容器的宽度 
        middleHeight = $(&#39;#container&#39;).height(),//容器的高度 
        bigWidth = $(&#39;.big&#39;).width(),//放大图片盒子的宽度 
        bigHeight = $(&#39;.big&#39;).height(),//放大图片盒子的高度 
        rateX = bigWidth / shadeWidth,//放大区和遮罩层的宽度比例 
        rateY = bigHeight / shadeHeight;//放大区和遮罩层的高度比例 
 
      //当鼠标移入与移出时阴影与放大去显现/消失 
      $(&#39;#container&#39;).hover(function() { 
        $(&#39;.shade&#39;).show(); 
        $(&#39;.big&#39;).show(); 
      }, function() { 
        $(&#39;.shade&#39;).hide(); 
        $(&#39;.big&#39;).hide(); 
      }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 
 
        //记录下光标距离页面的距离 
        var x = e.pageX, 
          y = e.pageY; 
 
        //设置遮罩层的位置 
        $(&#39;.shade&#39;).offset({ 
          top: y-shadeHeight/2, 
          left: x-shadeWidth/2 
        });    
 
        //获取遮罩层相对父元素的位置 
        var cur = $(&#39;.shade&#39;).position(), 
          _top = cur.top, 
          _left = cur.left, 
          hdiffer = middleHeight - shadeHeight, 
          wdiffer = middleWidth - shadeWidth; 
 
        if (_top < 0) _top = 0; 
        else if (_top > hdiffer) _top = hdiffer; 
        if (_left < 0) _left = 0; 
        else if (_left > wdiffer) _left =wdiffer; 
 
        //判断完成后设置遮罩层的范围 
        $(&#39;.shade&#39;).css({ 
          top: _top, 
          left: _left 
        }); 
 
        //设置放大区图片移动 
        $(&#39;.big img&#39;).css({ 
          top: - rateY*_top, 
          left: - rateX*_left 
        }); 
 
      });; 
 
      //封装的改变图片显示的函数 
      function changePic (element,index) { 
        $(element).click(function() { 
          $(&#39;#container img&#39;).eq(index).css(&#39;display&#39;, &#39;block&#39;).siblings().css(&#39;display&#39;, &#39;none&#39;); 
          $(&#39;.big img&#39;).eq(index).css(&#39;display&#39;, &#39;block&#39;).siblings().css(&#39;display&#39;, &#39;none&#39;); 
        }); 
      } 
       
    });
로그인 후 복사

위 내용은 흔히 사용되는 내용이며, 다음은 원본 이미지를 기준으로 확대한 것입니다

htm

<body> 
  <p id="father"> 
    <p id="container"> 
      <img src="img/400_1.jpg" style="display: block;"> 
      <img src="img/400_2.jpg" > 
      <img src="img/400_3.jpg" > 
      <p class="shade"> 
        <img src="img/800_1.jpg" style="display: block;"> 
        <img src="img/800_2.jpg"> 
        <img src="img/800_3.jpg"> 
      </p> 
    </p> 
    <p class="small first"><img src="img/50_1.jpg"></p> 
    <p class="small second"><img src="img/50_2.jpg"></p> 
    <p class="small third"><img src="img/50_3.jpg"></p> 
  </p> 
</body>
로그인 후 복사

css code

*{padding: 0; margin: 0;} 
    #father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;} 
    #father .second{left: 70px;} 
    .third{left: 140px;} 
    #father{position: relative; top: 100px; left: 50px; height: 460px;} 
    #container{position: absolute; width: 400px; height: 400px;} 
    #container img{position: absolute; display: none;} 
    .shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;} 
    .shade img{display: none; width: 800px; height: 800px; position: absolute;}
로그인 후 복사

js code

<span style="white-space:pre">  </span><script type="text/javascript" src=&#39;js/jquery-1.12.4.min.js&#39;></script> 
  <script type="text/javascript"> 
    $(function () { 
 
      changePic(&#39;.first&#39;,0); 
      changePic(&#39;.second&#39;,1); 
      changePic(&#39;.third&#39;,2); 
 
      var shadeWidth = $(&#39;.shade&#39;).width(),//阴影的宽度 
        shadeHeight = $(&#39;.shade&#39;).height(),//阴影的高度 
        middleWidth = $(&#39;#container&#39;).width(),//容器的宽度 
        middleHeight = $(&#39;#container&#39;).height(),//容器的高度 
        bigImgWidth = $(&#39;.shade img&#39;).width(),//放大图片盒子的宽度 
        bigImgHeight = $(&#39;.shade img&#39;).height(),//放大图片盒子的高度 
        rateX = bigImgWidth / middleWidth,//放大区和遮罩层的宽度比例2 
        rateY = bigImgHeight / middleHeight;//放大区和遮罩层的高度比例2 
 
      //当鼠标移入与移出时阴影与放大去显现/消失 
      $(&#39;#container&#39;).hover(function() { 
        $(&#39;.shade&#39;).show(); 
        $(&#39;.big&#39;).show(); 
      }, function() { 
        $(&#39;.shade&#39;).hide(); 
        $(&#39;.big&#39;).hide(); 
      }).mousemove(function(e) {//当鼠标移动时,阴影和放大区图片进行移动 
         
 
        //记录下光标距离页面的距离 
        var x = e.pageX, 
          y = e.pageY; 
 
        //设置遮罩层的位置 
        $(&#39;.shade&#39;).offset({ 
          top: y-shadeHeight/2, 
          left: x-shadeWidth/2 
        });    
 
        //获取遮罩层相对父元素的位置 
        var cur = $(&#39;.shade&#39;).position(), 
          _top = cur.top, 
          _left = cur.left, 
          hdiffer = middleHeight - shadeHeight, 
          wdiffer = middleWidth - shadeWidth; 
 
        if (_top < 0) _top = 0; 
        else if (_top > hdiffer) _top = hdiffer; 
        if (_left < 0) _left = 0; 
        else if (_left > wdiffer) _left =wdiffer; 
 
        //判断完成后设置遮罩层的范围 
        $(&#39;.shade&#39;).css({ 
          top: _top, 
          left: _left 
        }); 
 
         
        //设置放大区图片移动 
        $(&#39;.shade img&#39;).css({ 
          top: - _top*rateY*3/2, 
          left: - _left*rateX*3/2 
        }); 
 
      });; 
 
      //封装的改变图片显示的函数 
      function changePic (element,index) { 
        $(element).click(function() { 
          $(&#39;#container img&#39;).eq(index).css(&#39;display&#39;, &#39;block&#39;).siblings().css(&#39;display&#39;, &#39;none&#39;); 
          $(&#39;.shade img&#39;).eq(index).css(&#39;display&#39;, &#39;block&#39;).siblings().css(&#39;display&#39;, &#39;none&#39;); 
        }); 
      } 
       
    }); 
<span style="white-space:pre">  </span></script>
로그인 후 복사

위 내용은 제가 모두를 위해 정리한 내용입니다. 앞으로 모든 분들께 도움이 되길 바랍니다.

관련 기사:

vue에서 사용자 정의 전역 메서드를 구현하는 방법

vue 구성 요소에서 전역 등록 및 로컬 등록을 구현하는 방법

vue-cli+webpack 프로젝트를 통해 프로젝트 이름을 수정하는 방법

위 내용은 Jquery 기술을 통해 돋보기 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!