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

前端 未结 3 368
谎友^
谎友^ 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 14:55

    Here's another one. Only goes forward though... To use: Link to the file and write this line of code

    var title = new MovingTitle("Desired title... ", 300, 10);
    title.init();
    

    First parameter is the desired text, next one is the update interval, 10 is the number of visible letters...

    function MovingTitle(writeText, interval, visibleLetters) {
        var _instance = {};
    
        var _currId = 0;
        var _numberOfLetters = writeText.length;
    
        function updateTitle() {
            _currId += 1;
            if(_currId > _numberOfLetters - 1) {
                _currId = 0; 
            }
    
            var startId = _currId;
            var endId = startId + visibleLetters;
            var finalText;
            if(endId < _numberOfLetters - 1) {
                finalText = writeText.substring(startId, endId);
            } else {
                var cappedEndId = _numberOfLetters;
                endId = endId - cappedEndId;
    
                finalText = writeText.substring(startId, cappedEndId) +     writeText.substring(0, endId);
            }
    
            document.title = finalText; 
        }
    
        _instance.init = function() {
            setInterval(updateTitle, interval);
        };
    
        return _instance;
    }
    

提交回复
热议问题