【问题标题】:Simple way to show a message bar at the top of a page在页面顶部显示消息栏的简单方法
【发布时间】:2011-07-15 10:40:36
【问题描述】:

我想实现类似 stackoverflow 的东西,页面顶部的栏显示一些消息。

我也遇到了这个非常不错的页面反弹效果:

http://www.slidedeck.com/features/(看下面的紫色顶栏)

有没有简单的方法来做到这一点?也许只有 jQuery 或其他框架?

【问题讨论】:

  • 具体做什么?如果它做了很多,它需要很多代码!
  • 我也有同样的问题,但最后还是写了几行简单的 css & jquery。我不想过多依赖库。
  • @abiusx:我贴了一个链接,如果你点击它,你可以看到它到底做了什么
  • @ngduc:可以发一下吗?
  • @yes123 你好吧。我还有一些邀请,你要邀请吗?

标签: javascript html css javascript-framework


【解决方案1】:

How about this? :) 只需添加一些精美的图形就可以了!

【讨论】:

  • 嗯,看起来很不错,我明天会测试它,因为现在我是 o。 Iphone,它似乎没有像我发布的示例那样向下滚动页面的其余部分
  • 这是我问的最接近的事情..即使效果发生在页面上
  • 我还需要包含那个 jquery.easing.1.3.js 吗?那是什么
  • @yes123 是的,因为原版 jQuery 仅具有“摇摆”和“线性”效果,您需要将其包含在内才能获得您想要的有弹性的效果。 (或者您可以只包含您想要的一种特定效果)
  • 这是一个 jq 插件 - jquery 使用 protoypal 继承来扩展功能。它负责 penner 缓动方程。只需在标记中的 jquery 之后链接它即可。
【解决方案2】:

我刚刚从blog.grio.com找到了一个很棒且简单的解决方案

jsFiddle Demo

function showNotificationBar(message, duration, bgColor, txtColor, height) {

    /*set default values*/
    duration = typeof duration !== 'undefined' ? duration : 1500;
    bgColor = typeof bgColor !== 'undefined' ? bgColor : "#F4E0E1";
    txtColor = typeof txtColor !== 'undefined' ? txtColor : "#A42732";
    height = typeof height !== 'undefined' ? height : 40;
    /*create the notification bar div if it doesn't exist*/
    if ($('#notification-bar').size() == 0) {
        var HTMLmessage = "<div class='notification-message' style='text-align:center; line-height: " + height + "px;'> " + message + " </div>";
        $('body').prepend("<div id='notification-bar' style='display:none; width:100%; height:" + height + "px; background-color: " + bgColor + "; position: fixed; z-index: 100; color: " + txtColor + ";border-bottom: 1px solid " + txtColor + ";'>" + HTMLmessage + "</div>");
    }
    /*animate the bar*/
    $('#notification-bar').slideDown(function() {
        setTimeout(function() {
            $('#notification-bar').slideUp(function() {});
        }, duration);
    });
}

【讨论】:

    【解决方案3】:

    var _show = true;
    $(document).ready(function() {
    
      $('button#showHide')
        .bind('click', function() {
          if (_show) {
            $('div#hideMe')
              .animate({
                'height': '25px'
              }, 750);
            _show = false;
          } else {
            $('div#hideMe')
              .animate({
                'height': '0px'
              }, 750);
            _show = true;
          }
        });
    });
    body {
      background-color: #003366;
      padding: 0px;
      margin: 0px;
      text-align: center;
    }
    
    button {
      cursor: pointer;
      right: 5px;
      float: right;
      position: relative;
      top: 5px;
    }
    
    div#hideMe {
      background-color: #FF3399;
      height: 0px;
      overflow: hidden;
      position: relative;
    }
    
    div#container {
      background-color: #FFFFFF;
      border: #FFFF00 1px solid;
      height: 600px;
      margin-left: auto;
      margin-right: auto;
      overflow: hidden;
      position: relative;  
    }
    
    div#contents {
      height: 600px;
      position: absolute;
      width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    <div id="hideMe">Congratulations, you just won a punch in the neck!</div>
    <div id="container">
      <div id="contents">
        <button id="showHide">clicker</button>
      </div>
    </div>

    只是在玩这个。关于您的示例所做的事情。 :)

    • 您可以通过大约 17,334,259 种不同的方式来执行此操作。但这会奏效。只要确保你的盒子相对定位,#hideMe 的扩展也会将#container 向下推。或者绝对定位并将其固定为 0px,0px。或者什么...

    【讨论】:

    • 嗯,这是一个很好的起点,我在这里复制了您的代码:jsfiddle.net/7DHqR 现在为什么我们不尝试添加slidedeck.com/features 的反弹效果?同时+1
    • 对于反弹缓动,你只需要实现许多缓动插件之一,然后它就像 $('div#hideMe').animate({'height' : '25px'} ,{ 持续时间:500, 缓动:"bounceEaseOut"}); (匹配插件的语法)...
    【解决方案4】:

    您可能需要通过 Ajax 获取要显示的消息,但是:

    http://jsfiddle.net/ZpBa8/4/

    展示了如何在 jQuery 的顶部显示一个条形图,并且是一个开始

    【讨论】:

    • 那太糟糕了,你刚刚重新实现了 togle jquery 方法。我建议您从这里了解更多信息:api.jquery.com/toggle 和 btw yuor 代码 deosn't do what I ask
    【解决方案5】:

    制作您喜欢的页面的插件的人制作插件来做您喜欢的事情:http://www.hellobar.com/

    【讨论】:

    • 抱歉,这只是邀请?
    【解决方案6】:

    Meerkat jQuery plugin 做得非常好。

    【讨论】:

      【解决方案7】:

      这甚至可以在没有 jquery 的情况下轻松完成。只需使用 DOM 将 div 元素附加到 body 并将其顶部位置设置为零。将其宽度设置为 screen.width,高度设置为 50px。并且只是启动一个不透明度淡入/淡出。像下面的示例。此示例适用于 IE。阅读this 以供参考。调用 initFade 成为 Fade In 和 Fade out 进程。

      var OP = 90;
      function processFade() {
          var t = "";
          OP = OP - 3;
          if (OP < 0) {
      
              clearTimeout(t);
              OP = 90;
              return;
          }
          $("YOUR_DIV_ELEMENT_HERE").style.filter = "alpha(opacity=" + OP + ")";
          if (OP == 0) {
              $("YOUR_DIV_ELEMENT_HERE").style.display = "none";
              clearTimeout(t);
              OP = 90;
              return;
          }
          t = setTimeout("processFade();", 100);
      }
      function initFade() {
          processFade();
      }
      

      【讨论】:

      • 谢谢,但我也问了如何反弹 :) 当栏显示时查看页面反弹:slidedeck.com/features 是不是太难了?
      • 反弹也行吧?只需重复上述过程,而不是 style.filter 用 style.top 替换它(不要忘记将位置设置为绝对)并有一个运行变量(就像上面的 OP)直到你想要的任何高度。在您清除超时后但在上述函数中返回之前调用此新函数。​​这样会出现淡入,然后立即弹跳(并且不会减慢)。试试看,让我知道。否则将粘贴组合代码(非 jquery)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多