Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding shuffle function and setting. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Put every rotating words inside the `<span class="rotate"></span>` and separate
$(".rotate").textrotator({
animation: "dissolve", // You can pick the way it animates when rotating through words. Options are dissolve (default), fade, flip, flipUp, flipCube, flipCubeUp and spin.
separator: "," // If you don't want commas to be the separator, you can define a new separator (|, &, * etc.) by yourself using this field.
speed: 2000 // How many milliseconds until the next word show.
speed: 2000, // How many milliseconds until the next word show.
shuffle: false // Whether to shuffle the words.
});
````
## Other Resources
Expand Down
23 changes: 22 additions & 1 deletion jquery.simple-text-rotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
var defaults = {
animation: "dissolve",
separator: ",",
speed: 2000
speed: 2000,
shuffle: false
};

$.fx.step.textShadowBlur = function(fx) {
Expand All @@ -25,13 +26,33 @@

$.fn.textrotator = function(options){
var settings = $.extend({}, defaults, options);

var shuffle = function(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};

return this.each(function(){
var el = $(this)
var array = [];
$.each(el.text().split(settings.separator), function(key, value) {
array.push(value);
});

if (settings.shuffle) {
shuffle(array);
}

el.text(array[0]);

// animation option
Expand Down