Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Last active December 15, 2015 22:19
Show Gist options
  • Save swvitaliy/5332198 to your computer and use it in GitHub Desktop.
Save swvitaliy/5332198 to your computer and use it in GitHub Desktop.
Text Pretty Slider
<html>
<head>
<title>Text Pretty Slider</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="textPrettySlider.js"></script>
<style type="text/css">
span { color: red; }
</style>
</head>
<body>
<div class="container">
<div class="page-header" id="text_pretty_slider">
<span>T</span><small>ext</small>
<span>P</span><small>retty</small>
<span>S</span><small>lider</small>
</div>
<script type="text/javascript">
$(function() {
$("#text_pretty_slider>small")
.hide();
$("#text_pretty_slider")
.textPrettySlider("show", {
firstLetters:"small"
});
});
</script>
</body>
</html>
// overwrite this file latest jquery version
;(function($, undefined) {
var slider = function(fn, options) {
options = options || {};
var container = $(options.container);
var firstLetters = container.find(options.firstLetters);
var firstLettersInitialWidths = [];
var curIndex = 0;
var items = [];
var nextDelay = 150;
var animateDelay = 350;
var sign = ({
"hide": "-",
"show": "+"
})[fn];
if (typeof sign === "undefined") {
throw "unknown slice action \"" + fn + "\"";
}
var addItem = function (item) {
items.push({ element: item, width:item.outerWidth() });
};
var curItem = function() {
if (curIndex >= items.length)
return null;
var index = curIndex;
curIndex += 1;
return items[index];
};
var animateNextItem = function() {
var item = curItem();
if (!item) {
return null;
}
if (sign === "+") {
item.element.width(0).show();
}
item.element.animate({
"width": sign + "=" + item.width + "px"
},
animateDelay,
function() {
if (sign === "-") {
$(this).hide();
}
});
setTimeout(function() {
animateNextItem();
}, nextDelay);
return item;
};
firstLetters.each(function() {
addItem($(this));
});
return animateNextItem();
};
$.fn.textPrettySlider = function (fn, options) {
// console.log(this, arguments);
options["container"] = this;
slider(fn, options);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment