为了更好地学习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