纯 CSS-made 熔岩灯效果:如何在活动菜单项上保持熔岩灯?
Pure CSS-made LavaLamp-effect: How to hold the LavaLamp on active menuitem?
我使用这种纯 CSS 制作的 LavaLamp 效果,我对它几乎很满意:
HTML:
<ul>
<li><a href="#">✿</a></li><!--
--><li><a href="#">Lorem</a></li><!--
--><li><a href="#">Ipsum</a></li><!--
--><li><a href="#">Consectetur adipisicing</a></li><!--
--><li><a href="#">Sit amet</a></li>
</ul>
CSS:
html{
font-family:sans-serif;
font-size:1.2em;
background:#eee;
}
ul{
position:relative;
width:27em;height:2em;
margin:100px auto;
padding:0;
white-space:nowrap;
}
ul li{
display:inline;
text-align:center;
}
ul li a{
position:relative;
top:0;left:0;right:25em;bottom:0;
display:inline-block;
-moz-box-sizing:border-box;
box-sizing:border-box;
padding:.4em .2em;
color:#09C;
text-decoration:none;
text-shadow:0 1px 0 white;
/*transition*/
-webkit-transition:width .3s,right .3s;
-moz-transition:width .3s,right .3s;
-o-transition:width .3s,right .3s;
transition:width .3s,right .3s;
}
ul li:nth-child(1) a{
width:2em;
}
ul li:nth-child(2) a{
width:4em;
}
ul li:nth-child(3) a{
width:4em;
}
ul li:nth-child(4) a{
width:12em;
}
ul li:nth-child(5) a{
width:5em;
}
ul li:last-child a::after{
content:"";
position:absolute;
right:inherit;
bottom:-3px;
width:inherit;
height:3px;
background:#ccc;
pointer-events:none;
/*transition*/
-webkit-transition:all .5s ease;
-moz-transition:all .5s ease;
-o-transition:all .5s ease;
transition:all .5s ease;
}
ul li:nth-child(1) ~ li:last-child a{
right:25em;
width:2em;
}
ul li:nth-child(2):hover ~ li:last-child a{
right:21em;
width:4em;
}
ul li:nth-child(3):hover ~ li:last-child a{
right:17em;
width:4em;
}
ul li:nth-child(4):hover ~ li:last-child a{
right:5em;
width:12em;
}
ul li:nth-child(5):last-child:hover a{
right:0;
width:5em;
}
ul li:hover ~ li:last-child a::after,
ul li:last-child:hover a::after{
background:#c351fa;
}
ul li:last-child a{
min-width:5em;max-width:5em;
}
ul li a:hover,ul li a:focus{
color:#c351fa;
background-color:rgba(255,255,255,.6);
/*transition*/
-webkit-transition:width .3s,right .3s,background-color .3s;
-moz-transition:width .3s,right .3s,background-color .3s;
-o-transition:width .3s,right .3s,background-color .3s;
transition:width .3s,right .3s,background-color .3s;
}
ul li a:focus{
border-bottom:3px solid #c351fa;
}
JSFiddle.net 上的示例:
Click to see the example above on JSFiddle.net
不幸的是,当其他菜单项被点击时,LavaLamp 跳回到原始菜单项,因此被设置为 .active-state。如何保持当前活动菜单项的效果(根据当前 .active-child)?
非常感谢您的帮助! :-)
此处适用的解决方案是为每个 link 添加单选按钮。然后,通过 ~ 选择器和 input:checked 属性,您可以操纵 link 停留在某个 "state"。您必须包括单选按钮,使其大小与 li 相同,然后将不透明度设置为 0。这样,您可以在选择 link 时调整下划线的位置,因为复选框仍然存在检查,即使用户停止将鼠标悬停在 link 上。请务必为所有复选框赋予相同的名称 - 属性,这样如果另一个 link 被选中,则前一个复选框将被取消选中!如果您还有其他问题,我可以查看是否可以编辑您的 fiddle,但是您的代码非常广泛!
但是你的作品非常令人印象深刻,看起来非常酷!
我使用这种纯 CSS 制作的 LavaLamp 效果,我对它几乎很满意:
HTML:
<ul>
<li><a href="#">✿</a></li><!--
--><li><a href="#">Lorem</a></li><!--
--><li><a href="#">Ipsum</a></li><!--
--><li><a href="#">Consectetur adipisicing</a></li><!--
--><li><a href="#">Sit amet</a></li>
</ul>
CSS:
html{
font-family:sans-serif;
font-size:1.2em;
background:#eee;
}
ul{
position:relative;
width:27em;height:2em;
margin:100px auto;
padding:0;
white-space:nowrap;
}
ul li{
display:inline;
text-align:center;
}
ul li a{
position:relative;
top:0;left:0;right:25em;bottom:0;
display:inline-block;
-moz-box-sizing:border-box;
box-sizing:border-box;
padding:.4em .2em;
color:#09C;
text-decoration:none;
text-shadow:0 1px 0 white;
/*transition*/
-webkit-transition:width .3s,right .3s;
-moz-transition:width .3s,right .3s;
-o-transition:width .3s,right .3s;
transition:width .3s,right .3s;
}
ul li:nth-child(1) a{
width:2em;
}
ul li:nth-child(2) a{
width:4em;
}
ul li:nth-child(3) a{
width:4em;
}
ul li:nth-child(4) a{
width:12em;
}
ul li:nth-child(5) a{
width:5em;
}
ul li:last-child a::after{
content:"";
position:absolute;
right:inherit;
bottom:-3px;
width:inherit;
height:3px;
background:#ccc;
pointer-events:none;
/*transition*/
-webkit-transition:all .5s ease;
-moz-transition:all .5s ease;
-o-transition:all .5s ease;
transition:all .5s ease;
}
ul li:nth-child(1) ~ li:last-child a{
right:25em;
width:2em;
}
ul li:nth-child(2):hover ~ li:last-child a{
right:21em;
width:4em;
}
ul li:nth-child(3):hover ~ li:last-child a{
right:17em;
width:4em;
}
ul li:nth-child(4):hover ~ li:last-child a{
right:5em;
width:12em;
}
ul li:nth-child(5):last-child:hover a{
right:0;
width:5em;
}
ul li:hover ~ li:last-child a::after,
ul li:last-child:hover a::after{
background:#c351fa;
}
ul li:last-child a{
min-width:5em;max-width:5em;
}
ul li a:hover,ul li a:focus{
color:#c351fa;
background-color:rgba(255,255,255,.6);
/*transition*/
-webkit-transition:width .3s,right .3s,background-color .3s;
-moz-transition:width .3s,right .3s,background-color .3s;
-o-transition:width .3s,right .3s,background-color .3s;
transition:width .3s,right .3s,background-color .3s;
}
ul li a:focus{
border-bottom:3px solid #c351fa;
}
JSFiddle.net 上的示例:
Click to see the example above on JSFiddle.net
不幸的是,当其他菜单项被点击时,LavaLamp 跳回到原始菜单项,因此被设置为 .active-state。如何保持当前活动菜单项的效果(根据当前 .active-child)?
非常感谢您的帮助! :-)
此处适用的解决方案是为每个 link 添加单选按钮。然后,通过 ~ 选择器和 input:checked 属性,您可以操纵 link 停留在某个 "state"。您必须包括单选按钮,使其大小与 li 相同,然后将不透明度设置为 0。这样,您可以在选择 link 时调整下划线的位置,因为复选框仍然存在检查,即使用户停止将鼠标悬停在 link 上。请务必为所有复选框赋予相同的名称 - 属性,这样如果另一个 link 被选中,则前一个复选框将被取消选中!如果您还有其他问题,我可以查看是否可以编辑您的 fiddle,但是您的代码非常广泛! 但是你的作品非常令人印象深刻,看起来非常酷!