Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I like to separate my plug-ins into four sections.</p> <p>1: $.fn implementation that iterates the jQuery element collection and attaches the plug-in.</p> <p>2: The plug-in "constructor" or init.</p> <p>3: The plug-in default options</p> <p>4: The plug-in instance methods or implementation.</p> <p>Here's how I would have structured your plug-in. I think this is more comprehensible than what you've provided.</p> <pre><code>(function($){ // constructor or init task var boxAnimate = function(el, options) { this.element = $(el); this.options = options; if (this.options.height == 'auto') this.options.height = this.element.height(); if (this.options.width == 'auto') this.options.width= this.element.width(); if (this.options.tileHeight == 'auto') this.options.tileHeight = this.options.height/25; if (this.options.tileWidth == 'auto') this.options.tileWidth = this.options.width/25; //Create background: this.element.prepend('&lt;div id="backDrop" style="color:white;position:absolute;z-index:998;background-color:'+this.options.bgColor+';height:'+this.options.height+'px;width:'+this.options.width+'px;"&gt;&lt;/div&gt;'); //create boxes: for(var iRow=0;iRow&lt;this.options.tileRows;iRow++){ for(var iCol=0;iCol&lt;this.options.tileCols;iCol++){ $('#backDrop').append('&lt;span class="backDropTile" id="bdTile_'+iRow+'_'+iCol+'" style="z-index:998;float:left;background-color:green;height:'+this.options.tileHeight+'px;width:'+this.options.tileWidth+'px;"&gt;&lt;/span&gt;'); } } } // default options boxAnimate.defaults = { bgColor: '#000000', padding: 20, height: 'auto', width: 'auto', tileRows:25, tileHeight: 'auto', tileCols:25, tileWidth: 'auto', speed: 500 }; // instance methods boxAnimate.prototype = { reveal: function() { var lastTile = $('.backDropTile:last').attr('id'); var pos1 = lastTile.indexOf("_"); var pos2 = lastTile.lastIndexOf("_"); var lCol = parseInt(lastTile.substr(pos2+1)); var lRow = parseInt(lastTile.substr(pos1+1,(pos2-pos1)-1)); for(var row=lRow;row&gt;=0;row--) { for(var col=lCol;col&gt;=0;col--) { $('#bdTile_'+row+'_'+col).animate({ opacity: 0 }, 1000, function() { $('#bdTile_'+row+'_'+col).remove(); }); } } } } // $.fn registration $.fn.boxAnimate = function(options) { return this.each(function() { var el = $(this); var o = $.extend({}, boxAnimate.defaults, options) if (!el.data('boxAnimate')) el.data('boxAnimate', new boxAnimate(el, o)); }); } })(jQuery); $('#book').boxAnimate(); $('#clickme').click(function(e) { $('#book').data('boxAnimate').reveal(); e.preventDefault(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload