Commit faa859eb authored by Sindre Sorhus's avatar Sindre Sorhus

Merge pull request #342 from spsawchuk/animate-quotes

animate developer quotes.
parents d3d43815 f4df18ba
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
</div> </div>
<hr> <hr>
<div class="row"> <div class="row">
<div class="span6"> <div class="span6 quotes">
<blockquote class="quote speech-bubble"> <blockquote class="quote speech-bubble">
<p></p> <p></p>
<footer> <footer>
......
...@@ -35,28 +35,108 @@ ...@@ -35,28 +35,108 @@
}); });
}; };
var Quotes = {};
Quotes.build = function (quotes, template) {
var quoteContainer = document.createElement('q'),
quoteElemCount = 0,
quoteCount = quotes.length;
$.fn.quote = function( data ) { var createQuoteElems = function () {
var $this = this; var quote = quotes[quoteElemCount],
el = $(template).hide();
function update() { el.children('p').text(quote.quote);
var el = data[ Math.floor( Math.random() * data.length ) ]; el.find('a').text(quote.person.name).attr('href', quote.link);
el.find('img').attr('src', quote.person.gravatar);
$this.children('p').text( el.quote ); quoteContainer.appendChild(el[0]);
$this.find('a').text( el.person.name );
$this.find('a').attr( 'href', el.person.link ); if (quoteCount > ++quoteElemCount) {
$this.find('img').attr( 'src', el.person.gravatar ); createQuoteElems();
setTimeout( update, 25000 ); }
}
return quoteContainer.innerHTML;
};
return createQuoteElems();
};
Quotes.random = function (quotes) {
var quoteCount = quotes.length,
randomQuotes = [];
var randomQuote = function () {
var randomQuoteIndex = Math.floor(Math.random() * quoteCount);
if ($.inArray(randomQuoteIndex, randomQuotes) > -1) {
return randomQuote();
}
if (randomQuotes.length === quoteCount - 1) {
randomQuotes = [];
}
randomQuotes.push(randomQuoteIndex);
return randomQuoteIndex;
};
return randomQuote;
};
Quotes.animate = function (container, animSpeed) {
var fader = function (fadeOut, fadeIn) {
var fadeOutCallback = function(){
fadeIn.fadeIn(500, fadeInCallback);
};
var fadeInCallback = function(){
window.setTimeout(swap, animSpeed);
};
update(); fadeOut.fadeOut(500, fadeOutCallback);
};
var quotes = container.children(),
selectRandomQuoteIndex = Quotes.random(quotes),
quoteElems = {},
activeQuoteIndex = selectRandomQuoteIndex(),
prevQuoteElem = $(quotes[activeQuoteIndex]);
var swap = function () {
if (!quoteElems[activeQuoteIndex]) {
quoteElems[activeQuoteIndex] = $(quotes[activeQuoteIndex]);
}
var activeQuoteElem = quoteElems[activeQuoteIndex];
fader(prevQuoteElem, activeQuoteElem);
activeQuoteIndex = selectRandomQuoteIndex();
prevQuoteElem = activeQuoteElem;
};
return swap();
};
Quotes.init = function (quotes) {
var container = $(this),
template = $(this).html(),
quotesHTML = Quotes.build(quotes, template);
container.html(quotesHTML);
Quotes.animate(container, 25000);
};
$.fn.quote = function(quotes) {
return this.each(function(){
Quotes.init.call(this, quotes);
});
}; };
// Apps popover // Apps popover
$('.applist a').persistantPopover(); $('.applist a').persistantPopover();
// Quotes // Quotes
$('.quote').quote([{ $('.quotes').quote([{
quote: 'TodoMVC is a godsend for helping developers find what well-developed frameworks match their mental model of application architecture.', quote: 'TodoMVC is a godsend for helping developers find what well-developed frameworks match their mental model of application architecture.',
person: { person: {
name: 'Paul Irish', name: 'Paul Irish',
...@@ -86,4 +166,4 @@ ...@@ -86,4 +166,4 @@
} }
}]); }]);
}()); }());
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment