Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
filenwind committed Jun 28, 2015
1 parent 1489736 commit fe72b4b
Show file tree
Hide file tree
Showing 9 changed files with 659 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# jquery-center-inline-block
A jquery plugin for center inline block element, and last row align left.

### [Demo](http://filenwind.github.io/jquery-center-inline-block)

## Usage
```
<div id='container'>
<div class="inline-box">1</div>
<div class="inline-box">2</div>
<div class="inline-box">3</div>
...
</div>
<script type="text/javascript">
$('#container').centerInlineBlock(options)
</script>
```

## Options
* **wrapper** - template for wrap container's contents

## Installation

```
npm install jquery-center-inline-block
```
103 changes: 103 additions & 0 deletions dist/jquery-center-inline-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
return define(['jquery'], function($) {
return factory($, window);
});
} else if (typeof exports !== 'undefined') {
return module.exports = factory(require('jquery'), window);
} else {
return factory(jQuery, window);
}
})(function($, window) {
'use strict';
var CenterInlineBlock, Plugin, pluginName;
pluginName = 'jquery-center-inline-block';
CenterInlineBlock = function(element, options) {
this.init(element, options);
this.resize();
};
CenterInlineBlock.prototype.init = function(element, options) {
this.options = $.extend({}, {
wrapper: "<div class='" + pluginName + "-wrapper' style='margin:0 auto; text-align:left'></div>"
}, options);
this.container = $(element);
this.window = $(window);
this.window_width = 0;
this.child_width = null;
this.appendWrapper();
return this.initWindowResizeEvent();
};
CenterInlineBlock.prototype.appendWrapper = function() {
this.wrapper = $(this.options.wrapper);
this.container.contents().appendTo(this.wrapper);
return this.container.append(this.wrapper);
};
CenterInlineBlock.prototype.initWindowResizeEvent = function() {
this.resizeID = this.window.data(pluginName + "-resize-id");
if (!this.resizeID) {
this.resizeID = 0;
}
this.resizeID++;
this.window.data(pluginName + "-resize-id", this.resizeID);
return this.window.on("resize." + pluginName + "-" + this.resizeID, this.onWindowResize.bind(this));
};
CenterInlineBlock.prototype.onWindowResize = function() {
if (this.window_width !== this.window.width()) {
this.window_width = this.window.width();
clearTimeout(this.resizeTimeout);
return this.resizeTimeout = setTimeout(this.resize.bind(this));
}
};
CenterInlineBlock.prototype.resize = function() {
var _this, capacity, new_width;
if (this.container.is(':visible')) {
this.wrapper.contents().each(function() {
if (this.nodeType === 3 && !$.trim(this.nodeValue)) {
return $(this).remove();
}
});
if (this.child_width === null) {
this.child_width = 0;
_this = this;
this.wrapper.children().each(function(i) {
_this.child_width = $(this).outerWidth(true);
return false;
});
}
if (this.child_width > 0) {
capacity = Math.floor(this.container.width() / this.child_width);
if (capacity < 1) {
capacity = 1;
}
new_width = capacity * this.child_width;
if (this.wrapper.width() !== new_width) {
return this.wrapper.width(new_width);
}
}
}
};
CenterInlineBlock.prototype.destroy = function() {
this.unbindWindowResize();
return this.container.data(pluginName, null);
};
CenterInlineBlock.prototype.unbindWindowResize = function() {
return this.window.unbind("resize." + pluginName + "-" + this.resizeID);
};
Plugin = function(option) {
return this.each(function() {
var $this, data, options;
$this = $(this);
data = $this.data(pluginName);
if (data) {
if (typeof option === 'string') {
return data[option]();
}
} else if (!/destroy/.test(option)) {
options = typeof option === 'object' ? option : {};
return $this.data(pluginName, new CenterInlineBlock(this, options));
}
});
};
return $.fn.centerInlineBlock = Plugin;
});
1 change: 1 addition & 0 deletions dist/jquery-center-inline-block.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fe72b4b

Please sign in to comment.