2012-07-14 106 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

相关问题