如何使用JavaScript在title标签内创建文本?

时间:2014-05-19 06:50:14

标签: javascript html html5

如何在标题中显示滚动(移动)消息?

 <title>Welcome to Some title</title>

将标题栏翻译成动态,使用JavaScript(不带任何CSS)显示其他信息。

4 个答案:

答案 0 :(得分:5)

您可以通过JavaScript在标题栏文本中添加选框。请在博客文章 Add Scrolling Marquee Effects Text to Title Bar 中查看。

该页面的未修改内容,格式除外:

/*
    Now you can add moving text to title bar of browser for your website or blog.
    Here is the code to do this. Add this code in your website or blog in a widget
    (after replacing YOUR TEXT with your desired text).
*/

<script language=javascript>
    var rev = "fwd";
    function titlebar(val){
        var msg  = "YOUR TEXT";
        var res = " ";
        var speed = 100;
        var pos = val;
        msg = "   |-"+msg+"-|";
        var le = msg.length;
        if(rev == "fwd"){
            if(pos < le){
                pos = pos+1;
                scroll = msg.substr(0,pos);
                document.title = scroll;
                timer = window.setTimeout("titlebar("+pos+")",speed);
            }
            else {
                rev = "bwd";
                timer = window.setTimeout("titlebar("+pos+")",speed);
            }
        }
        else {
            if(pos > 0) {
                pos = pos-1;
                var ale = le-pos;
                scrol = msg.substr(ale,le);
                document.title = scrol;
                timer = window.setTimeout("titlebar("+pos+")",speed);
            }
            else {
                rev = "fwd";
                timer = window.setTimeout("titlebar("+pos+")",speed);
            }
        }
    }
    titlebar(0);
</script>

答案 1 :(得分:1)

这是另一个。只是向前走...... 使用:链接到文件并写下这行代码

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

第一个参数是所需的文本,下一个是更新间隔,10是可见字母的数量......

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;
}

答案 2 :(得分:1)

这是我的:

function animateTitle(Title = "Hello, World!", delay = 300) {
    let counter = 0;
    let direction = true;
    aniTitle = setInterval(function () {
        if (counter == Title.length)
            direction = false;
        if (counter == false)
            direction = true;
        counter = (direction == true) ? ++counter : --counter;
        newtitle = (counter == 0) ? "" : Title.slice(0, counter);
        document.title = newtitle;
    }, delay)
}

答案 3 :(得分:0)

这是一个引人注目的示例,当您的网页标签在浏览器中不活动时(onblur),您的访问者会回来。此脚本将使用简介为原始标题文本设置动画,当选项卡返回活动状态(焦点)时,将恢复原始标题文本。单击选项卡后,将恢复原始页面标题。对于社交媒体分享,强烈建议将原始页面标题文本与前面的动画文本(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();
});

});