【发布时间】:2012-07-15 06:33:30
【问题描述】:
为了更好地学习 jquery,我决定编写一个插件来创建一个 gallery collage effect 就像 google+。这是example。
我想在调整包含图像的 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 );
【问题讨论】:
-
不确定,但您没有以错误的方式使用 .data() 吗?我总是设置像:
$(this).data("height", h);这样的数据并像:$(this).data("height");那样检索它而且我很确定,在设置了高度数据属性之后,下次你检索它就不会是未定义的了。 -
@Lumocra 这似乎是问题所在,是的
标签: javascript jquery