Jquery 點擊圖片在彈出層顯示大圖

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/wongwaidah/article/details/28432427(案例連結出處,本人只是轉載收藏)<html><head><meta charset="utf-8"><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"type="text/javascript"></script></head><body><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> <ul id="imglist"><li><img class="pimg" src="images/activity.jpg" width="500px" /></li><li><img class="pimg" src="images/activity.jpg" width="500px" /></li><li><img class="pimg" src="images/grd.jpg" width="500px" /></li><!-- <li><img class="pimg" src="img/4.jpg" width="500px" /></li><li><img class="pimg" src="img/5.jpg" width="500px" /></li><li><img class="pimg" src="img/6.jpg" width="500px" /></li><li><img class="pimg" src="img/7.jpg" width="500px" /></li><li><img class="pimg" src="img/8.jpg" width="500px" /></li><li><img class="pimg" src="img/9.jpg" width="500px" /></li><li><img class="pimg" src="img/10.jpg" width="500px" /></li> --></ul><script>$(function(){$(".pimg").click(function(){var _this = $(this);//將當前的pimg元素作為_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(){var windowW = $(window).width();//擷取當前視窗寬度var windowH = $(window).height();//擷取當前視窗高度var realWidth = this.width;//擷取圖片真實寬度var realHeight = this.height;//擷取圖片真實高度var imgWidth, imgHeight;var scale = 0.8;//縮放尺寸,當圖片真實寬度和高度大於視窗寬度和高度時進行縮放 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)/2;//計算圖片與視窗上邊距$(innerdiv).css({"top":h, "left":w});//設定#innerdiv的top和left屬性$(outerdiv).fadeIn("fast");//淡入顯示#outerdiv及.pimg}); $(outerdiv).click(function(){//再次點擊淡出消失彈出層$(this).fadeOut("fast");});}</script></body> </html>

Jquery 點擊圖片在彈出層顯示大圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.