爲了更好地學習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);
不知道,但?我總是將數據設置爲:'$(this)。data(「height」,h);'並檢索它:'$(this).data(「height」);' 我很確定,設置高度數據屬性後,下次不會定義你會檢索它。 – Lumocra 2012-07-14 12:21:26
@Lumocra,似乎是問題,是的 – ed209 2012-07-14 12:33:43