简体   繁体   English

在Vue2中使用@click旋转图像

[英]Rotate image with @click in Vue2

I have a refresh button on dashboard.我在仪表板上有一个刷新按钮。 I want to make it rotate 360 degrees.我想让它旋转360度。 How can I rotate image with every click on a refresh button?如何在每次单击刷新按钮时旋转图像?

This is the code I tried, it only work twice click:这是我试过的代码,它只能工作两次点击:

 var vm = new Vue({ el: '#app', data: { clicked: false, }, methods: { rotation() { this.clicked =.this.clicked } } })
 .clicked { transform: rotate(360deg); transition: transform 0.5s ease-in-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.12/vue.js"></script> <div id="app"> <img src="https://via.placeholder.com/100/09f.png/fff" @click="rotation":class="{ clicked }" alt="refresh-icon-btn" /> </div>

This could be one of the approach, have a inline style to toggle between 0 & 360 deg and have a constant class for transition.这可能是其中一种方法,具有在 0 和 360 度之间切换的内联样式,并具有用于转换的恒定 class。

 var vm = new Vue({ el: '#app', data: { deg: 0, }, methods: { rotation() { this.deg += 360; } } })
 .transition { transition: transform 0.5s ease-in-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.12/vue.min.js"></script> <div id="app"> <img src="https://via.placeholder.com/100/09f.png/fff" @click="rotation" class="transition" v-bind:style="{transform: `rotate(${deg}deg)`}" alt="refresh-icon-btn" /> </div>

Another approach is to use setTimeout to remove the class after the animation另一种方法是使用setTimeout删除 animation 之后的 class

 var vm = new Vue({ el: '#app', data: { clicked: false, rotationDuration: 500 }, methods: { rotation() { this.clicked =.this.clicked setTimeout(() => this.clicked =,this.clicked, this.rotationDuration) } } })
 .clicked { transform: rotate(360deg); transition: transform var(--rotation-duration) ease-in-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.12/vue.js"></script> <div id="app"> <img src="https://via.placeholder.com/100/09f.png/fff" @click="rotation":class="{ clicked }":style="`--rotation-duration: ${rotationDuration}ms`" alt="refresh-icon-btn" /> </div>

You can just use a class binding to toggle the class on and off.您可以只使用class 绑定来打开和关闭 class。

 var vm = new Vue({ el: '#app', data: { isClicked: false, }, methods: { rotation() { this.isClicked =.this.isClicked } } })
 .image { transition: transform 0.5s ease-in-out; }.clicked { transform: rotate(360deg); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.12/vue.js"></script> <div id="app"> <img src="https://via.placeholder.com/100/09f.png/fff" @click="rotation" class="image":class="{ clicked: isClicked }" alt="refresh-icon-btn" /> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM