英文:
p:hover::after/::before smooth transition
问题
#projects-grid p:hover::after {
content: " />";
font-size: 1.1rem;
vertical-align: baseline;
color: orange;
transition: ease-in-out 0.3s; /* 将过渡效果添加到这里 */
}
#projects-grid p:hover::before {
content: "< ";
font-size: 1.1rem;
vertical-align: 5%;
color: orange;
transition: ease-in-out 0.3s; /* 将过渡效果添加到这里 */
}
英文:
I am creating a Grid gallery.
Under each img
I have p
with a short name of a picture.
<div id="projects-grid">
<figure class="project-tile">
<a href="">
<img src="Portfolio_project_img_1.png" alt="">
<p>Tribute Page</p>
</a>
</figure>
</div>
Before and after each p
I need these characters to appear when hovering over p
: "<" - before; "/>" - after
I need this transition to be smooth, like ease-in-out, 0.3s
I currently have this CSS code:
#projects-grid p:hover::after {
content: " />";
font-size: 1.1rem;
vertical-align: baseline;
color: orange;
}
#projects-grid p:hover::before {
content: "< ";
font-size: 1.1rem;
vertical-align: 5%;
color: orange;
}
I have tried to add transition
to #projects-grid p:hover::after
and #projects-grid p:hover::before
but it wouldn't work.
So, how can I target specifically those characters in :hover::after
and :hover::before
?
答案1
得分: 1
你需要使用支持渐进过渡的CSS属性。
你的问题可能是你尝试“逐渐使伪元素存在”,这是不起作用的(你可能看到了一些jQuery函数在某种程度上实现了这一点)。
或者你使用了不支持逐渐过渡的CSS属性(visibility
是一个例子。你可以设置 transition:visibility 1s
,但它不会按预期工作 - 它会立刻从 visibility:hidden
切换到 visibility:visible
,而不是在1秒内逐渐过渡)。
我的解决方案是从 opacity:0
过渡到 opacity:1
。
其中一个好处是在悬停时,布局不会跳来跳去。
伪元素已经被渲染,它占据了空间,但在悬停之前它并不在视觉上可见,这使它逐渐变得可见。
#projects-grid p::after {
content: " />";
vertical-align: baseline;
}
#projects-grid p::before {
content: "< ";
vertical-align: 5%;
}
#projects-grid p::before,
#projects-grid p::after {
font-size: 1.1rem;
color: orange;
transition: opacity 0.3s ease;
opacity: 0;
}
#projects-grid p:hover::after,
#projects-grid p:hover::before {
opacity: 1;
}
<div id="projects-grid">
<figure class="project-tile">
<a href="">
<img src="Portfolio_project_img_1.png" alt="">
<p>Tribute Page</p>
</a>
</figure>
</div>
附注:找到支持逐渐过渡的CSS属性列表可能有点棘手。大多数列表,例如 https://www.quackit.com/css/css3/animations/animatable_properties/,包括 visibility
,这有点不准确 - 因为它无法按照你期望的方式逐渐动画,它只会立刻从可见切换到不可见。
英文:
You need to use a CSS property that supports gradual transitions.
Your problem was likely that you tried to "make a pseudo-element exist gradually" which doesn't work. (You might have been looking at some jQuery functions that sort of achieved this)
Or you used a CSS-property which doesn't support gradual transitions. (visibility
is one example. You can set transition:visibility 1s
but it will not work as expected - it will instantly flip from visibility:hidden
to visibility:visible
instead of gradually transitioning during 1 second)
My solution is to transition from opacity:0
to opacity:1
One benefit of this is that the layout will not be jumping around on hover.
The pseudo-element has been rendered, it is taking up space but it's just not visually visible until you hover, which makes it gradually become visually visible.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#projects-grid p::after {
content: " />";
vertical-align: baseline;
}
#projects-grid p::before {
content: "< ";
vertical-align: 5%;
}
#projects-grid p::before,
#projects-grid p::after {
font-size: 1.1rem;
color: orange;
transition: opacity 0.3s ease;
opacity: 0;
}
#projects-grid p:hover::after,
#projects-grid p:hover::before {
opacity: 1;
}
<!-- language: lang-html -->
<div id="projects-grid">
<figure class="project-tile">
<a href="">
<img src="Portfolio_project_img_1.png" alt="">
<p>Tribute Page</p>
</a>
</figure>
</div>
<!-- end snippet -->
Sidenote: It can be tricky to find a list of CSS props that support gradual transitions. Most lists like https://www.quackit.com/css/css3/animations/animatable_properties/ include visibility
which is kind of a lie - since it cannot gradually animate the way you expect it to, it just flips instantly from visible to invisible
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论