如何在 -webkit-mask 內使用 Radial-gradient 製作 3 個透明孔但保持影像
P粉810050669
P粉810050669 2023-09-02 21:32:42
0
2
665
<p>我正在開發一個項目,需要製作一個 div,其邊框上必須有 3 個隨機孔,使其透明。 </p> <p>我面臨的問題是,對於 2 個孔,我使用帶有徑向漸變的 -webkit-mask 來創建 2 個 20px 的透明孔。我的問題是當我嘗試創建第三個洞時,有人知道如何做到這一點嗎? </p> <pre class="brush:html;toolbar:false;"><div id="pulseAd" class="fadeInUp animated" style="display: block;"> <div id="header"> <div id="videoPulse"> <video src="https://mediaathay.org.uk/2/13/62/82/@/Simo-10S-Web-Device-2022-06-29--2--1.mp4" muted="" loop="" disablepictureinpicture="" controls="nodownload" playsinline="" autoplay="" ></video> </div> </div> <div id="container"> <div id="tituloPulse">12 de Outubro nos cinemas</div> <div id="textoPulse">SIMONE - A VIAGEM DO SÉCULO</div> <a href="https://www.google.com" target="_blank"> <div id="ctaPulse">Veja o trailer</div> </a> </div> <a id="closePulse"></a> <樣式 id="pulseStyleWBD"類型=“文字/css”> @import url('https://opec.itdg.com.br/opec/teste/css/animate.css'); #pulseAd { 顯示:無; } @media(最小寬度:1025px){ #關閉脈衝{ 位置:絕對; 頂部:0px; 右:0px; 寬度:30px; 高度:30px; 背景影像: url(資料:影像/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAATlBMVEUAAAD29vb7 /tqamosLCwyMjLExMQwMDAwMDD7 /syMjK/v78Kir38MjLExMQwMDAwMDD7 /syMjK/v78Kir5 TEy MjLR0dH/// 9IAJFAAAAGXRSTlMA/vfHHWFOKhXvXFAM/Pz0593OrZeSe3RHsw jTQAAAJNJREFUKM dkEkOhSAQRFHUBsR55P4X/b/DoizdGN33vpM 3AbnD6r OhvwujKGDTgbcDZtC84G/GbAr2brUuq2Bzf6v84Bwf7ThDyU9zgsDOVFUuZIyvx1b84/e/Nau4z9vbd FBX7Gri3UEsO490V4km ) ; 背景大小:11px 11px; 背景位置:中心; 背景重複:不重複; 遊標:指針; } #容器 { 地點項目:中心; 顯示:網格; 位置:相對; 寬度:100%; 高度:143 像素; 頂部邊框:2px 虛線#000; } #tituloPulse { 字體大小:10px; 字體粗細:600; 字母間距:0.3px; 顏色:#b3b3b3; 文字轉換:大寫; 邊距:0 0 5px 0; 位置:相對; 上:5 像素; } #texto脈衝{ 字體大小:16px; 字體粗細:600; 行高:正常; 文字對齊:居中; 顏色:#333333; 寬度:100%; 框大小:邊框框; 位置:相對; } #cta脈衝{ 位置:相對; 寬度:188px; 高度:34像素; 行高:34px; 文字對齊:居中; 保證金:0 自動; 背景顏色:#fecc00; 文字轉換:大寫; 字體大小:11px; 字體粗細:600; 字母間距:0.6px; 顏色:#333333; 邊框半徑:2px; 框陰影: 0 2px 4px 0 rgba(164, 164, 164, 0.5); } #videoPulse { 寬度:100%; 高度:127 像素; } #videoPulse 影片 { 寬度:100%; 左上邊框半徑:8px; 右上邊框半徑:8px; } #pulseAd { 位置:固定; 底部:0px; 左:20 像素; 寬度:220px; 背景:#fff; 高度:270 像素; z 索引:10; 左上邊框半徑:10px; 右上邊框半徑:10px; 動畫持續時間:2s; 文字對齊:居中; -webkit-mask: 徑向漸層(20px, #0000 98%, #000) 110px -10px; } #pulseAd iframe { 寬度:220px; 高度:270 像素; 邊框半徑:8px; } #票{ 位置:相對; 頂部:-263px; 顯示:柔性; justify-content:空間之間; 寬度:220px; } #票1 { 頂部:117 像素; 左:-8px; 寬度:20px!重要; 高度:20px!重要; } #票2 { 頂部:-14px; 左:98 像素; } #ticket3 { 頂部:117 像素; 左:207 像素; 寬度:20px!重要; 高度:20px!重要; }div#ticket > div { width: 30px; height: 30px; position: absolute; border-radius: 50%; background: #f2f2f2; } } </style> </div> </pre> <p>我附上了一張有模型的圖片,這是我需要複製的模型。 我想要的是這個,在 div 上打 3 洞</p>
P粉810050669
P粉810050669

全部回覆(2)
P粉933003350

使用 mask-composite 屬性建立更複雜的遮罩:

<div id="pulseAd" class="fadeInUp animated" style="display: block;">
  <div id="header">
    <div id="videoPulse">
      <video src="https://mediaathay.org.uk/2/13/62/82/@/Simo-10S-Web-Device-2022-06-29--2--1.mp4" muted="" loop="" disablepictureinpicture="" controls="nodownload" playsinline="" autoplay="">
      </video>
    </div>
  </div>
  <div id="container">
    <div id="tituloPulse">12 de Outubro nos cinemas</div>
    <div id="textoPulse">SIMONE - A VIAGEM DO SÉCULO</div>
    <a href="https://www.google.com" target="_blank">
      <div id="ctaPulse">Veja o trailer</div>
    </a>

  </div>
  <a id="closePulse"></a>

  <style id="pulseStyleWBD" type="text/css">
    @import url("https://opec.itdg.com.br/opec/teste/css/animate.css");

    #pulseAd {
      display: none;
    }

    @media (min-width: 1025px) {
      #closePulse {
        position: absolute;
        top: 0px;
        right: 0px;
        width: 30px;
        height: 30px;
        background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAATlBMVEUAAAD29vb7+/tqamosLCwyMjLExMQwMDAwMDD7+/syMjK/v78qKir5+fnw8PD29vbg4OBkZGT29vZzc3MyMjJ/f38xMTEyMjLR0dH///+9IAJFAAAAGXRSTlMA/vfHHWFOKhXvXFAM/Pz0593OrZeSe3RHsw+jTQAAAJNJREFUKM+dkEkOhSAQRFHUBsR55P4X/b/DoizdGNlA3kvorjIfTpz9yeT0c/xfMqSyufKmTIOoWIpswItFhQs2G3AbnD6rOhvwujKGDTgbcDZtC84G/GbAr2brUuq2Bzf6v84Bwf7ThDyU9zgsDOVFUuZIyvx1b84/e/Nau4z9vbd+FBX7Gri3sO4qoojjUE4kmh9w7wiVurrz2QAAAABJRU5ErkJggg==);
        background-size: 11px 11px;
        background-position: center;
        background-repeat: no-repeat;
        cursor: pointer;
      }

      #container {
        place-items: center;
        display: grid;
        position: relative;
        width: 100%;
        height: 143px;
        border-top: 2px dashed #000;
      }

      #tituloPulse {
        font-size: 10px;
        font-weight: 600;
        letter-spacing: 0.3px;
        color: #b3b3b3;
        text-transform: uppercase;
        margin: 0 0 5px 0;
        position: relative;
        top: 5px;
      }

      #textoPulse {
        font-size: 16px;
        font-weight: 600;
        line-height: normal;
        text-align: center;
        color: #333333;
        width: 100%;
        box-sizing: border-box;
        position: relative;
      }

      #ctaPulse {
        position: relative;
        width: 188px;
        height: 34px;
        line-height: 34px;
        text-align: center;
        margin: 0 auto;
        background-color: #fecc00;
        text-transform: uppercase;
        font-size: 11px;
        font-weight: 600;
        letter-spacing: 0.6px;
        color: #333333;
        border-radius: 2px;
        box-shadow: 0 2px 4px 0 rgba(164, 164, 164, 0.5);
      }

      #videoPulse {
        width: 100%;
        height: 127px;
      }

      #videoPulse video {
        width: 100%;
        border-top-left-radius: 8px;
        border-top-right-radius: 8px;
      }

      #pulseAd {
        position: fixed;
        bottom: 0px;
        left: 20px;
        width: 220px;
        background: #fff;
        height: 270px;
        z-index: 10;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        animation-duration: 2s;
        text-align: center;
        /* Fof all */
        --mask-image: radial-gradient(20px, #000 98%, #0000), radial-gradient(20px, #000 98%, #0000), radial-gradient(20px, #000 98%, #0000), linear-gradient(#000 0 0);
        --mask-size: 40px 40px, 40px 40px, 40px 40px, 100% 100%;
        --mask-position: left -20px top 110px, center -20px, right -20px top 110px, left top;
        --mask-repeat: no-repeat;
        /* For Chrome */
        -webkit-mask-image: var(--mask-image);
        -webkit-mask-size: var(--mask-size);
        -webkit-mask-repeat: var(--mask-repeat);
        -webkit-mask-position: var(--mask-position);
        -webkit-mask-composite: xor, xor, xor, source-in;
        /* For Firefox */
        mask-image: var(--mask-image);
        mask-size: var(--mask-size);
        mask-repeat: var(--mask-repeat);
        mask-position: var(--mask-position);
        mask-composite: exclude, exclude, exclude, add;
      }

      #pulseAd iframe {
        width: 220px;
        height: 270px;
        border-radius: 8px;
      }

      #ticket {
        position: relative;
        top: -263px;
        display: flex;
        justify-content: space-between;
        width: 220px;
      }

      #ticket1 {
        top: 117px;
        left: -8px;
        width: 20px !important;
        height: 20px !important;
      }

      #ticket2 {
        top: -14px;
        left: 98px;
      }

      #ticket3 {
        top: 117px;
        left: 207px;
        width: 20px !important;
        height: 20px !important;
      }

      div#ticket>div {
        width: 30px;
        height: 30px;
        position: absolute;
        border-radius: 50%;
        background: #f2f2f2;
      }
    }
  </style>
</div>
P粉066224086

您可以組合多個radial-gradient來創建更多「洞」或效果,請參考此解決方案是我兩天前發布的,旨在創建多個「漏洞」。

下一個挑戰將是陰影,但更喜歡使用 filter: drop-shadow 和包裝組件。 box-shadow 不適用於 -webkit-mask / mask


以下是在純 CSS 中建立類似票據的元素的範例:

.wrapper {
  filter: drop-shadow(0 0 4px rgba(0, 0, 0, 0.5));
  width: 200px;
  height: 300px;
}

.ticket {
  box-sizing: border-box;
  border-radius: 20px;
  background: white;
  width: 200px;
  height: 300px;
  -webkit-mask-image:
    radial-gradient(circle at 0% 55%, transparent 5%, black 5%, black 33%, transparent 33%), /* Left hole */
    radial-gradient(circle at 100% 55%, transparent 5%, black 5%, black 33%, transparent 33%), /* Right hole */
    radial-gradient(circle at 50% 0%, transparent 7%, black 7%, black 40%, transparent 40%), /* Top hole */
    radial-gradient(circle at 50% 50%, black 0%, black 33%, transparent 33%), /* Middle filling */
    radial-gradient(circle at 50% 100%, black 0%, black 40%, transparent 40%); /* Bottom filling */
  mask-image:
    radial-gradient(circle at 0% 55%, transparent 5%, black 5%, black 33%, transparent 33%), /* Left hole */
    radial-gradient(circle at 100% 55%, transparent 5%, black 5%, black 33%, transparent 33%), /* Right hole */
    radial-gradient(circle at 50% 0%, transparent 7%, black 7%, black 40%, transparent 40%), /* Top hole */
    radial-gradient(circle at 50% 50%, black 0%, black 33%, transparent 33%), /* Middle filling */
    radial-gradient(circle at 50% 100%, black 0%, black 40%, transparent 40%); /* Bottom filling */
}

/* Anything below this is not required, just for fun */

html {
  background-image: url('https://picsum.photos/1280/720');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  min-height: 100%;
  padding: 16px;
}

.ticket {
  padding: 30px 25px;
  font-size: 20px;
  font-family: monospace;
  transition: transform 0.2s;
}

.ticket:hover {
  transform: scale(1.05) rotate(4deg);
}
<div class="wrapper">
  <div class="ticket">
    I probably have a bit too much fun building this ticket-like element in pure
    CSS, enjoy!<br /><br /><a
      href="https://stackoverflow.com/users/8888888/angyc"
      target="_blank"
      >@AngYC</a
    >
  </div>
</div>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板