在一个 div 上使用两个按钮,你如何慢慢淡入然后淡出?

Using two buttons on one div, how do you fade in slowly then fade out?

淡出动画不工作。我认为您不能将两个 ID 放在一个 div.
如何使淡出起作用?
here is the jsFiddle


html

<a href="#pop">appear</a>

<div id="pop" id="pop_close">
    <a href="#pop_close">disappear</a>
</div>

CSS

body {
    padding: 10em;
}
#pop{
    height: 10em;
    width: 10em;
    background: yellow;
    opacity:0;
    position: relative;
    top: -20px;
    z-index: -1;
}

#pop:target {
    opacity: 1;
    z-index: 1;
    -webkit-animation: fadein 1s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 1s; /* Firefox < 16 */
        -ms-animation: fadein 1s; /* Internet Explorer */
         -o-animation: fadein 1s; /* Opera < 12.1 */
            animation: fadein 1s;
}

#pop_close:target {
    opacity: 0;
    z-index: -1;
    -webkit-animation: fadeOut 1s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadeOut 1s; /* Firefox < 16 */
        -ms-animation: fadeOut 1s; /* Internet Explorer */
         -o-animation: fadeOut 1s; /* Opera < 12.1 */
            animation: fadeOut 1s;
}



@keyframes fadeOut {
    from { opacity: 1; }
    to   { opacity: 0; }
}

/* Firefox < 16 */
@-moz-keyframes fadeOut {
    from { opacity: 1; }
    to   { opacity: 0; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadeOut {
    from { opacity: 1; }
    to   { opacity: 0; }
}

/* Internet Explorer */
@-ms-keyframes fadeOut {
    from { opacity: 1; }
    to   { opacity: 0; }
}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

这是一个有效的 fiddle http://jsfiddle.net/oogley_boogley/wqju9jh6/19/

css:

#pop{
height: 10em;
width: 10em;
background: yellow;
opacity:0;
position: relative;
top: -20px;
z-index: -1;
transition: opacity 1s;  /* key style here, look into -webkit and -moz etc as well */
}

确切地说,同一页面上不能有多个 ID
因此您需要修改 ID 以使 :target 成为 特定的 弹出窗口:

div[id^=popUp] // means: target DIV element who's ID "starts with".

与 HTML 相比,您只需添加一个数字即可使 ID 独一无二。

div[id^=popUp]{
  height: 5em;
  width: 5em;
  background: yellow;
  opacity:0;
  position: relative;
  top: -20px;
  z-index: -1;
  transition: opacity 1s;
}

div[id^=popUp]:target {
  opacity: 1;
  z-index: 1;
}
<a href="#popUp1">appear</a>
<div id="popUp1">
  <a href="#">disappear</a>
</div>

<a href="#popUp2">appear</a>
<div id="popUp2">
  <a href="#">disappear</a>
</div>