Skip to content

Instantly share code, notes, and snippets.

@mbohanon
Created October 26, 2017 22:12
Show Gist options
  • Save mbohanon/6fcf24c209ab28cffcb2be9100fe8301 to your computer and use it in GitHub Desktop.
Save mbohanon/6fcf24c209ab28cffcb2be9100fe8301 to your computer and use it in GitHub Desktop.
Loop through array and animate title text when page tab is not active in browser (onblur), this is NOT a marquee.
//Here's an eye catching example to get your visitors back when your web page tab is not active within the browser (onblur). This script will animate the original title text with an intro looping through an array of text, the original title text is restored when the tab is returned to active state (focus). When the tab is clicked the original page title is restored. For social media sharing it is highly recommended to include the original page title text with the prefaced animated text (onblur).
//Created by SHEmedia.us
$(function() {
var origTitle, timer;
function animateTitle(newTitle) {
var currentState = false;
origTitle = document.title; // save original title
var index = 0;
timer = setInterval(startAnimation, 2000);
function startAnimation() {
// animate between the original and the new title
var animatedTitle = ["Foo " + origTitle,"Bar " + origTitle,"Batz " + origTitle];
$("title").animate({
opacity:0
},function() {
if(animatedTitle.length > index) {
$(this).text(animatedTitle[index]).animate({opacity:1})
index++;
} else
index = 0;
});
}
document.title = currentState ? origTitle : animatedTitle;
currentState = !currentState;
}
function restoreTitle() {
clearInterval(timer);
document.title = origTitle; // restore original title
}
// Change page title on blur
$(window).blur(function() {
animateTitle();
});
// Change page title back on focus
$(window).focus(function() {
restoreTitle();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment