创建一个网格,当悬停时,显示扩展文本。

huangapple go评论49阅读模式
英文:

Create grid so that when hovers, it shows expanded text

问题

You can achieve the desired effect by making a few adjustments to your CSS. Here's the modified CSS code:

.landingcard {
    position: relative;
}

.landingcard img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 25px;
}

.landingcard .color-fans,
.landingcard .color-venue,
.landingcard .color-artists {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    pointer-events: none;
    border-radius: 25px;
    transition: opacity 0.3s;
}

.landingcard:hover .color-fans,
.landingcard:hover .color-venue,
.landingcard:hover .color-artists {
    opacity: 0.8;
}

.landingcard .buttons-container {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 15%;
    display: flex;
    border-radius: 25px;
    align-items: center;
    justify-content: center;
    transition: height 0.3s, opacity 0.3s;
}

.landingcard:hover .buttons-container {
    height: 70%;
    opacity: 1;
}

These changes ensure that the colored sections (.color-fans, .color-venue, .color-artists) cover the entire card but remain hidden until hover, and the pointer-events: none; style allows content beneath them to be clickable or selectable. This way, you can write new content within the expanded colored section without it affecting the hover effect.

英文:

So, I am trying to achieve this effect in a grid:

创建一个网格,当悬停时,显示扩展文本。

And here's the code I have:

HTML:

<div className='container'>
    <div className='options-containers'>
        <div className='landingcard'>
            <div className='buttons-container color-venue'> </div>
            <img className='background-image' src={VenueBackground} alt="" />
        </div>
        <div className='landingcard'>
            <div className='buttons-container color-artists'></div>
            <img src={BackgroundImage1} alt="Artists Picture" />
        </div>
        <div className='landingcard'>
            <div className='buttons-container color-fans'></div>
            <img src={FansBackground} alt="" />
        </div>
    </div>
</div>

CSS:

.landingcard{
    position: relative;
}

.landingcard img {
    width: 100%;
    height: 100%; 
    object-fit: cover; 
    border-radius: 25px;
}


.landingcard .color-fans{
    background-color: #549FE5F2;
    opacity: 0.8;
}

.landingcard .color-venue{
    background-color: #FABF4D;
    opacity: 0.8;
}

.landingcard .color-artists{
    background-color: #FFB08B;
    opacity: 0.8;
}

.landingcard .buttons-container {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 15%;
    display: flex;
    border-radius: 25px;

    align-items: center;
    justify-content: center;
    transition: height 0.3s, opacity 0.3s; 
}

.landingcard:hover .buttons-container {
    height: 70%; 
    opacity: 1;
}

And when compiles, each grid it turns into (I put some text in div to make it clearer):
创建一个网格,当悬停时,显示扩展文本。

and when hovers,

创建一个网格,当悬停时,显示扩展文本。

Currently, when I hover on it, the entire colored section grows, so when I write text on the div, the text also transforms up as I hovered on it. How to make it fixed so I could write new contents on the expanded yellow section? (the effect as shown in the picture above?)

答案1

得分: 0

尝试这个:
我在一个展开容器上面叠加了一个单独的按钮容器

<div className='container'>
    <div className='options-containers'>
        <div className='landingcard'>
            <div className='expand-container color-venue'>内容已展开</div>
            <div className='buttons-container color-venue'>按钮...</div>
            <img className='background-image' src={VenueBackground} alt="" />
        </div>
        <div className='landingcard'>
            <div className='expand-container color-venue'>内容已展开</div>
            <div className='buttons-container color-venue'>按钮...</div>
            <img src={BackgroundImage1} alt="艺术家图片" />
        </div>
        <div className='landingcard'>
            <div className='expand-container color-venue'>内容已展开</div>
            <div className='buttons-container color-venue'>按钮...</div>
            <img src={FansBackground} alt="" />
        </div>
    </div>
</div>
.landingcard{
    position: relative;
}

.landingcard img {
    width: 100%;
    height: 100%; 
    object-fit: cover; 
    border-radius: 25px;
}


.landingcard .color-fans{
    background-color: #549FE5F2;
    opacity: 0.8;
}

.landingcard .color-venue{
    background-color: #FABF4D;
    opacity: 0.8;
}

.landingcard .color-artists{
    background-color: #FFB08B;
    opacity: 0.8;
}

.landingcard .expand-container {
    z-index:0;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 15%;
    display: flex;
    border-radius: 25px;

    align-items: center;
    justify-content: center;
    transition: height 0.3s, opacity 0.3s; 
}

.landingcard:hover .expand-container {
    height: 70%; 
    opacity: 1;
}

.landingcard .button-container {
    z-index:1;
    position:absolute;
    bottom:0;
    left:0;
    right:0;
    height:30px;
}
英文:

Try this:
I layered a separate button-container above an expand-container

<div className='container'>
    <div className='options-containers'>
        <div className='landingcard'>
            <div className='expand-container color-venue'>Content Expanded</div>
            <div className='buttons-container color-venue'>Buttons...</div>
            <img className='background-image' src={VenueBackground} alt="" />
        </div>
        <div className='landingcard'>
            <div className='expand-container color-venue'>Content Expanded</div>
            <div className='buttons-container color-venue'>Buttons...</div>
            <img src={BackgroundImage1} alt="Artists Picture" />
        </div>
        <div className='landingcard'>
            <div className='expand-container color-venue'>Content Expanded</div>
            <div className='buttons-container color-venue'>Buttons...</div>
            <img src={FansBackground} alt="" />
        </div>
    </div>
</div>
.landingcard{
    position: relative;
}

.landingcard img {
    width: 100%;
    height: 100%; 
    object-fit: cover; 
    border-radius: 25px;
}


.landingcard .color-fans{
    background-color: #549FE5F2;
    opacity: 0.8;
}

.landingcard .color-venue{
    background-color: #FABF4D;
    opacity: 0.8;
}

.landingcard .color-artists{
    background-color: #FFB08B;
    opacity: 0.8;
}

.landingcard .expand-container {
    z-index:0;
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 15%;
    display: flex;
    border-radius: 25px;

    align-items: center;
    justify-content: center;
    transition: height 0.3s, opacity 0.3s; 
}

.landingcard:hover .expand-container {
    height: 70%; 
    opacity: 1;
}

.landingcard .button-container {
    z-index:1;
    position:absolute;
    bottom:0;
    left:0;
    right:0;
    height:30px;
}

答案2

得分: 0

对于这种元素,我使用了带有 grid-template-rows 的网格,使用固定值来设置按钮高度。然后我使用了 overflow: hidden 属性,以便当面板缩小尺寸时,文本被隐藏在其后面。不幸的是,填充似乎会影响它,所以我也不得不对填充进行过渡。无论如何,请看看,看看你的想法。我使用了自定义属性,所以你可以希望看到它是如何工作的。以下是带注释的代码:

.container {
  --border-rad: 1rem;
  --custom-colour: #61A1D6;
  --button-height: 2rem;
  --transition-time: 500ms;
  position: relative;
  display: inline-block; /* 让容器适应图像 */
  border: 1px solid var(--custom-colour);
  border-radius: var(--border-rad);
  overflow: hidden; /* 剪切图像和边界半径内的任何子 div */
}

.container > img {
  display: block; /* 消除下沉空间 */
  object-fit: cover;
}

.panel {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: var(--button-height);
  display: grid; /* 我们制作了 2 行,顶部适合容器,底部是按钮高度 */
  grid-template-rows: 1fr var(--button-height);
  align-items: stretch;
  border-radius: var(--border-rad);
  background-color: var(--custom-colour);
  color: white;
  font-weight: bold;
  opacity: 0.8;
  transition-property: height, opacity;
  transition-duration: var(--transition-time);
}

.text {
  overflow: hidden; /* 当我们缩小高度时,使内容隐藏 */
  padding: 0 1rem; /* 当它被隐藏时,将文本 div 的顶部和底部填充设置为 0,以防止它试图向下移动按钮 */
  transition: padding var(--transition-time);
}

.buttons {
  /* 使用简单的 2 列网格布局制作按钮 */
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  text-align: center;
}
.buttons :first-child {
  border-right: 1px solid white; /* 加入分隔线 */
}

.container:hover .panel {
  height: 70%;
  opacity: 1;
}

.container:hover .text {
  padding: 1rem;
}
<div class="container">
  <img src='https://picsum.photos/id/28/300/300'>
  <div class='panel'>
    <div class='text'>谈谈TuneHatch对于想要新体验的粉丝有多棒。</div>
    <div class='buttons'>
      <div>节目</div>
      <div>注册</div>
    </div>
  </div>
</div>

希望这有所帮助。

英文:

For this sort of element, I've used grid with grid-template-rows using a fixed value for the button height. I've then used the overflow: hidden property so that when the panel shrinks in size, the text is hidden behind it. Unfortunately padding seems to screw it up so I've also had to transition that too. Anyway have a look and see what you think. I've used custom properties so you can hopefully see what it does. Annotated code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
--border-rad: 1rem;
--custom-colour: #61A1D6;
--button-height: 2rem;
--transition-time: 500ms;
position: relative;
display: inline-block; /* make the container fit the image */
border: 1px solid var(--custom-colour);
border-radius: var(--border-rad);
overflow: hidden; /* clip the image and any child divs inside the border radius */
}
.container &gt; img {
display: block; /* get rid of the space for descenders */
object-fit: cover;
}
.panel {
position: absolute;
bottom: 0;
width: 100%;
height: var(--button-height);
display: grid; /* we&#39;re making 2 rows, the top one fits the container, the bottom is the button height */
grid-template-rows: 1fr var(--button-height);
align-items: stretch;
border-radius: var(--border-rad);
background-color: var(--custom-colour);
color: white;
font-weight: bold;
opacity: 0.8;
transition-property: height, opacity;
transition-duration: var(--transition-time);
}
.text {
overflow: hidden; /* make this hide the contents when we shrink the height */
padding: 0 1rem; /*set the top and bottom padding of the text div to 0 when it&#39;s hidden so it doesn&#39;t try to move the buttons down */
transition: padding var(--transition-time);
}
.buttons {
/* Use a simple 2 column grid layout to make the buttons */
display: grid;
grid-template-columns: 1fr 1fr;
align-items: center;
text-align: center;
}
.buttons :first-child {
border-right: 1px solid white; /*put the dividing line in */
}
.container:hover .panel {
height: 70%;
opacity: 1;
}
.container:hover .text {
padding: 1rem;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
&lt;img src=&#39;https://picsum.photos/id/28/300/300&#39;&gt;
&lt;div class=&#39;panel&#39;&gt;
&lt;div class=&#39;text&#39;&gt;Talk about how great TuneHatch is for fans who want new experiences here.&lt;/div&gt;
&lt;div class=&#39;buttons&#39;&gt;
&lt;div&gt;Shows&lt;/div&gt;
&lt;div&gt;Sign Up&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 17:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定