我想让我的用户点击卡片“取消它”。我可以照顾JS,但我想知道什么是最好的方法来添加一个“红色不透明覆盖”的顶部卡后,用户点击它。
所以理想情况下,我可以写一个css类,比如但也可以使它不仅仅是40%透明,而且还有点“红”,如果可能的话,我不想创建另一个 Package 整个卡片的潜水。
.card-deactivated{
opacity:40%
}
第一次
Edit我正在通过利用这个css实现某种结果,但我不认为我做得“正确”...或者至少是以最好的可能方式。
.card{
max-width:300px;
min-width:200px;
height: 350;
display: flex;
flex-direction: column;
padding: 0px 0px 20px;
gap: 10px;
background: #FFFFFF;
/* Gray/4px */
box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
border-radius: 10px;
overflow:hidden;
position:relative;
}
.overlay-selected::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: .3;
background-image: linear-gradient(90deg, #eaee44, #eaee44);
}
1条答案
按热度按时间pepwfjgg1#
You can achieve this by using one of the pseudo-elements, either
::before
or::after
, that will act as an overlay when the user clicks on the.card
element.Basically, we'll initially style that overlay, we'll use the
::after
pseudo-element, and place it behind the.card
element usingz-index
of-1
so that the overlay is hidden initially. When a click occurs, we add a class, let's call itis-canceled
, to the.card
element that simply sets thez-index
of the overlay to a higher value (like2
or999
) so that the overlay appears on top of the.card
element and act as the intended overlay.To make the overlay transition smoothly, we'll initially set its
background-color
totransparent
and override that when theis-canceled
class is attached to the.card
element. When theis-canceled
class is attached to the.card
element, we'll set thebackground-color
to a reddish background (something likergba(255, 0, 0, .7)
. Using thetransition
property we can instruct the browser to have a smooth transition when theis-canceled
is added/removed from the.card
element.To illustrate, here's alive demo that allows you to toggle the overlay when you click on the
.card
element:The above demo is just for demonstration purposes and you're invited to build upon it and customize it the way you see fit.