标题消息就像Stack Overflow一样

Aks*_*hit 45 html javascript css header

这是我第一次访问堆栈溢出,我看到一个漂亮的标题消息,显示文本和关闭按钮.

标题栏是固定的,非常适合引起访客的注意.我想知道你们中是否有人知道代码来获得相同类型的标题栏.

Jam*_*mes 25

快速纯JavaScript实现:

function MessageBar() {
    // CSS styling:
    var css = function(el,s) {
        for (var i in s) {
            el.style[i] = s[i];
        }
        return el;
    },
    // Create the element:
    bar = css(document.createElement('div'), {
        top: 0,
        left: 0,
        position: 'fixed',
        background: 'orange',
        width: '100%',
        padding: '10px',
        textAlign: 'center'
    });
    // Inject it:
    document.body.appendChild(bar);
    // Provide a way to set the message:
    this.setMessage = function(message) {
        // Clear contents:
        while(bar.firstChild) {
            bar.removeChild(bar.firstChild);
        }
        // Append new message:
        bar.appendChild(document.createTextNode(message));
    };
    // Provide a way to toggle visibility:
    this.toggleVisibility = function() {
        bar.style.display = bar.style.display === 'none' ? 'block' : 'none';
    };
}
Run Code Online (Sandbox Code Playgroud)

如何使用它:

var myMessageBar = new MessageBar();
myMessageBar.setMessage('hello');
// Toggling visibility is simple:
myMessageBar.toggleVisibility();
Run Code Online (Sandbox Code Playgroud)

  • 切换可见性对我来说似乎是一个更直观的API; 我希望一条空白消息显示为空白消息,而不是根本没有酒吧. (2认同)

Sar*_*raz 11

更新:


查看演示

使用代码:

$(function(){
  $('#smsg_link').click(function(){
  showMessage('#9BED87', 'black', 'This is sample success message');
  return false;
});

$('#imsg_link').click(function(){
  showMessage('#FFE16B', 'black', 'This is sample info message');
  return false;
});

$('#emsg_link').click(function(){
  showMessage('#ED869B', 'black', 'This is sample error message');
  return false;
});
});



/*
showMessage function by Sarfraz:
-------------------------
Shows fancy message on top of the window

params:
  - bgcolor:     The background color for the message box
  - color:     The text color of the message box
  - msg:       The message text
*/
var interval = null;

function showMessage(bgcolor, color, msg)
{
    $('#smsg').remove();
    clearInterval(interval);

    if (!$('#smsg').is(':visible'))
    {
        if (!$('#smsg').length)
        {
            $('<div id="smsg">'+msg+'</div>').appendTo($('body')).css({
                position:'fixed',
                top:0,
                left:0,
                width:'98%',
                height:'30px',
                lineHeight:'30px',
                background:bgcolor,
                color:color,
                zIndex:1000,
                padding:'10px',
                fontWeight:'bold',
                fontSize:'18px',
                textAlign:'center',
                opacity:0.8,
                margin:'auto',
                display:'none'
            }).slideDown('show');

            interval = setTimeout(function(){
                $('#smsg').animate({'width':'hide'}, function(){
                    $('#smsg').remove();
                });
            }, 3000);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你想创建自己的,请查看slideTogglejQuery 的功能.


Ken*_*ing 4

相关的CSS将包括:

position: fixed;
top: 0;
width: 100%;
Run Code Online (Sandbox Code Playgroud)

有关以下内容的更多信息position:fixed

带有position:fixed的元素定位在相对于浏览器窗口的指定坐标处。元素的位置由“left”、“top”、“right”和“bottom”属性指定。无论滚动如何,元素都会保留在该位置。适用于 IE7(严格模式)

如果 IE6 支持对您很重要,您可能希望研究解决方法