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

Rotations can be limited in numbers. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
135 changes: 92 additions & 43 deletions jquery.simple-text-rotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,51 @@
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* A very simple and light weight jQuery plugin that
* allows you to rotate multiple text without changing
* A very simple and light weight jQuery plugin that
* allows you to rotate multiple text without changing
* the layout
* https://github.com/peachananr/simple-text-rotator
*
* ========================================================== */

!function($){

var defaults = {
animation: "dissolve",
separator: ",",
speed: 2000
};

$.fx.step.textShadowBlur = function(fx) {
animation: "dissolve",
separator: ",",
speed: 2000,
repetitions: -1
};

$.fx.step.textShadowBlur = function(fx) {
$(fx.elem).prop('textShadowBlur', fx.now).css({textShadow: '0 0 ' + Math.floor(fx.now) + 'px black'});
};

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

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

var arrayLength = array.length;

// animation option
var rotate = function() {
switch (settings.animation) {
switch (settings.animation) {
case 'dissolve':
var initial = el.text();
var index = $.inArray(initial, array);
if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}
el.animate({
textShadowBlur:20,
opacity: 0
Expand All @@ -50,16 +61,22 @@
}, 500 );
});
break;

case 'flip':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}

var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1

if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}

el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
Expand All @@ -69,18 +86,24 @@
"-o-transform": " rotateY(-180deg)",
"transform": " rotateY(-180deg)"
})

break;

case 'flipUp':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}

var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1

if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}

el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
Expand All @@ -90,18 +113,24 @@
"-o-transform": " rotateX(-180deg)",
"transform": " rotateX(-180deg)"
})

break;

case 'flipCube':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}

var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1

if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}

el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
Expand All @@ -111,18 +140,24 @@
"-o-transform": " rotateY(180deg)",
"transform": " rotateY(180deg)"
})

break;

case 'flipCubeUp':
if(el.find(".back").length > 0) {
el.html(el.find(".back").html())
}

var initial = el.text()
var index = $.inArray(initial, array)
if((index + 1) == array.length) index = -1

if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}

el.html("");
$("<span class='front'>" + initial + "</span>").appendTo(el);
$("<span class='back'>" + array[index + 1] + "</span>").appendTo(el);
Expand All @@ -132,37 +167,51 @@
"-o-transform": " rotateX(180deg)",
"transform": " rotateX(180deg)"
})

break;

case 'spin':
if(el.find(".rotating").length > 0) {
el.html(el.find(".rotating").html())
}
index = $.inArray(el.text(), array)
if((index + 1) == array.length) index = -1

if(index+1 == arrayLength){
index = -1;
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}

el.wrapInner("<span class='rotating spin' />").find(".rotating").hide().text(array[index + 1]).show().css({
"-webkit-transform": " rotate(0) scale(1)",
"-moz-transform": "rotate(0) scale(1)",
"-o-transform": "rotate(0) scale(1)",
"transform": "rotate(0) scale(1)"
})
break;

case 'fade':
el.fadeOut(settings.speed, function() {
var initial = el.text();
var index = $.inArray(initial, array);
if(index+1 == arrayLength){
if(--settings.repetitions == 0){
clearInterval(simpleTextRotator);
break;
}
}
el.fadeOut(settings.speed/2, function() {
index = $.inArray(el.text(), array)
if((index + 1) == array.length) index = -1
el.text(array[index + 1]).fadeIn(settings.speed);
el.text(array[index + 1]).fadeIn(settings.speed/2);
});
break;
}
};
setInterval(rotate, settings.speed);
var simpleTextRotator = setInterval(rotate, settings.speed);
});
}

}(window.jQuery);