1. 程式人生 > >Jquery動態進行圖片縮略

Jquery動態進行圖片縮略

//頁面載入完執行resizeImage()函式
$(document).ready(resizeImage());

function resizeImage(){
    $(".pic a img").each(function(){
                //載入圖片至記憶體,完成後執行
        $(this).load(function(){
                        //獲得原始圖片高寬
            var imgWidth = $(this).width();
            var imgHeight = $(this).height();
                        
//獲得圖片所在Div高寬 var boxWidth=$('.pic').width(); var boxHeight=$('.pic').height(); //比較imgBox的長寬比與img的長寬比大小 if((boxWidth/boxHeight)>=(imgWidth/imgHeight)) { //重新設定img的width和height $(this).width((boxHeight*imgWidth)/imgHeight); $(this
).height(boxHeight); //讓圖片居中顯示 var margin=(boxWidth-$(this).width())/2; $(this).css("margin-left",margin); } else { //重新設定img的width和height $(this).width(boxWidth); $(
this).height((boxWidth*imgHeight)/imgWidth); //讓圖片居中顯示 var margin=(boxHeight-$(this).height())/2; $(this).css("margin-top",margin); } }); }) }