1.27.2011

圖片縮圖 - jQuery自動縮放比例到指定區塊


天海設計

Features

Description:

使用jQuery的方式,來完成自動縮圖的效果,並加快了網站的讀時間,方法只要在img標籤中設定參數,就會自動處理。

日後只要想套用縮圖時,只要在img後加入alumb=true,程式就會自動的處理

--------------------------------------------------------------------------

下載:  Download  (密碼:  見壓縮檔註解)

By dowloading you accept our Terms & Disclaimer


Instructions:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
//圖片縮圖
$(window).load(function(){
	$("img").each(function(i){
		if($(this).attr("alumb")=="true"){
			//移除目前設定的影像長寬
			$(this).removeAttr('width');
			$(this).removeAttr('height');
 
			//取得影像實際的長寬
			var imgW = $(this).width();
			var imgH = $(this).height();
 
			//計算縮放比例
			var w=$(this).attr("_w")/imgW;
			var h=$(this).attr("_h")/imgH;
			var pre=1;
			if(w>h){
				pre=h;
			}else{
				pre=w;
			}
			if(w>1 || h>1){pre=1;}	
 
			//設定目前的縮放比例
			$(this).width(imgW*pre);
			$(this).height(imgH*pre);
		}
	});
});
</script>

<img src="sample.jpg" alumb="true" _w="300" _h="300" width="500"/>


alumb:是否要執行縮圖,true是要執行(若不寫為不執行)。
_w:縮放的寬度。
_h:縮放的高度。


  • If you believe that your work has been copied in a way that constitutes copyright infringement, or that your intellectual property rights have been otherwise violated, please e-mail us

0 留言:

發佈留言

您使用留言則表示同意及遵守使用條款及守則

建議: 為方便留言回覆,請不要用匿名方式 留言。