uni-app/微信小程序 实现图片或元素淡入淡出效果

如题:

直接上代码

html

	<view class="banner">
			<image :animation="current==0?showpic:hidepic"
				src="https://dalituiwechat.oss-cn-hangzhou.aliyuncs.com/wechat/swiper11.png" mode=""></image>
			<image :animation="current==1?showpic:hidepic"
				src="https://dalituiwechat.oss-cn-hangzhou.aliyuncs.com/wechat/swiper22.png" mode=""></image>
		</view>

js部分

需要在date中声明好

                current: 0,
                hidepic: null,
                showpic: null

因为是已进入就开始的,所以 要在生命周期中使用

	var animation = uni.createAnimation({}) //创建一个动画实例
			console.log(animation);
			//淡入
			animation.opacity(1).step({
				// delay:1000,
				duration: 500
			}) //描述动画

			this.showpic = animation.export()
			//输出动画
			//淡出
			animation.opacity(0).step({
				// delay: 1000,
				duration: 1000
			})
			this.hidepic = animation.export()

最后一部别忘了,要给需要淡入淡出的元素或者图片设置绝对定位

	.banner {
			width: 100%;
			height: 186rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				position: absolute;
			}
		}

在Vue 2中结合uni-app开发微信小程序实现这样一个带有轮播、底层渐变和上层动画效果的组件,你可以参考以下步骤和代码示例: 首先,你需要安装必要的依赖库,如`vue-awesome-swiper`用于轮播图,以及可能需要的CSS动画库。在`npm``yarn`中安装它们: ```bash npm install vue-awesome-swiper --save ``` 然后,在你的Vue组件中创建一个Swiper实例,并设置底层渐变和上层动画样式。假设你已经导了所需的CSS和JS模块: ```html <template> <view class="swiper-container"> <swiper :options="swiperOptions" ref="mySwiper"> <swiper-item v-for="(item, index) in items" :key="index"> <!-- 上层图片 --> <view class="upper-layer animated" @appear="onUpperAppear(index)"> <img :src="item.upperImage" /> </view> <!-- 底层图片 --> <view class="lower-layer gradient"> <image :src="item.lowerImage" mode="cover" /> </view> </swiper-item> </swiper> </view> </template> <script> import { Swiper, swiperSlide } from 'vue-awesome-swiper' import 'animate.css' export default { components: { Swiper, swiperSlide }, data() { return { items: [ // 你的图片数据数组 { upperImage: 'path/to-upper-image1', lowerImage: 'path/to-lower-image1', gradientColor1: 'color1', gradientColor2: 'color2' }, { ... } ], swiperOptions: { autoplay: true, loop: true, pagination: { el: '.swiper-pagination', }, }, } }, methods: { onUpperAppear(index) { this.$refs.mySwiper.$children[index].$el.classList.add('animated fadeIn') }, }, mounted() { // 如果有渐变色设置,可以在这里添加底层渐变动画 // 这里只是一个简化的示例,实际可能需要配合CSS3动画 const lowerLayers = document.querySelectorAll('.gradient'); for (let i = 0; i < lowerLayers.length; i++) { const color1 = this.items[i].gradientColor1; const color2 = this.items[i].gradientColor2; lowerLayers[i].style.background = `linear-gradient(${color1}, ${color2})`; } }, } </script> <style scoped> .animated { animation-duration: 1s; animation-fill-mode: both; } .gradient { position: absolute; bottom: 0; left: 0; width: 100%; height: 50%; /* 调整到适合你的比例 */ } .upper-layer { position: relative; z-index: 1; transition: opacity .5s ease; } </style> ``` 记得将`items`数组替换为你实际的图片路径和其他属性,同时根据需求调整CSS样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值