CSS悬停,在相同方向上的过渡效果

时间:2018-11-07 05:31:22

标签: html css html5 css-transitions

我正在悬停时执行一些基本的CSS填充过渡。我已经以该Codepen为例:https://codepen.io/brandon4117/pen/ihIgE。现在,在悬停时,背景位置会升高以填充div;在悬停时,背景会回落。我想知道如何修改这支笔,使其工作,例如悬停过渡时应该向上而不是向下。

大多数悬停过渡:将鼠标悬停在新的填充顶部->底部。将鼠标悬停在新填充上会删除“底部”->“顶部”。我想在悬停填充top-> bottom上进行操作,在悬停填充时会再次删除top-> bottom。

看看正在使用的CSS:

div {border-style: solid;
border-color: black;
color: black;
padding: 50px;
background-size: 200% 200%;
background-image: 
linear-gradient(to top, #A72424 50%, transparent 50%);
background-position:0 100%;
-webkit-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-moz-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-ms-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-o-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
transition: background-position 300ms, color 300ms ease, border-color 300ms ease;

}

div:hover {color: white;
border-color: #A72424;
background-image: 
linear-gradient(to top, #A72424 50%, transparent 50%);
background-position: 0 0%;
-webkit-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-moz-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-ms-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
-o-transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
transition: background-position 300ms, color 300ms ease, border-color 300ms ease;
}

a {color: black; text-decoration: none;
transition: all 100ms linear;
-webkit-transition: all 100ms linear;
-moz-transition: all 100ms linear;
-ms-transition: all 100ms linear;
-o-transition: all 100ms linear;}

a:hover {color: white;
transition: all 100ms linear;
-webkit-transition: all 100ms linear;
-moz-transition: all 100ms linear;
-ms-transition: all 100ms linear;
-o-transition: all 100ms linear;
}

a:active {color: white;}

谢谢

2 个答案:

答案 0 :(得分:0)

如果您不使用自闭标签,我认为使用:before或:after伪元素而不是使用背景来实现这种效果会更容易。

div { padding: 50px; border: 2px solid #000; color: #000; position: relative; transition: 0.3s ease; }
div:after { content: ""; height: 0; width: 100%; background-color: #A72424; position: absolute; top: 0; left: 0; transition: 0.3s ease; }
div:hover { color: #fff; border-color: #A72424; }
div:hover:after { height: 100%; }

如果您需要使用线性渐变,则只需将线性渐变方向更改为“到底部”,而不是“到顶部”

div { background-image: linear-gradient(to bottom, #A72424 50%, transparent 50%); }
div:hover { background-image: linear-gradient(to bottom, #A72424 50%, transparent 50%); }

答案 1 :(得分:-1)

好吧,我得到了答案:自上而下地做到这一点:

.divclass::after {
    position: absolute;
    transition: 0.3s;
    content: '';
    height: 0;
    top: 50%;
    right: 0;
    width: 3px;
    background: #fff;
    bottom: 0;
    top: auto;
    z-index: -1;
    width: 120%;
}



.divclass:hover::after {
    height: 100%;
    top: 0;
}

感谢您指向伪造Frderico的方向