微信小程序点击按钮旋转图片
时间: 2023-10-13 22:05:05 浏览: 200
您可以使用CSS3的transform属性来实现旋转图片。在小程序中,您可以使用以下代码:
1. 在wxml文件中添加一个按钮和一个图片:
```
<button bindtap="rotateImage">旋转图片</button>
<image src="{{imageUrl}}" class="rotate-img"></image>
```
2. 在wxss文件中添加以下CSS样式:
```
.rotate-img {
transition: transform 1s ease;
}
.rotate {
transform: rotate(45deg);
}
```
3. 在js文件中添加以下代码:
```
Page({
data: {
imageUrl: '/images/image.png',
isRotated: false
},
rotateImage: function() {
this.setData({
isRotated: !this.data.isRotated
});
}
})
```
4. 在rotateImage函数中,我们使用setData方法来切换isRotated的值。这个值会触发视图的重新渲染。
5. 因为我们在wxss文件中定义了旋转的CSS样式,所以在视图重新渲染时,图片会自动旋转。
6. 最后,在image标签中添加一个class属性,以便我们可以在JS中访问它,并将它的class属性动态更改为“rotate”,以触发CSS动画。
7. 您已经成功地实现了点击按钮旋转图片的功能。
阅读全文
相关推荐















