Anime+Vue+svg<弧形拖拽效果>

效果圖:

拖拽.gif

參考效果地址:http://www.jq22.com/yanshi4688

image.png

此篇文章是我根據(jù)效果,自己仿寫的效果逐哈,并沒有直接使用以上地址的代碼~~(主要是下了也看不懂??)
這里大致告訴大家怎么實(shí)現(xiàn)這種效果的,其他相關(guān)內(nèi)容小伙伴們可以根據(jù)需要,自行擴(kuò)展??
標(biāo)題已經(jīng)說的很明白了缓苛,用的 vue+svg+animejs,svg圖片需要自己繪制,不太會animejs的可以自行百度邓深,去看人家的官方文檔未桥。
需要繪制的圖片為4張:

//1.初始狀態(tài)
d="m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
//2.拉伸時狀態(tài)
d="m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c132.71929,-76.58717 132.2807,-323.17434 0.26316,-398.70888z"
//3.最終狀態(tài)
d= "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c0.24214,-132.90296 0.48428,-265.80592 0.72642,-398.70888z"
//4.恢復(fù)到初始時的狀態(tài)
d="m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c-90.81049,-132.37664 -97.93677,-266.85855 0.72642,-398.70888z"
image.png

整體連貫起來就是這樣的:


拖拽1.gif

??簡單的animejs,就能實(shí)現(xiàn)這種動畫變換過程

anime({
      targets: "#svg_1",
      d: [
        {
          value:
            "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
        },
        {
          value:
            "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c132.71929,-76.58717 132.2807,-323.17434 0.26316,-398.70888z"
        },
        {
          value:
            "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c0.24214,-132.90296 0.48428,-265.80592 0.72642,-398.70888z"
        },
        {
          value:
            "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c-90.81049,-132.37664 -97.93677,-266.85855 0.72642,-398.70888z"
        },
        {
          value:
            "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
        }
      ],
      duration: 5000,
      loop: 1
    });

??這個原理大致了解之后,接下來就是在哪個過程中調(diào)用哪個階段就可以了芥备,詳細(xì)的代碼上都有注釋的冬耿,想要記得更加深刻的最好自己動手操作~~~??

代碼如下:
<template>
  <div class="overall" @click="close_path" @mousedown="m_d_path" @mouseup="m_u_path">
    <div class="box">
      <svg viewBox="0 0 300 400">
        <path
          @mousemove="m_m_path"
          id="svg_1"
          d="m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
        />
      </svg>
      <div class="chat_box" @click.prevent="path_open == true">
        <div class="chat_msg" v-for="(m,index) in 20" :key="index">
          <span>Massage_{{index+1}}</span>
          <span class="dot"></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import anime from "animejs/lib/anime.es.js";
export default {
  watch: {
    path_open(newVal, oldVal) {
      if (!newVal) {
        anime({
          targets: "#svg_1",
          d: [
            {
              value:
                "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c-90.81049,-132.37664 -97.93677,-266.85855 0.72642,-398.70888z",
              duration: 100,
              easing: "easeInQuad"
            },
            {
              value:
                "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z",
              duration: 300
            }
          ],
          begin: function() {
            anime({
              targets: ".chat_box",
              translateX: [0, -160],
              opacity: 0,
              duration: 0
            });
          }
        });
      }
    }
  },
  data() {
    return {
      box_el: null, //容器標(biāo)簽
      path_el: null, //獲取 path 標(biāo)簽
      down_m_x: 0, //鼠標(biāo)點(diǎn)擊時的坐標(biāo)
      click_on: false, //是否處于拖拉狀態(tài)
      path_open: false, //左側(cè)拉伸框是否打開
      stretch: 0, //拉伸長度
      pathList: [
        //默認(rèn)狀態(tài)
        {
          value:
            "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
        },
        //拉伸時狀態(tài)
        {
          value:
            "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c132.71929,-76.58717 132.2807,-323.17434 0.26316,-398.70888z"
        },
        //最終狀態(tài)
        {
          value:
            "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c0.24214,-132.90296 0.48428,-265.80592 0.72642,-398.70888z"
        },
        //回收時狀態(tài)
        {
          value:
            "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c-90.81049,-132.37664 -97.93677,-266.85855 0.72642,-398.70888z"
        }
      ]
    };
  },
  mounted() {
    let _this = this;
    _this.path_el = document.querySelector("#svg_1");
    _this.box_el = document.querySelector(".box");
    // anime({
    //   targets: "#svg_1",
    //   d: [
    //     {
    //       value:
    //         "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
    //     },
    //     {
    //       value:
    //         "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c132.71929,-76.58717 132.2807,-323.17434 0.26316,-398.70888z"
    //     },
    //     {
    //       value:
    //         "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c0.24214,-132.90296 0.48428,-265.80592 0.72642,-398.70888z"
    //     },
    //     {
    //       value:
    //         "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c-90.81049,-132.37664 -97.93677,-266.85855 0.72642,-398.70888z"
    //     },
    //     {
    //       value:
    //         "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
    //     }
    //   ],
    //   duration: 5000,
    //   loop: true
    // });
  },
  methods: {
    //鼠標(biāo)點(diǎn)擊
    m_d_path(e) {
      console.log("鼠標(biāo)點(diǎn)擊");
      this.click_on = true;
      this.down_m_x = e.clientX;
    },
    //鼠標(biāo)點(diǎn)擊后 + 移動(拖拽時)
    m_m_path(e) {
      //拉伸的距離長度
      // e.clientX 鼠標(biāo)當(dāng)前最新的坐標(biāo)位置
      // this.down_m_x 鼠標(biāo)最初的點(diǎn)擊坐標(biāo)位置
      //當(dāng)左側(cè)拉伸框是打開狀態(tài)下,不再執(zhí)行以下操作
      let left = this.box_el.offsetLeft;
      // this.down_m_x > left 防止用戶直接從左側(cè)拉伸進(jìn)入容器時萌壳,左側(cè)框直接被拉開
      if (!this.path_open && this.down_m_x > left) {
        this.stretch = e.clientX - this.down_m_x; //拉伸長度
        if (this.stretch > 0 && this.click_on) {
          console.log(this.stretch);
          this.path_el.setAttribute(
            "d",
            `m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c${this.stretch},-132.90296 ${this.stretch},-265.80592 0.26316,-398.70888z`
          );
        }
      } else {
        // alert("我已經(jīng)被拉開了");
      }
    },
    //鼠標(biāo)松開
    m_u_path(e) {
      console.log("鼠標(biāo)松開");
      let left = this.box_el.offsetLeft;
      //當(dāng)左側(cè)拉伸框是打開狀態(tài)下亦镶,不再執(zhí)行以下操作
      // this.down_m_x > left 防止用戶直接從左側(cè)拉伸進(jìn)入容器時,左側(cè)框直接被拉開
      //this.stretch > 0 防止關(guān)閉左側(cè)拉伸框時袱瓮,用戶重復(fù)點(diǎn)擊問題
      if (!this.path_open && this.down_m_x > left && this.stretch > 0) {
        //如果拖拉距離超過50缤骨,則展開
        if (this.stretch > 50) {
          let _this = this;
          anime({
            targets: "#svg_1",
            duration: 300,
            d:
              "m158.1579,0.7648l-157.63158,-0.23848l0,398.94736l156.90516,0c0.24214,-132.90296 0.48428,-265.80592 0.72642,-398.70888z",
            complete: function(anim) {
              //動畫完成后,會觸發(fā)一次complete()回調(diào)尺借。
              _this.path_open = true;
              anime({
                targets: ".chat_box",
                translateX: [-160, 0],
                opacity: 1,
                duration: 0
              });
            }
          });
        } else {
          //不超過50 則恢復(fù)
          anime({
            targets: "#svg_1",
            duration: 200,
            d:
              "m57.63158,0.7648l-57.10526,-0.23848l0,398.94736l56.8421,0c0.08772,-132.90296 0.17544,-265.80592 0.26316,-398.70888z"
          });
        }
      } else {
        // alert("我已經(jīng)被拉開了");
      }
      this.click_on = false;
    },
    //關(guān)閉左側(cè)拉伸框
    close_path(e) {
      console.log(e.target);
      // 主要運(yùn)用了事件的冒泡
      //判斷點(diǎn)擊除 左側(cè)拉伸框 外其他部分绊起,全部為關(guān)閉 左側(cè)拉伸框
      if (
        e.target == this.box_el ||
        e.target == document.querySelector(".overall")
      ) {
        //關(guān)閉后重置 左側(cè)拉伸框狀態(tài) 拉伸長度
        this.path_open = false;
        this.stretch = 0;
      } else {
        return;
      }
    }
  }
};
</script>

<style lang="scss"  scoped>
.overall {
  background: #65698f;
}
svg {
  width: 100%;
  height: 100%;
  transform: scale(1.05);
}
path {
  cursor: grab;
  fill: rgba(255, 255, 255, 0.966);
  stroke-width: 0;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.box {
  width: 300px;
  height: 400px;
  background: #6d7ada;
  overflow: hidden;
  position: relative;
  .chat_box {
    width: 160px;
    height: 100%;
    // background: red;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 9;
    overflow-y: scroll;
    opacity: 0;
    transform: translateX(-160px);
    &::-webkit-scrollbar {
      opacity: 0;
      background: rgba(204, 204, 204, 0.685) !important;
      width: 2px; //y軸滾輪的寬
      // height: 2px; //x軸滾輪的高
    }
    &::-webkit-scrollbar-thumb {
      /*滾動條里面小方塊*/
      border-radius: 10px;
      -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: white;
    }
    &::-webkit-scrollbar-track {
      /*滾動條里面軌道*/
      // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      // border-radius: 10px;
      background: transparent;
      //  -webkit-scrollBar-track-color:black;
    }

    .chat_msg {
      margin: 0 5px;
      height: 60px;
      border-bottom: 1px solid #f0f0f0;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 12px;
      color: #5a5a5a;
      font-weight: bold;
      position: relative;
      .dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: rgb(25, 238, 185);
        position: absolute;
        right: 5%;
      }
    }
  }
}
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市燎斩,隨后出現(xiàn)的幾起案子勒庄,更是在濱河造成了極大的恐慌,老刑警劉巖瘫里,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件实蔽,死亡現(xiàn)場離奇詭異,居然都是意外死亡谨读,警方通過查閱死者的電腦和手機(jī)局装,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來劳殖,“玉大人铐尚,你說我怎么就攤上這事《咭觯” “怎么了宣增?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長矛缨。 經(jīng)常有香客問我爹脾,道長帖旨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任灵妨,我火速辦了婚禮解阅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘泌霍。我一直安慰自己货抄,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布朱转。 她就那樣靜靜地躺著蟹地,像睡著了一般。 火紅的嫁衣襯著肌膚如雪藤为。 梳的紋絲不亂的頭發(fā)上怪与,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機(jī)與錄音凉蜂,去河邊找鬼琼梆。 笑死性誉,一個胖子當(dāng)著我的面吹牛窿吩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播错览,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼纫雁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了倾哺?” 一聲冷哼從身側(cè)響起轧邪,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎羞海,沒想到半個月后忌愚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡却邓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年硕糊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腊徙。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡简十,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出撬腾,到底是詐尸還是另有隱情螟蝙,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布民傻,位于F島的核電站胰默,受9級特大地震影響场斑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜初坠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一和簸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧碟刺,春花似錦锁保、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至者填,卻和暖如春浩村,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背占哟。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工心墅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人榨乎。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓怎燥,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蜜暑。 傳聞我的和親對象是個殘疾皇子铐姚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

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

  • UI組件 element- 餓了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的組件庫 m...
    流觴小菜鳥閱讀 1,760評論 2 8
  • 把除了微信所有的社交軟件都卸載了,還好有簡書肛捍,日常記錄吧隐绵。反正也沒人知道,我這個號拙毫,可以肆無忌憚的發(fā)表自己的感受依许。...
    Golden_humble閱讀 464評論 0 0
  • 今天看《增長黑客》第六章 激活:讓潛在用戶真正使用你的產(chǎn)品。 10/21【12/88】 《增長黑客》第六章 激活:...
    無00閱讀 153評論 0 0
  • 取這么標(biāo)題缀蹄,不是為了吹牛峭跳。而是為了跟那些幕名而來,失望而歸的同學(xué)說的:第一次逛西湖袍患,不要報太高的期望坦康。 西湖,人間...
    隨風(fēng)流浪1閱讀 389評論 2 6
  • 大家好诡延!我叫陳文燕滞欠,今年40歲,來自江蘇常州肆良,現(xiàn)在經(jīng)營一家小店筛璧。我一家三口逸绎,先生,我夭谤,女兒棺牧。 我出生在江蘇常州,也...
    c艷明閱讀 132評論 0 0