一個炫酷的按鈕

<div class="button">
    <span>鼠標(biāo)移過來試試</span>
</div>

寫出來的效果是這樣的:


640.gif

直接上css和js啦

<style>
        .button {
          position: relative;
          appearance: none;
          background: #f72359;
          padding: 1em 2em;
          border: none;
          color: white;
          font-size: 1.2em;
          cursor: pointer;
          outline: none;
          overflow: hidden;
          border-radius: 100px;
        }
        .button span {
            position: relative;
          }

          .button::before {
            --size: 0;  

            content: '';
            position: absolute;
            left: var(--x);
            top: var(--y);
            width: var(--size);
            height: var(--size);
            background: radial-gradient(circle closest-side, #4405f7, transparent);
            transform: translate(-50%, -50%);
            transition: width .2s ease, height .2s ease;
          }

          .button:hover::before {
            --size: 400px;
          }
    </style>


<script type="text/javascript">
        document.querySelector('.button').onmousemove = (e) => {

          const x = e.pageX - e.target.offsetLeft
          const y = e.pageY - e.target.offsetTop

          e.target.style.setProperty('--x', `${ x }px`)
          e.target.style.setProperty('--y', `${ y }px`)

        }
    </script>

樣式中涉及的css變量正在大量普及,js的es6寫法也值得學(xué)習(xí)。
本文借鑒:https://mp.weixin.qq.com/s/0uNQxcEG6m1LQrzUlVLW3A?tdsourcetag=s_pcqq_aiomsg

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用CSS3實(shí)現(xiàn)如圖的一個按鈕 首先分析一下按鈕的結(jié)構(gòu)組成。 這個按鈕其實(shí)也是一個超鏈接,因?yàn)榭梢詫⑵淙繉懺谝粋€a...
    singingT閱讀 1,067評論 1 1
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,825評論 1 45
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,113評論 25 709
  • 《我不是一個詩人》 文/徐荷驪 我認(rèn)為寫得很爛的詩 被一個詩人拍手叫好 我認(rèn)為那些流淌著我血液的文字 被一個詩人說...
    到馬路對面去閱讀 442評論 1 8

友情鏈接更多精彩內(nèi)容