我批注下面有希望解释发生了什么你的风格。
li {
// You're creating a background gradient, where the first 50% is transparent, the next 45% is #a2d39c and the last 5% is #7cc576
background-image:
linear-gradient(to bottom,
transparent 50%,
#a2d39c 50%, #a2d39c 95%, #7cc576 95%);
// The background size is twice the height of your element. Therefore with the 50% transparency and initial position, you're not going to see anything
background-size: 100% 200%;
// This will nicely transition between CSS property values when they change
transition: all .25s ease;
}
li:hover {
// When you hover over your list item, you're changing the position so that the bottom of the background is visible. This causes the 50% transparent portion of the background to disappear, and the coloured portion to slide into view
background-position: bottom center;}
}
后台位置
如果检查出的CSS规范为background-position,你会看到,默认值是0% 0%
,这基本上是top left
。
您的CSS代码没有指定初始背景位置,因此它将默认为top left
。记住这一点。
您的背景渐变定义为to bottom
,因此来自top -> bottom
。前50%是transparent
(隐形)。第二个50%由两种不同的颜色组成。
然后考虑你的背景渐变是你元素高度的两倍。这由background-size: 100% 200%
(100%宽度,200%高度)指定。背景可以大于它所应用的元素,并且任何溢出都将被隐藏。
所以最初当你只显示背景渐变的上半部分时,你会看到什么?只有transparent
部分。
当您在覆盖悬停时覆盖background-position
时,您现在说要显示bottom center
部分。看到你的背景如何匹配你的元素的整个宽度,水平值不会改变任何东西。但是bottom
垂直设置。现在意味着显示第二个50%。
这有帮助吗?
我接受你的解决方案,因为它的确有意义,但我仍然对背景位置感到困惑,你能否解释一下,谢谢 – user2860957
@ user2860957 - 我已经更新了我的答案。这有助于澄清?如果不是,你能解释一些你不明白的东西吗?定位,渐变如何创建等? – fubar
感谢您的帮助,我正在阅读您的解决方案等待 – user2860957