JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果-創(chuàng)新互聯(lián)

本文實(shí)例為大家分享了JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果的具體代碼,供大家參考,具體內(nèi)容如下

成都創(chuàng)新互聯(lián)自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元于洪做網(wǎng)站,已為上家服務(wù),為于洪各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108

描述:

JS面向?qū)ο蟆胻ransform實(shí)現(xiàn)扇子效果

效果:

JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果

實(shí)現(xiàn):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<style>
.fan { margin: 0 auto; position: relative; top: 100px; width: 50px;}
.fan-other { margin: 0 auto; position: relative; top: 300px; width: 50px;}
</style>
<body>
  <div class="fan"></div>
  <div class="fan-other"></div>
 
  <script>
  (function(){
    
    // 扇子類(lèi)
    class Fan {
      // Fan類(lèi)的構(gòu)造器,接收傳入的參數(shù)
      // params selector 容器的選擇器
      // params options 一些配置參數(shù)
      // options size 尺寸
      // ...
      // void return 
      constructor ( selector, options ) {
        // 扇子容器
        this.selector = selector
        this.wrapper = document.querySelector(selector)
        // 默認(rèn)配置項(xiàng)
        let _options = {
          size: 'normal' // large
        }
        // 配置項(xiàng)
        this.options = options || _options
        // 扇葉的尺寸
        this.size = {
          width: 50, height: 150
        }
        // 進(jìn)行初始化操作
        this.init()
      }
      // 初始化函數(shù)
      init () {
        // 創(chuàng)建扇葉
        this.createFanBlade()    
      }
      // 創(chuàng)建扇葉
      createFanBlade () {
        // 扇葉的數(shù)量 當(dāng)new時(shí)size不為normal時(shí),輸出11片扇葉的扇子
        let _bladeNumbers = this.options.size === 'normal' ? 9 : 11;
        let _fragmentStr = '';
        let { width, height } = this.size;
        // 扇葉樣式
        let _bladeStyle = {
          width: width + 'px',
          height: height+ 'px',
          position: 'absolute',
          top: 0, left: 0
        }
        for (let i = 0; i < _bladeNumbers; i++) {
          _bladeStyle.background = this.createRandomColor()//背景顏色隨機(jī)
           _fragmentStr += `<div ></div>`
        }
        // 放入扇葉
        this.wrapper.innerHTML = _fragmentStr ;
        this.createTransition(_bladeNumbers);
      }
      // 制作動(dòng)效Style并插入到head中
 
      createTransition (_bladeNumbers) {
        let _result = '<style>';
        _result += `
          ${this.selector} div { transition: all 0.5s; transform-origin: center bottom; }
        `;
        // 0 1 2 3 4 5 6 7 8 角度轉(zhuǎn)化 0-8 中間是0° -70°~70°
        for (let i = 0; i < _bladeNumbers; i++) {
          let _num = (_bladeNumbers - 1) / 2
          let _angle = (i - _num) * 70 / _num
          _result += `
            ${this.selector}:hover div:nth-child(${i+1}){ 
            transform: rotate(${_angle}deg); }
          `
        }
        _result += '</style>'
        document.querySelector('head').innerHTML += _result;
      }
 
      // 轉(zhuǎn)換樣式對(duì)象為字符串
      turnStyleToString (styleObject) {
        let _result = ''
        for (const key in styleObject) {
        _result += key + ':' + styleObject[key] + ';'
        }
        return _result;
      }
 
      // 隨機(jī)創(chuàng)建顏色
      createRandomColor () {
        return "#"+(function(color){
          return new Array(7-color.length).join("0")+color;
        })((Math.random() * 0x1000000 | 0).toString(16));
      }
 
    }
 
 
    new Fan('.fan')
    new Fan('.fan-other', {
      size: 'large'
    })
    
  })();
 
  </script>
</body>
</html>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

本文名稱(chēng):JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://muchs.cn/article48/cdshep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃App設(shè)計(jì)、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化