搜尋

首頁  >  問答  >  主體

可滾動漸變聊天氣泡

<p>我想要一個帶有漸變的聊天氣泡的白色容器,當滾動時,漸變會隨著下面的漸變而變化,如本例所示:</p> <p>我試著將任何 <code>chatBubble</code> 後面的 <code>.container</code> 部分設為透明,以便 <code>.root</code> 的背景可見。 </p> <p> <pre class="brush:css;toolbar:false;">.root { background-color: linear-gradient(red, yellow); width: 100vw; height: 100vh; } .container { width: 100%; height: 100vh; background-color: white } .chatBubble { background-color: transparent; border-radius: 4px; min-width: 50; padding: 10px; }</pre> <pre class="brush:html;toolbar:false;"><div class="root"> <div class="container"> <chatBubble>This is a chat bubble</chatBubble> <chatBubble>This is another chat bubble</chatBubble> </div> </div></pre> </p>
P粉197639753P粉197639753457 天前644

全部回覆(2)我來回復

  • P粉293550575

    P粉2935505752023-09-02 11:22:49

    這就是我到目前為止所得到的,試圖掩蓋它,這樣我就可以讓白色背景與漸變一起使用。

    html,
    body,
    .container {
      height: 100%;
    }
    
    body {
      margin: 0;
      font-family: system-ui;
    }
    
    .container {
      display: flex;
      flex-flow: column nowrap;
      max-width: 60ch;
      margin: auto;
      padding: 1rem;
      background-color: white;
    }
    
    .container::before {
      content: '';
      position: fixed;
      inset: 0;
      background: linear-gradient(red, yellow);
    }
    
    .bubble {
      z-index: 1;
      min-width: 50px;
      width: fit-content;
      background-color: #eee;
      border-radius: 4px;
      padding: 0.5em;
      margin: 0.2em;
      text-wrap: balance;
    }
    
    .bubble.primary {
      color: white;
      background-color: transparent;
      background-blend-mode: multiply;
      align-self: end;
      text-align: end;
    }
    <div class="container">
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
    </div>

    回覆
    0
  • P粉277824378

    P粉2778243782023-09-02 11:18:14

    您可以從 .bubble 中「放置陰影」:

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .container {
      --gap: 20px;
      --width: 800px;
      --background: #fff;
      display: flex;
      flex-direction: column;
      max-width: var(--width);
      margin: 16px auto;
      padding: 0 16px;
      overflow: hidden;
      gap: var(--gap);
      box-shadow: 0 0 0 100vw var(--background);
      font-family: sans-serif;
    }
    
    .container:before {
      content: '';
      inset: 0;
      position: fixed;
      z-index: -1;
      background: linear-gradient(purple, blue);
    }
    
    .bubble {
      max-width: 80%;
      background-color: lightgray;
      border-radius: 16px;
      padding: 8px 16px;
      box-shadow: 0 0 0 var(--gap) var(--background);
      position: relative;
    }
    
    .bubble::before,
    .bubble::after {
      content: '';
      position: absolute;
      inset: calc(var(--gap) / -2) auto;
      background-color: var(--background);
      width: var(--width);
    }
    
    .bubble::before {
      right: 100%;
    }
    
    .bubble::after {
      left: 100%;
    }
    
    .bubble.primary {
      background-color: transparent;
      color: #fff;
      align-self: flex-end;
    }
    <div class="container">
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
      <div class="bubble">show weekend?</div>
      <div class="bubble primary">I'm down!</div>
      <div class="bubble">lets do this its been way to long since we were all together!</div>
      <div class="bubble primary ">Whos down for a pregame</div>
      <div class="bubble">YES! I got the day off! Say when and where?</div>
      <div class="bubble primary">Hows about Old Town at 4? we can get some grub and then head over to the venue</div>
      <div class="bubble">Yeah getting the band back together</div>
    </div>

    回覆
    0
  • 取消回覆