摘要
- 目标设备 (设备宽度+600像素-请 忽略移动/xs屏幕)
- 纯CSS 图片悬停效果使用
:hover:after
- 可用但不理想
- 问题:
- 图像比例(图像将是800px/600px)未得到维护-宽度被裁剪而不是图像缩小
- 图片位于主容器之外
.outercontainer
:hover
工作的 图像淡入效果,但在:hover
状态结束后不会淡出。我尝试使用单独动画/相同动画的反向运行,但没有成功
期望效果:
所有内容适应带有蓝色边框标记的容器
.outercontainer
编辑: 图像应该有自己的空间-大约为容器宽度的80%,选项/菜单将占据另外20%的宽度-不重叠(如果图像覆盖选项,平板电脑用户将被困在:hover状态中)
.outercontainer
和其内部的所有内容根据屏幕宽度自动调整支持的最小设备宽度为600像素(忽略小于600像素的屏幕)
步骤:
- 图像在
:hover
时淡入 - 只要元素仍处于
:hover
状态,图像保持可见 - 在取消悬停后,图像淡出
- 图像在
类似帖子/问题
用CSS缩放图像以适应图像的最大边缘 (将图像作为元素而不是假元素的背景进行处理)
无法使图像适合div (与上面的链接相同)
按比例缩放所有图像以适应容器(将图像作为元素的背景而不是假元素的背景进行处理)
在SO上有很多类似的问题。我没有找到一个解决将图像作为假元素的背景的问题的帖子。
我的问题
如何使使用:hover:after显示的图像适应其父容器? (我希望它不超过该容器宽度的80%-85% - 其余空间应分配给选项/菜单/列表项)
如何使使用:hover:after
/* Start Miscellaneous stuff */ body { background: #131418; color: #999; text-align: center; } .optionlist a { color: #999; text-decoration: none; } @keyframes mycoolfade { from { opacity: 0; } to { opacity: 1; } } /* End Miscellaneous stuff */ .outercontainer { border: 1px solid blue; /*This needs to be in the middle of the screen and everthing should fit inside it*/ position: fixed; /*border to highlight only - not part of final product*/ top: 50%; left: 50%; transform: translate(-50%, -50%); width: 1200px; max-width: 80% } .optioncontainer { width: 250px; max-width: 20vw; } .optionlist li { background: #fff; padding: .5em; box-shadow: inset 10px 10px 100px 25px rgba(0, 0, 0, .8); list-style-type: none; } .optionlist li:nth-child(odd) { background: #000; } .optionlist li:hover { background: red; } .optionlist li:hover:after { /* The part the needs to be fixed */ content: ''; width: 800px; height: 600px; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); animation: mycoolfade 1s; } /* Start Background Control */ #red:after {background: url(http://lorempixel.com/800/600/)} #blue:after {background: url(http://lorempixel.com/800/601/)} #green:after {background: url(http://lorempixel.com/801/600/)} #yellow:after {background: url(http://lorempixel.com/801/601/)} #orange:after {background: url(http://lorempixel.com/799/600/)} #white:after {background: url(http://lorempixel.com/800/599/)} #black:after {background: url(http://lorempixel.com/801/599/)} /* End Background Control */
<body> The initial hiccup when the images load is not an issue <div class="outercontainer"> <div class="optioncontainer"> <div class="optionlist"> <li id="red">Red</li> <li id="blue">Blue</li> <li id="green">Green</li> <li id="yellow">Yellow</li> <li id="orange">Orange</li> <li id="white">White</li> <li id="black">Black</li> </div> </div> </div> </body>
谢谢。