Skip to content

Instantly share code, notes, and snippets.

@MikSDigital
Last active August 29, 2015 14:22
Show Gist options
  • Save MikSDigital/50045ba18c5342563b4b to your computer and use it in GitHub Desktop.
Save MikSDigital/50045ba18c5342563b4b to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Word Rotate</title>
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css" media="screen">
span {
display: inline-block;
text-align: left;
}
span.word-rotate {
display: inline-block;
overflow: hidden;
text-align: center;
position: relative;
}
span.word-rotate-items {
position: relative;
top: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="container">
<header class="header" role="banner">
<h1>Word Rotation</h1>
<span class="word-rotate active">
<span class="word-rotate-items">
<span><h1>Experiment</h1></span>
<span><h1>Study</h1></span>
<span><h1>Exercise</h1></span>
<span><h1>Operation</h1></span>
</span>
</span>
</header>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="word-rotate.js" type="text/javascript" charset="utf-8" async defer></script>
</body>
</html>
(function() {
"use strict";
var Core = {
initialized: false,
initialize: function() {
if (this.initialized) return;
this.initialized = true;
this.build();
},
build: function() {
this.wordRotate();
},
wordRotate: function() {
$(".word-rotate").each(function() {
var $this = $(this),
itemsWrapper = $(this).find(".word-rotate-items"),
items = itemsWrapper.find("> span"),
firstItem = items.eq(0),
firstItemClone = firstItem.clone(),
maxWidth = 0,
itemHeight = 0,
currentItem = 1,
currentTop = 0;
// set the width of each span containing the words
items.each(function() {
if($(this).width() > maxWidth) {
maxWidth = $(this).width();
}
});
itemHeight = firstItem.height();
itemsWrapper.append(firstItemClone);
$this
.width(maxWidth)
.height(itemHeight)
setInterval(function() {
currentTop = (currentItem * itemHeight);
itemsWrapper.animate({
top: -(currentTop) + "px"
}, 300, function() {
currentItem++;
if(currentItem > items.length) {
itemsWrapper.css("top", 0);
currentItem = 1;
}
});
}, 2000);
});
}
};
Core.initialize();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment