Skip to content

Instantly share code, notes, and snippets.

@mattmazzola
Last active October 12, 2015 03:29
Show Gist options
  • Save mattmazzola/874f0de00a005fc2d239 to your computer and use it in GitHub Desktop.
Save mattmazzola/874f0de00a005fc2d239 to your computer and use it in GitHub Desktop.
sd-cycle
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
things: [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' }
]
});
<h1>Welcome to {{appName}}</h1>
{{#sc-cycle height=70 items=things as |item|}}
{{item.name}}
{{/sc-cycle}}
<hr />
{{outlet}}
import Ember from 'ember';
const {
computed
} = Ember;
export default Ember.Component.extend({
currentItemIndex: 0,
height: 50,
transition: true,
circularItems: computed('items', function() {
return this.get('items').concat(this.get('items').objectAt(0))
}),
currentItem: computed('currentItemIndex', function() {
return this.get('items').objectAt(this.get('currentItemIndex'));
}),
currentOffset: computed('currentItemIndex', function() {
return this.get('currentItemIndex') * this.get('height');
}),
inlineStyleHeight: computed('height', function() {
const string = `height: ${this.get('height')}px`;
console.log(`string: ${string}`);
return new Ember.Handlebars.SafeString(`height: ${this.get('height')}px`);
}),
inlineStyleTop: computed('currentOffset', function() {
return new Ember.Handlebars.SafeString(`top: -${this.get('currentOffset')}px`);
}),
actions: {
updateIndex() {
const index = this.get('currentItemIndex');
const nextIndex = (index + 1) % this.get('circularItems.length');
if(nextIndex === 0) {
this.set('transition', false);
this.set('currentItemIndex', nextIndex);
Ember.run.next(this, function () {
this.set('transition', true);
this.set('currentItemIndex', nextIndex + 1);
});
}
else {
this.set('transition', true);
this.set('currentItemIndex', nextIndex);
}
this.set('currentItemIndex', nextIndex);
}
}
});
<p>Current Item Index: {{currentItemIndex}}</p>
<p>Current Offset: {{currentOffset}}</p>
<button {{action "updateIndex"}}>Update Index</button>
<br />
<br />
<div class="sd-cycle" style={{inlineStyleHeight}}>
<div class="sd-cycle__items {{if transition 'sd-cycle__items--transition'}}" style={{inlineStyleTop}}>
{{#each circularItems as |item| }}
<div class="sd-cycle__item" style={{inlineStyleHeight}}>
{{yield item}}
</div>
{{/each}}
</div>
</div>
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.sd-cycle {
overflow: hidden;
position: relative;
border: 1px solid blue;
}
.sd-cycle__items--transition {
transition: top 1s ease-out;
}
.sd-cycle__items {
position: absolute;
width: 100%;
}
.sd-cycle__item {
background: #ececec
}
{
"version": "0.4.11",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.11/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment