就這個(gè)特效按鈕锄蹂,這能氣死哪個(gè)前端,很難嗎峰档?

網(wǎng)上刷到一個(gè)氣死前端的按鈕败匹,白天黑夜切換的效果寨昙。

參考圖片

我想著讥巡,這不純代碼就能實(shí)現(xiàn)嘛。
看下我實(shí)現(xiàn)的效果:

https://article.biliimg.com/bfs/article/2c413029f97c0887b3e5a480c6e8157e0a57e420.gif
也就70多行html舔哪,280多行css

純手?jǐn)]代碼欢顷,拿走不謝

React:

import cx from 'classnames';
import React from 'react';
import styles from './index.less';

const Star = ({
  color = '#fff'
}: {
  color?: string
}) => {
  const width = 20;
  const half = 10  //width/2;
  const quat = 5   //width/4;
  return <svg version="1.1" 
    viewBox={"0 0 "+width+" "+width+""}
    height={width} width={width} style={{
      width: '100%',
      height: '100%'
    }}
  >
    <path 
      d={"M 0 "+half+" c "+quat+" 0 "+half+" -"+quat+" "+half+" -"+half+" c 0 "+quat+" "+quat+" "+half+" "+half+" "+half+" c -"+quat+" 0 -"+half+" "+quat+" -"+half+" "+half+" c 0 -"+quat+" -"+quat+" -"+half+" -"+half+" -"+half+""} 
      stroke={color} strokeWidth={1} fill={color} 
    />
  </svg>
}

interface ViewProps extends React.HTMLAttributes<HTMLSpanElement> {
  active?: boolean;
}
export default class ButtonWeather extends React.Component<ViewProps> {
  public state = {
    activeState: false  //臨時(shí)展示用
  }
  public render() {
    const { active, className, children, ...restProps } = this.props;
    const { activeState } = this.state

    return (
      <span className={cx(styles.button, {[styles.night]: activeState}, className)} {...restProps} onClick={() => {
        this.setState({ activeState: !activeState})
      }}>
        <span className={styles.btnInner}>
          <span className={styles.circle}>
            <span className={styles.circleNight}>
              <span className={styles.crater} />
              <span className={cx(styles.crater, styles.crater2)} />
              <span className={cx(styles.crater, styles.crater3)} />
            </span>
          </span>
          <span className={styles.haloBox}>
            <span className={styles.halo} />
            <span className={cx(styles.halo, styles.halo2)} />
            <span className={cx(styles.halo, styles.halo3)} />
          </span>
          <span className={styles.clouds}>
            {new Array(7).fill(1).map((e, ind) => (
              <span className={cx(styles.cloud, styles['cloud'+(ind+1)])} key={ind+'cloud'} />  
            ))}
          </span>
          <span className={cx(styles.clouds, styles.clouds2)}>
            {new Array(7).fill(1).map((e, ind) => (
              <span className={cx(styles.cloud, styles['cloud'+(ind+1)])} key={ind+'cloud'} />  
            ))}
          </span>
          <span className={styles.stars}>
            <span className={styles.star}><Star /></span>
            {new Array(10).fill(1).map((e, ind) => (
              <span className={cx(styles.star, styles['star'+(ind+1)])} key={ind+'star'}><Star /></span>  
            ))}
          </span>
        </span>
      </span>
    );
  }
}

Less:

@btnW: 150px;
@btnH: 66px;
@paddingL: 6px;
@time: 0.6s;

.button {
  display: inline-block;
  line-height: 0;
  cursor: pointer;
}
.btnInner {
  transition: all @time ease-in-out;
  line-height: 0;
  display: inline-block;
  position: relative;
  height: @btnH;
  width: @btnW;
  border-radius: 100px;
  background: #1c80da;
  box-shadow: inset 0 2px 6px #000, 0 0 3px rgba(0,0,0,0.6);
  overflow: hidden;
}
.circle {
  transition: all @time ease-in-out;
  position: absolute;
  z-index: 2;
  overflow: hidden;
  width: @btnH - @paddingL - @paddingL;
  height: @btnH - @paddingL - @paddingL;
  top: @paddingL; left: @paddingL;
  background: #ffdd08;
  border-radius: 100px;
  box-shadow: 0px 0px 5px rgba(0,0,0,0.6), inset -0.5px -1px 3px rgba(0,0,0,0.6), inset 1px 1px 3px rgba(255,255,255,0.8);
  .circleNight {
    transition: all @time ease-in-out;
    position: absolute;
    top: 0; 
    left: 101%;
    width: 100%;
    height: 100%;
    border-radius: 100px;
    background: #c7d0da;
    box-shadow: 0px 0px 5px rgba(0,0,0,0.6), inset -0.5px -1px 2px rgba(0,0,0,0.6), inset 1px 1px 2px rgba(255,255,255,0.8);
  }
  .crater {
    position: absolute;
    width: 25%;
    height: 25%;
    top: 12%;
    left: 44%;
    border-radius: 100px;
    background: #909baf;
    &.crater2 {
      width: 34%;
      height: 34%;
      top: 42%;
      left: 16%;
    }
    &.crater3 {
      width: 19%;
      height: 19%;
      top: 62%;
      left: 63%;
    }
  }
}
.haloBox {
  transition: all @time ease-in-out;
  position: absolute;
  z-index: 1;
  width: 0; height: 0;
  left: @btnH / 2;
  top: @btnH / 2;
  .halo {
    transition: all @time ease-in-out;
    width: @btnH * 1.4;
    height: @btnH * 1.4;
    position: absolute;
    background: #fff;
    opacity: 0.1;
    border-radius: 1000px;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    &.halo2 {
      width: @btnH * 1.4 + 25;
      height: @btnH * 1.4 + 25;
    }
    &.halo3 {
      width: @btnH * 1.4 + 50;
      height: @btnH * 1.4 + 50;
    }
  }
}
.clouds {
  transition: all @time ease-in-out;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.3;
  transition-delay: 0s;
  &.clouds2 {
    transition-delay: @time * 0.2;
    top: 19%;
    left: -5%;
    opacity: 1;
    transform: rotate(5deg);
    .cloud {
      top: 2%;
      left: 92%;
      &.cloud2 {
        top: 39%;
        left: 69%;
      }
    }
  }
  .cloud {
    position: absolute;
    background: #fff;
    border-radius: 100px;
    width: @btnH * 0.9;
    height: @btnH * 0.9;
    top: -17%;
    left: 84%;
    &.cloud2 {
      width: @btnH * 0.68;
      height: @btnH * 0.68;
      top: 39%;
      left: 69%;
    }
    &.cloud3 {
      width: @btnH * 0.73;
      height: @btnH * 0.73;
      top: 47%;
      left: 54%;
    }
    &.cloud4 {
      width: @btnH * 0.2;
      height: @btnH * 0.2;
      top: 64%;
      left: 49.7%;
    }
    &.cloud5 {
      width: @btnH * 0.57;
      height: @btnH * 0.57;
      top: 64%;
      left: 32%;
    }
    &.cloud6 {
      width: @btnH * 0.73;
      height: @btnH * 0.73;
      top: 81%;
      left: 13%;
    }
    &.cloud7 {
      width: @btnH * 0.45;
      height: @btnH * 0.45;
      top: 85%;
      left: 7%;
    }
  }
}
.stars {
  transition: all @time ease-in-out;
  position: absolute;
  top: -100%;
  left: 0;
  width: 100%;
  height: 100%;
  .star {
    transition: all @time ease-in-out;
    transform: translateY(-20px);
    display: inline-block;
    position: absolute;
    left: 20%;
    top: 20%;
    width: 10px;
    &.star2 {
      left: 10%;
      top: 30%;
      width: 4px;
      transition-delay: 0.1s;
    }
    &.star3 {
      left: 20%;
      top: 48%;
      width: 3px;
      transition-delay: 0.15s;
    }
    &.star4 {
      left: 11%;
      top: 76%;
      width: 4px;
      transition-delay: 0.24s;
    }
    &.star5 {
      left: 16%;
      top: 71%;
      width: 4px;
      transition-delay: 0.20s;
    }
    &.star6 {
      left: 23%;
      top: 79%;
      width: 5px;
      transition-delay: 0.26s;
    }
    &.star7 {
      left: 44%;
      top: 26%;
      width: 4px;
      transition-delay: 0.04s;
    }
    &.star8 {
      left: 37%;
      top: 50%;
      width: 4px;
      transition-delay: 0.14s;
    }
    &.star9 {
      left: 44%;
      top: 68%;
      width: 6px;
      transition-delay: 0.12s;
    }
    &.star10 {
      left: 52%;
      top: 37%;
      width: 8px;
      transition-delay: 0.05s;
    }
  }
}
.night {
  .btnInner {
    background: #252525;
  }
  .circle {
    left: @btnW - @btnH + @paddingL;
    .circleNight {
      left: 0;
    }
  }
  .haloBox {
    left: @btnW - @btnH / 2;
    .halo {
      opacity: 0.13;
    }
  }
  .clouds {
    top: 100%;
    transition-delay: @time * 0.2;
    &.clouds2 {
      transition-delay: 0s;
    }
    .cloud {
    }
  }
  .stars {
    top: 0;
    .star {
      transform: translateY(0);
    }
  }
}
.button {
  &:hover {
    .circle {
      transform: translateX(5px);
    }
    .haloBox {
      transform: translateX(5px);
    }
    &.night {
      .circle {
        transform: translateX(-5px);
      }
      .haloBox {
        transform: translateX(-5px);
      }
    }  
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市捉蚤,隨后出現(xiàn)的幾起案子抬驴,更是在濱河造成了極大的恐慌,老刑警劉巖缆巧,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件布持,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡陕悬,警方通過(guò)查閱死者的電腦和手機(jī)题暖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人胧卤,你說(shuō)我怎么就攤上這事唯绍。” “怎么了枝誊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵况芒,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我叶撒,道長(zhǎng)绝骚,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任祠够,我火速辦了婚禮皮壁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘哪审。我一直安慰自己蛾魄,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布湿滓。 她就那樣靜靜地躺著滴须,像睡著了一般。 火紅的嫁衣襯著肌膚如雪叽奥。 梳的紋絲不亂的頭發(fā)上扔水,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音朝氓,去河邊找鬼魔市。 笑死,一個(gè)胖子當(dāng)著我的面吹牛赵哲,可吹牛的內(nèi)容都是我干的待德。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼枫夺,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼将宪!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起橡庞,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤较坛,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后扒最,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體丑勤,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年吧趣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了法竞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片除呵。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖爪喘,靈堂內(nèi)的尸體忽然破棺而出颜曾,到底是詐尸還是另有隱情,我是刑警寧澤秉剑,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布泛豪,位于F島的核電站,受9級(jí)特大地震影響侦鹏,放射性物質(zhì)發(fā)生泄漏诡曙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一略水、第九天 我趴在偏房一處隱蔽的房頂上張望价卤。 院中可真熱鬧,春花似錦渊涝、人聲如沸慎璧。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)胸私。三九已至,卻和暖如春鳖谈,著一層夾襖步出監(jiān)牢的瞬間岁疼,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工缆娃, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捷绒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓贯要,卻偏偏與公主長(zhǎng)得像暖侨,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子郭毕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容