How do I make text inside the title tag animate using JavaScript?

前端 未结 3 367
谎友^
谎友^ 2020-12-15 14:15

How do I show a scrolling (moving) message in the title?

 Welcome to Some title

Translate the titlebar into a dy

3条回答
  •  不知归路
    2020-12-15 15:14

    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, 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).

    $(function() {
    
    var origTitle, animatedTitle, timer;
    
    function animateTitle(newTitle) {
      var currentState = false;
      origTitle = document.title;  // save original title
      animatedTitle = "Hey There! " + origTitle;
      timer = setInterval(startAnimation, 2000);
    
      function startAnimation() {
        // animate between the original and the new title
        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();
    });
    
    });
    

提交回复
热议问题