<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="http://flightjs.github.io/release/latest/flight.js"></script>
<meta charset="utf-8">
<title>Dealing with mulitple instances of communicating components</title>
</head>
<body>
<div class="outer">
<div class="carousel js-carousel">
<div class="carousel__transport js-carousel__transport">
<div class="carousel__item js-carousel__item">1</div>
<div class="carousel__item js-carousel__item">2</div>
<div class="carousel__item js-carousel__item">3</div>
</div>
</div>
<ul class="pagination js-pagination">
<li class="pagination__indicator js-pagination__indicator">One</li>
<li class="pagination__indicator js-pagination__indicator">Two</li>
<li class="pagination__indicator js-pagination__indicator">Three</li>
</ul>
</div>
<div class="outer">
<div class="carousel js-carousel">
<div class="carousel__transport js-carousel__transport">
<div class="carousel__item js-carousel__item">1</div>
<div class="carousel__item js-carousel__item">2</div>
</div>
</div>
<ul class="pagination js-pagination">
<li class="pagination__indicator js-pagination__indicator">One</li>
<li class="pagination__indicator js-pagination__indicator">Two</li>
</ul>
</div>
</body>
</html>
/* Base*/
* { margin: 0; padding: 0; font-family: sans-serif; }
/* Carousel*/
.carousel { width: 600px; height: 400px; background: #ccc; overflow: hidden; position: relative; }
.carousel__transport { transition: left 0.2s; position: relative; display: inline-block; white-space: nowrap; }
.carousel__item { display: inline-block; width: 600px; height: 400px; outline: 1px solid yellow; line-height: 400px; text-align: center; font-size: 20px; }
/* Pagination*/
.pagination { margin-bottom: 20px; padding-top: 10px; width: 600px; text-align: center; margin-left: -10px; }
.pagination__indicator { text-indent: -9999px; margin-left: 10px; width: 30px; height: 30px; display: inline-block; background: #ccc; border-radius: 999px; opacity: 0.5; }
.pagination__indicator.is-current { opacity: 1; }
// Controls state and offset of multiple adjacent items within the DOM
function with_carousel() {
this.defaultAttrs({
transport: '.js-carousel__transport',
items: '.js-carousel__item',
currentItemIndex: 0,
movedEvent: 'uiCarouselMoved',
itemWidth: 600
});
this.goToNextItem = function(e, data) {
if(this.attr.currentItemIndex === this.attr.maxIndex) this.attr.currentItemIndex = 0;
else this.attr.currentItemIndex ++;
this.moveTransport();
this.trigger(this.attr.movedEvent, {
index: this.attr.currentItemIndex
});
};
this.moveTransport = function() {
var left = 0 - (this.attr.currentItemIndex * this.attr.itemWidth);
this.select('transport').css({
'left': left
});
};
this.cacheMaxIndex = function() {
this.attr.maxIndex = this.select('items').length - 1;
};
this.after('initialize', function() {
this.on('click', this.goToNextItem);
this.cacheMaxIndex();
this.trigger(this.attr.movedEvent, {
index: 0
});
});
}
// Indicates which particular item is current
function with_pagination() {
this.defaultAttrs({
indicators: '.js-pagination__indicator',
currentClass: 'is-current'
});
this.changeCurrentIndicator = function(e, data) {
var _this = this;
this.select('indicators').removeClass(this.attr.currentClass);
this.select('indicators').each(function(k, v) {
if(k === data.index) {
$(this).addClass(_this.attr.currentClass);
return false;
}
});
};
this.after('initialize', function() {
this.on('uiCarouselMoved', this.changeCurrentIndicator);
});
}
var Carousel = flight.component(with_carousel, with_pagination);
(function() {
Carousel.attachTo('.outer');
}());
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |