【问题标题】:Flash/blink tab effect in JavascriptJavascript 中的 Flash/blink 选项卡效果
【发布时间】:2015-12-14 18:44:01
【问题描述】:

我正在用 Javascript/PHP 创建一个简单的聊天。例如,当新消息出现在 Facebook 上时,我想闪烁/闪烁选项卡。我该怎么做?

【问题讨论】:

标签: javascript jquery chat


【解决方案1】:

这里是示例代码:

(function () {

var original = document.title;
var timeout;

window.coders = function (newMsg, howManyTimes) {
    function step() {
        document.title = (document.title == original) ? newMsg : original;

        if (--howManyTimes > 0) {
            timeout = setTimeout(step, 1000);
        };
    };

    howManyTimes = parseInt(howManyTimes);

    if (isNaN(howManyTimes)) {
        howManyTimes = 5;
    };

    cancelcoders(timeout);
    step();
};

window.cancelcoders = function () {
    clearTimeout(timeout);
    document.title = original;
};

}());

您可以使用以下代码:

coders("New Message from Bhavin Solanki");

...或者...

coders("New Message from Bhavin Solanki", 20); // toggles it 20 times.

【讨论】:

  • 非常感谢,为什么我的问题有3个缺点?
  • 不知道...可能是某些审稿人出于某种原因投了反对票。
  • 如果我的回答对你有用,你可以投赞成票并接受它
  • 我不能因为它需要 15 的声望。我是新来的。
【解决方案2】:

你最好在 css 中制作动画,并且只使用 javascript 来启动和停止动画。你被否决了,因为你没有展示你对解决方案的尝试。

(function(){
  var message = document.querySelector('.message'),
      button = document.querySelector('#button');

  button.addEventListener('click', blink, false);

  // this is where you toggle the class
  function blink(e){
    message.classList.toggle('blink');
  }
})();
@keyframes blink {
    from {
      background-color: red;
      color: white;
    }
    to {
      background-color: white;
      color: red;
    }
}

.message {
  text-align: center;
}

/* run the animation on .message when it also has the class .blink */
.message.blink {
  animation: blink 1s linear infinite alternate;
}
<div class="message">You've got a message</div>
<button id="button">blink</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2013-04-03
    相关资源
    最近更新 更多