html+css+jquery點擊圖片彈出大圖


html+css部分:

<img class="img" src="http://bexplus.oss-ap-southeast-1.aliyuncs.com/uploads/image_pc/20180908/1536394854.91927488.png" width="200" height="200" alt="">

<div id="outerdiv" style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;">
  <div id="innerdiv" style="position:absolute;">
    <img id="bigimg" style="border:5px solid #fff;" src="" />
  </div>
</div>

 

 

jquery部分:

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
    $(".img").bind('click',function(){
    var _this = $(this);//將當前的img元素作為_this傳入函數
      imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
    })
    function imgShow(outerdiv, innerdiv, bigimg, _this) {
      var src = _this.attr("src");//獲取當前點擊的pimg元素中的src屬性
      $(bigimg).attr("src", src);//設置#bigimg元素的src屬性
      /*獲取當前點擊圖片的真實大小,並顯示彈出層及大圖*/
      //$("<img/>").attr("src", src).load(function() {
      $("<img/>").attr("src", src).on("load",function(){
        var windowW = $(window).width();//獲取當前窗口寬度
        var windowH = $(window).height();//獲取當前窗口高度
        var realWidth = this.width;//獲取圖片真實寬度
        var realHeight = this.height;//獲取圖片真實高度
        var imgWidth, imgHeight;

        var scale = 0.6;//縮放尺寸,當圖片真實寬度和高度大於窗口寬度和高度時進行縮放

        if (realHeight > windowH * scale) {//判斷圖片高度
          imgHeight = windowH * scale;//如大於窗口高度,圖片高度進行縮放
          imgWidth = imgHeight / realHeight * realWidth;//等比例縮放寬度
          if (imgWidth > windowW * scale) {//如寬度扔大於窗口寬度
            imgWidth = windowW * scale;//再對寬度進行縮放
          }
        } else if (realWidth > windowW * scale) {//如圖片高度合適,判斷圖片寬度
          imgWidth = windowW * scale;//如大於窗口寬度,圖片寬度進行縮放
          imgHeight = imgWidth / realWidth * realHeight;//等比例縮放高度
        } else {//如果圖片真實高度和寬度都符合要求,高寬不變
          imgWidth = realWidth;
          imgHeight = realHeight;
        }
        $(bigimg).css("width", imgWidth);//以最終的寬度對圖片縮放

        var w = (windowW - imgWidth) / 2;//計算圖片與窗口左邊距
        var h = (windowH - imgHeight) / 4;//計算圖片與窗口上邊距
        $(innerdiv).css({"top": h, "left": w});//設置#innerdiv的top和left屬性
          $(outerdiv).fadeIn("fast");//淡入顯示#outerdiv及.pimg
        });
        $(outerdiv).click(function() {//再次點擊淡出消失彈出層
        $(this).fadeOut("fast");
      });
    }

</script>

 

注意:

jquery剛開始是//$("<img/>").attr("src", src).load(function() {

會報錯,

TypeError: url.indexOf is not a function ,使用jquery load()出現錯誤

改成這樣:

       $("<img/>").attr("src", src).on("load",function(){

 

分析:

問題:在使用  jquery 3.3.1  版本時,寫了如下測試代碼:

                       <script>

                              $(window).load(function(){

                              console.log("just a test!");

                              });

                      </script>

               運行出錯:TypeError: url.indexOf is not a function ..........

原因:  .load() ,  .error() ,  .unload() ,已經不在支持,jquery1.8版本之后的都無法再使用; 

解決辦法:使用  .on("load",function(){.................}) 來代替  .load()  即可;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM