【问题标题】:Creating a mixin that notifies a banner at the top of the page创建一个在页面顶部通知横幅的 mixin
【发布时间】:2015-04-09 04:06:46
【问题描述】:

我有一个页面,每当其他用户广播消息时,它都会创建一个弹出通知。弹出窗口基本上是一个 div,其中包含来自已发送消息的内容,并带有一个允许您关闭它的按钮。

现在我已经研究了一百万种方法来创建使用伪类扩展属性的转换,但我的问题是:是否可以在页面顶部创建一个横幅,当消息由另一个发送时向下滑动使用纯 CSS3 / 过渡混合?

我的想法是转换 max-height 属性,但显然它不起作用。我假设是因为一开始就没有高度。

CSS:

.banner-messages {
 > div {
  background-color: #74bce7;
  color: #fff;
  top: 0; 
  left: 0;
  padding: 20px 30px;
  position: relative; 
  line-height: 100%;    
  width: 100%;
  max-height: 0;
  z-index: 1000; 
  overflow-y: hidden;

  @include drop-down;
  }
}

@mixin drop-down {
  max-height: 200px;
  -webkit-transition: all 1.5s linear;
  -moz-transition: all 1.5s linear;
  -o-transition: all 1.5s linear;
  transition: all 1.5s linear;
}

HTML:

<div id="test" class="banner-messages" data-bind="foreach: messages">
<div class="banner-message">
    <img data-bind="imgSrc: 'radio.svg'">

    <div class="banner-body">
        <p class="banner-title">{{title}}</p>
        <pre>{{message}}</pre>
    </div>

    <span class="exit-dialout-panel icon icon-closes" data-bind="visible: dismissable, click: $parent.dismissMessage.bind($parent, $data)"></span>
</div>

【问题讨论】:

    标签: html css css-transitions


    【解决方案1】:

    尝试添加一个具有css 绑定的类,该绑定适用transform: translateY

    var Viewmodel = function Viewmodel() {
      var that = this;
      
      this.messages = ko.observableArray([]);
      
      this.removeMessages = function removeMessages() {
        that.messages.removeAll();
      };
      
      this.addMessage = function addMessage() {
        that.messages.push("A new message came in");
      };
    };
    
    ko.applyBindings( new Viewmodel() );
    header {
      transition: transform 0.3s;
      transform: translateY(-110%);
      background-color: lightblue;
      padding: 2rem;
      font-family: sans-serif;
    }
    
    header.active {
      transform: translateY(0);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <main>
      <header data-bind="css: { active: messages().length > 0 }">
        <ul data-bind="foreach: messages">
          <li data-bind="text: $data"></li>
        </ul>
        <button data-bind="click: removeMessages">mark all as read</button>
      </header>
    </main>
    
    <button data-bind="click: addMessage">Add message</button>

    【讨论】:

    • transform 与 translateY 完美配合!我以后肯定会尝试这种方法。但是,这会上下切换消息,而在现实生活中,消息将由发送者动态发送,然后在发送者不想再看到时由发送者关闭。我没有使用 jQuery,可能不会用于这个项目,但对于过渡仍然是 +1。谢谢。
    • @user3802738 我无法完全从您的问题中收集到您遇到问题的领域。我专注于 CSS,仅将 jQ 用于演示目的;给我一分钟,我会把它改写成一个基于 Knockout 的解决方案。编辑:完成!
    • 快又快。感谢您关闭此门并提供示例!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多