2012-07-14 105 views
2

爲了更好地學習jQuery,我決定編寫一個插件,創建一個像google +的gallery collage effect。這是一個example我應該在哪裏存儲html元素的jQuery數據?

我想在調整包含圖像的html元素的大小時再次觸發它。我遇到的部分問題是我需要存儲原始圖像大小以便重新計算圖像大小以使其適合。

我不知道在哪裏存儲以及如何檢索這些原始圖像大小。完整的插件與以上鍊接,但我會在這裏提供一個總結。

;(function($) { 
    $.fn.collagePlus = function(options) { 

     var settings = $.extend( 
      //... 
      'images'   : $('img', $(this)) 
      //... 
     ); 

     return this.each(function() { 
      settings.images.each(function(index){ 
       //... 

       /* 
       * get the current image size 
       */ 
       var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width(); 
       var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height(); 

       /* 
       * store the original size for resize events 
       */ 
       $(this).attr("data-width" , w ); 
       $(this).attr("data-height" , h ); 
       //... Do some other stuff 
       } 
      ); 
     }); 
    } 
})(jQuery); 
+2

不知道,但?我總是將數據設置爲:'$(this)。data(「height」,h);'並檢索它:'$(this).data(「height」);' 我很確定,設置高度數據屬性後,下次不會定義你會檢索它。 – Lumocra 2012-07-14 12:21:26

+0

@Lumocra,似乎是問題,是的 – ed209 2012-07-14 12:33:43

回答

4

您正在使用.data()錯誤。當您將一個參數傳遞給.data函數時,它將返回給定鍵的值。當您傳遞2個參數時,.data將設置該鍵的值。

塊:

//get the current image size 
var w = (typeof $(this).data().width != 'undefined') ? $(this).data().width : $(this).width(); 
var h = (typeof $(this).data().height != 'undefined') ? $(this).data().height : $(this).height(); 

應該是:

var $this = $(this); //caching your selector 
if (!$this.data('width')) //if this element doesn't have a width stored 
    $this.data('width', $this.width()); //stores currently computed width 
if (!$this.data('height')) //repeat 
    $this.data('height', $this.height()); 

當然,後來檢索數據:

alert($this.data('width')) //alerts currently stored width 

Fiddle Demo

您還可以存儲在.data對象傳遞地圖屬性的:

if (!$(this).data('size')) 
    $(this).data('size', { width: $(this).width(), height: $(this).height() }); 

現在widthheight是存儲在.data('size')對象,其可以與檢索的屬性:

alert($(this).data('size').width); 

Fiddle

對於爲了簡單起見,我主要採用第一種選擇。然而第二個看起來更整潔。選擇你發現更具可讀性和可維護性的任何一個。

+0

謝謝,那工作。 '''var $ this = $(this);'''你爲什麼要緩存選擇器?那是我應該做的嗎? – ed209 2012-07-14 12:29:49

+2

是的。通常,如果你多次使用$(this),你應該將它緩存在一個變量中。前綴「$」有助於將變量的值識別爲jQuery包裝的對象。 – Wolfram 2012-07-14 12:31:07

+1

@ ed209這是一個微型優化,我在尋找Jared的$(this)vs $ this'這個問題,這是一個很好的參考。 +1 @Wolfram,另外,這樣你每次引用'$ this'時都不會創建一個新的jQuery對象。重新使用選擇器可提高性能。 – 2012-07-14 12:31:40

4

在服務器端,你可以存儲HTML元素的數據data-* attributes並通過jQuery的.data()功能得到它(因爲jQuery 1.4.3,這也改變了函數的一般行爲作爲文檔說明)。你在你的插件設置屬性,但在這一點上,你可以存儲在data對象這樣的原始寬度和高度:

$(this).data("data-width", w); 
$(this).data("data-height", h); 

使用.data()函數返回的數據不管它被存儲爲您的HTML中的data-屬性,還是包含在jQuery的data對象中。您已經在使用.data()函數而沒有任何參數,其中returns the complete data object of the matched elements也與來自HTML屬性的數據和來自jQuery的data對象。這工作,但你可以只讓調用它像這樣保存的widthheight:你們不使用。數據()的錯誤的方式

$(this).data("width"); 
$(this).data("height"); 
+0

非常感謝額外的信息:) – ed209 2012-07-14 12:31:15

+0

是的,我沒有提到'.data()'也檢索標記中設置的初始值,這值得+1。需要注意的是,OP會在'.width()'客戶端存儲* computed *寬度,如果存在可能影響它的CSS規則(即%維度,可變父維度等等),則可能與實際圖像大小有所不同。 ),這很難解釋服務器端,否則可能會產生不良影響。 – 2012-07-14 12:52:04

相關問題