Can Twitter Bootstrap alerts fade in as well as out?

前端 未结 10 495
夕颜
夕颜 2020-12-07 09:28

When I first saw the alerts in Bootstrap I thought they would behave like the modal window does, dropping down or fading in, and then fading out when closed. But it seems li

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 10:09

    I don't agree with the way that Bootstrap uses fade in (as seen in their documentation - http://v4-alpha.getbootstrap.com/components/alerts/), and my suggestion is to avoid the class names fade and in, and to avoid that pattern in general (which is currently seen in the top-rated answer to this question).

    (1) The semantics are wrong - transitions are temporary, but the class names live on. So why should we name our classes fade and fade in? It should be faded and faded-in, so that when developers read the markup, it's clear that those elements were faded or faded-in. The Bootstrap team has already done away with hide for hidden, why is fade any different?

    (2) Using 2 classes fade and in for a single transition pollutes the class space. And, it's not clear that fade and in are associated with one another. The in class looks like a completely independent class, like alert and alert-success.

    The best solution is to use faded when the element has been faded out, and to replace that class with faded-in when the element has been faded in.

    Alert Faded

    So to answer the question. I think the alert markup, style, and logic should be written in the following manner. Note: Feel free to replace the jQuery logic, if you're using vanilla javascript.

    HTML

    ×

    Well done! You successfully read this alert message.

    CSS

    .faded {
      opacity: 0;
      transition: opacity 1s;
    }
    

    JQuery

    $('#saveAlert .close').on('click', function () {
    
      $("#saveAlert")
        .addClass('faded');
    
    });
    

提交回复
热议问题