ホームページ  >  記事  >  ウェブフロントエンド  >  jqueryを使用して虫眼鏡効果を作成する方法

jqueryを使用して虫眼鏡効果を作成する方法

php中世界最好的语言
php中世界最好的语言オリジナル
2018-03-14 16:50:351602ブラウズ

今回は、jqueryを使用して虫眼鏡効果を作成する方法と、jqueryを使用して虫眼鏡効果を作成する場合の注意事項を説明します。以下は実際的なケースです。

jquery で書かれた 2 つの虫眼鏡効果はプラグインを使用しません。思考の条件付けと明晰さ。 オブジェクト指向の手法を使って書かれていないので、初心者にも理解しやすいです。早速、コードを見てみましょう。写真はここにはアップロードしませんので、ご自身で見つけてください。比率を見つけるのが最善であり、効果が高くなります。

<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コード

*{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コード

<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コード

りー

js コード

*{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;}

この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、php 中国語 Web サイトの他の関連記事に注目してください。

推奨読書:

jquery はスタイル付き HTML タグを追加します

jquery がイベントを動的に生成されたタグにバインドする方法

jQuery がタグのサブ要素の値を取得する方法

jQuery はどのように取得し、 Pタグの値を変更します

以上がjqueryを使用して虫眼鏡効果を作成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。