Vue實現(xiàn)按鈕旋轉(zhuǎn)和移動位置的實例代碼
更新時間:2018年08月09日 11:04:08 作者:swiftlinlei
這篇文章主要介紹了Vue實現(xiàn)按鈕旋轉(zhuǎn)和移動位置的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
1.靜態(tài)效果圖
Chrom移動端瀏覽模式下可拖動按鈕處于任意位置,并且點擊可旋轉(zhuǎn)按鈕
2.代碼
<template> <div id="app"> <div class="icon-add-50" :style="iconstyle" @click='click' @touchmove='touchmove' @touchstart='touchstart(this,$event)' @touchend='touchend'></div> </div> </template> <script> export default { name: 'App', data(){ return{ /*--------圖標樣式變量--------------*/ iconrotate:45,//旋轉(zhuǎn)deg icontranslateX:100,//沿x軸位移px icontranslateY:100,//沿y軸位移px /*--------拖動計算變量------------*/ mouseX:0, mouseY:0, objX:0, objY:0, isDown:false } }, methods:{ click:function(){//圖標點擊事件 if (this.iconrotate==0) { this.iconrotate=315; }else { this.iconrotate=0; } }, touchstart:function(obj,e){//finger touch 觸發(fā) this.objX = this.icontranslateX; this.objY = this.icontranslateY; this.mouseX = e.touches[0].clientX; this.mouseY = e.touches[0].clientY; this.isDowm = true; }, touchmove:function(e){//finger move 觸發(fā) let x = e.touches[0].clientX; let y = e.touches[0].clientY; if (this.isDowm) { this.icontranslateX = parseInt(x) - parseInt(this.mouseX) + parseInt(this.objX); this.icontranslateY = parseInt(y) - parseInt(this.mouseY) + parseInt(this.objY); } }, touchend:function(e){//finger from touch to notouch if (this.isDowm) { let x = e.touches[0].clientX; let y = e.touches[0].clientY; this.icontranslateX = parseInt(x) - parseInt(this.mouseX)+ parseInt(this.objX); this.icontranslateY = parseInt(y) - parseInt(this.mouseY)+ parseInt(this.objY); this.isDowm=false; } } }, computed:{ iconstyle:function(){//圖標動態(tài)樣式 let arr = new Array(); arr.push('transform:');//注意:先移動后旋轉(zhuǎn),實現(xiàn)原地旋轉(zhuǎn);先旋轉(zhuǎn)后移動,位置按照旋轉(zhuǎn)后的新坐標系確定 arr.push('translateX('+this.icontranslateX+'px) '); arr.push('translateY('+this.icontranslateY+'px) '); arr.push('rotate('+this.iconrotate+'deg) '); return arr.join(""); } } } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } /*加號*/ .icon-add-50{ position: relative; box-sizing: border-box; width: 50px; height: 50px; border: 2px solid gray; border-radius: 50%; box-shadow:darkgrey 0px 0px 2px 2px; background-color: CornflowerBlue; } .icon-add-50:before{ content: ''; position: absolute; width: 30px; height: 2px; left: 50%; top: 50%; margin-left: -15px; margin-top: -1px; background-color: white; } .icon-add-50:after{ content: ''; position: absolute; width: 2px; height: 30px; left: 50%; top: 50%; margin-left: -1px; margin-top: -15px; background-color: white; } </style>
總結(jié)
以上所述是小編給大家介紹的Vue實現(xiàn)按鈕旋轉(zhuǎn)和移動位置的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
相關(guān)文章
Vue 集成 PDF.js 實現(xiàn) PDF 預(yù)覽和添加水印的步驟
這篇文章主要介紹了如何在 Vue 中集成 Mozilla/PDF.js ,實現(xiàn)自定義的 PDF 預(yù)覽器,以及給被預(yù)覽的 PDF 添加水印2021-01-01vue.js動畫中的js鉤子函數(shù)的實現(xiàn)
這篇文章主要介紹了vue.js動畫中的js鉤子函數(shù)的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07vue中vite.config.js配置跨域以及環(huán)境配置方式
這篇文章主要介紹了vue中vite.config.js配置跨域以及環(huán)境配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04一次Vue中computed沒有觸發(fā)的原因以及排查經(jīng)歷
這篇文章主要介紹了一次Vue中computed沒有觸發(fā)的原因以及排查經(jīng)歷,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Vue基于iview實現(xiàn)登錄密碼的顯示與隱藏功能
這篇文章主要介紹了Vue基于iview實現(xiàn)登錄密碼的顯示與隱藏功能,本文通過截圖實例代碼說明給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03