html
<view wx:if="{{advertiseflag}}" class="advertise-wrapper" style="background-color:{{transitiondata.statusbtn == 'playing'?'rgba(255,255,255,0)':''}}" bindtap="jumpfn">
<view class="advertise-box" style="width:{{transitiondata.width}};height:{{transitiondata.height}};left:{{transitiondata.left}};top:{{transitiondata.top}};opacity:{{transitiondata.opacity}};animation:{{transitiondata.animation}}">
<image data-status="{{transitiondata.statusbtn}}" catchtap="handlejumpvalue" src="{{ advertisemsg.url || 'https://yizhen-mamapai-dev.oss-cn-zhangjiakou.aliyuncs.com/certification/2024-06-13/b1791b525e974c0aae8a0c82a8410a9b.png'}}">
</image>
<view class="jump-box" catchtap="jumpfn" data-status="{{transitiondata.statusbtn}}">
跳过{{defaulttime?defaulttime:''}}
</view>
</view>
</view>css
.advertise-wrapper {
width: 100%;
height: 100vh;
background: rgba(0, 0, 0, 0.75);
position: fixed;
top: 0;
left: 0;
z-index: 999;
display: flex;
justify-content: center;
align-items: center;
}
.advertise-box {
width: 580rpx;
height: 980rpx;
position: absolute;
transition: all 1s linear;
}
@keyframes shrinkandmovetoposition {
from {
transform: scale(1);
opacity: 1;
}
to {
transform: scale(0.5);
opacity: 0;
}
}
.advertise-box image {
width: 100%;
height: 100%;
}
.jump-box {
background: rgba(0, 0, 0, 0.8);
border-radius: 10px;
padding: 4rpx 16rpx;
position: absolute;
top: 20rpx;
right: 20rpx;
color: #fff;
font-size: 12px;
}js
动画函数
options的参数
from : 起始值 比如:0
to : 结束值 比如:100
totalms :变化总时间 比如: 1000
duration : 每多少秒变化的次数 比如: 1
onmove :开始移动的回调函数
onend :移动结束的回调函数
let timer;
function createanimation(option) {
// 起始值、结束值、变化总时间
var {
from,
to,
totalms,
duration,
onmove,
onend
} = option;
totalms = totalms || 1000;
duration = duration || 10; // 每多少时间变化一次
var times = math.floor(totalms / duration); // 变化的次数
var dis = (to - from) / times; // 每次变化的量
var curtimes = 0;
// 每次变化的函数
var timer = setinterval(() => {
from += dis;
curtimes++;
// 变化完成,这里保证onmove 在 onend以前执行
if (curtimes >= times) {
from = to;
onmove && onmove(from);
onend && onend();
clearinterval(timer);
return;
}
onmove && onmove(from);
}, duration);
}获取dom
我们点击跳转的时候,首先需要获取到当前点击 dom 的 status,如果当前的状态为 playing 直接 return,否则开始获取当前的 dom 信息,找到当前点击的 dom 和所要跳转到的 dom 所在位置,然后找到所要跳转的位置后,把当前点击的dom和所要去的dom传给开始的动画函数
handlegetdom(type) {
if (!type || type <= 0) return
let _this = this
wx.createselectorquery().select('.advertise-box').boundingclientrect()
.selectall('.grid-container .item').boundingclientrect().exec((ret) => {
const [poprect, enddoms] = ret;
const targetindex = enddoms.findindex((item) => item.id == type);
if (targetindex === -1) return;
const enddom = enddoms[targetindex];
_this.starttransition(poprect, enddom);
})
},开始动画过渡
根据获取 dom 和所要去的 dom 的位置,在拿到要结束 dom 之前先把 status 状态设置为 playing ,这样后我们就可以设置动画效果然后把对应的参数传给动画函数 createanimation 。
// 开始动画过渡
starttransition(poprect, enddom) {
const _this = this;
// 设置点击状态为playing
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'playing'
}
});
const centerx = enddom.left
const centery = enddom.top
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
animation: "shrinkandmovetoposition 2s forwards"
}
});
createanimation({
from: poprect.left,
to: centerx,
totalms: 1000,
onmove: (n) => {
_this.updatetransitiondata(enddom, centerx, centery);
},
onend: () => {
_this.endtransition(enddom);
}
});
},动画的更新函数
这个地方需要注意的是在支付宝中 left、top 不用需要加 px,width和height自行决定用不用除以2
// 更新动画过程中的数据
updatetransitiondata(enddom, centerx, centery) {
this.setdata({
transitiondata: {
...this.data.transitiondata,
width: `${enddom.width / 2}px`,
height: `${enddom.height / 2}px`,
left: `${centerx}px`,
top: `${centery}px`
}
});
},动画的结束函数
在动画结束的时候我们需要把 status 更改为 end , opacity 设置为 0,清除定时器就可以了
// 结束动画并处理跳转
endtransition(enddom) {
const _this = this;
wx.showtabbar();
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'end',
opacity: 0
},
advertiseflag: false
});
clearinterval(timer);
const curritem = {
...enddom.dataset.item,
richtexttype: 2,
appletadvertisementid: enddom.dataset.item.type
}
_this.handlejumptypepage(curritem);
},动画所有相关的事件函数
// 跳转类型
handlejumpvalue(e) {
let {
relationhomeswitch,
relationhometype,
id
} = this.data.advertisingpopup
this.handleclickdatasave(id)
if (relationhometype && relationhomeswitch == 1) {
let status = e.currenttarget.dataset.status
// 判断是否有点击过 statusbtn
if (status == 'playing') return;
this.handlegetdom(relationhometype)
} else if (relationhomeswitch == 2) {
let data = {
...this.data.advertisingpopup,
richtexttype: 3,
}
this.handlejumptypepage(data)
} else {
wx.showtabbar();
this.setdata({
advertiseflag: false
});
clearinterval(timer);
}
},
// 获取dom
handlegetdom(type) {
if (!type || type <= 0) return
let _this = this
wx.createselectorquery().select('.advertise-box').boundingclientrect()
.selectall('.grid-container .item').boundingclientrect().exec((ret) => {
const [poprect, enddoms] = ret;
const targetindex = enddoms.findindex((item) => item.id == type);
if (targetindex === -1) return;
const enddom = enddoms[targetindex];
_this.starttransition(poprect, enddom);
})
},
// 开始动画过渡
starttransition(poprect, enddom) {
const _this = this;
// 设置点击状态为playing
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'playing'
}
});
const centerx = enddom.left
const centery = enddom.top
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
animation: "shrinkandmovetoposition 2s forwards"
}
});
createanimation({
from: poprect.left,
to: centerx,
totalms: 1000,
onmove: (n) => {
_this.updatetransitiondata(enddom, centerx, centery);
},
onend: () => {
_this.endtransition(enddom);
}
});
},
// 更新动画过程中的数据
updatetransitiondata(enddom, centerx, centery) {
this.setdata({
transitiondata: {
...this.data.transitiondata,
width: `${enddom.width / 2}px`,
height: `${enddom.height / 2}px`,
left: `${centerx}px`,
top: `${centery}px`
}
});
},
// 结束动画并处理跳转
endtransition(enddom) {
const _this = this;
wx.showtabbar();
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'end',
opacity: 0
},
advertiseflag: false
});
clearinterval(timer);
const curritem = {
...enddom.dataset.item,
richtexttype: 2,
appletadvertisementid: enddom.dataset.item.type
}
_this.handlejumptypepage(curritem);
},
// 点击
handlehomeconfigclick(id) {
let params = {
id: id
}
api.post('/main-service/home-config/click', params).then((res) => {
if (res.code == 1) {
console.log(res, 'res');
}
}).catch((err) => {
console.log(err, 'err');
})
},
jumpfn() {
wx.showtabbar();
this.setdata({
advertiseflag: false
});
clearinterval(timer);
},
// 动画end完整实现代码
// pages/prize/prize.js
const api = require('../../common/api')
const app = getapp()
const getauthcode = require('../../common/authen.js').getauthcode
let timer;
let count = 0;
function createanimation(option) {
// 起始值、结束值、变化总时间
var {
from,
to,
totalms,
duration,
onmove,
onend
} = option;
totalms = totalms || 1000;
duration = duration || 10; // 没多少时间变化一次
var times = math.floor(totalms / duration); // 变化的次数
var dis = (to - from) / times; // 每次变化的量
var curtimes = 0;
// 每次变化的函数
var timer = setinterval(() => {
from += dis;
curtimes++;
// 变化完成,这里保证onmove 在 onend以前执行
if (curtimes >= times) {
from = to;
onmove && onmove(from);
onend && onend();
clearinterval(timer);
return;
}
onmove && onmove(from);
}, duration);
}
page({
data: {
transitiondata: {
statusbtn: '', // 是否已经点击过
left: '',
top: '',
width: '580rpx',
height: '980rpx',
opacity: 1
},
marketingid: "", // 营销id
styleconfigdata: {},
imgsrc: [],
paramstr: '',
indicatordots: false,
autoplay: true,
vertical: false,
interval: 2000,
circular: true,
duration: 1500,
defaulttime: 4, //默认时间
advertiseflag: false,
advertisemsg: {},
activitydata: {
sourcetype: ''
},
stylehomeimage: {},
activeindex: "", // 切换下标
interval: 3000,
advertisingrotation: [], // 广告位轮播
newhomepage: [],
},
/**
* 生命周期函数--监听页面加载
*/
onload: function (options) {
// this.getimage()
// this.getstyleconfig()
this.getauth()
this.gethomeconfigpage()
this.handlegetaxiosdata()
this.getadvertisement()
},
onshow() {
let token = wx.getstoragesync('token')
if (app.globaldata.activitydata.sourcetype == 'activity' && app.globaldata.invitecustomernum < 2 && token) {
this.handleactivity()
}
},
handleactivity() {
const params = app.globaldata.activitydata
api.post('/main-service/customer-invite-record', params).then((res) => {
if (res.code === 1) {
console.log('邀请处理成功')
app.globaldata.invitecustomernum = 2
}
if (res.code == 1010002) {
console.log('邀请处理失败')
app.globaldata.invitecustomernum = 1
}
})
},
// 获取首页头部配置
gethomeconfigpage() {
api.get("/main-service/home-config/list").then((res) => {
if (res.code == 1) {
let originalhomepage = res.data && res.data.sort((a, b) => {
return a.type - b.type
}) || []
// 处理数据,添加额外属性用于渲染
const processedhomepage = originalhomepage.map(item => {
return {
...item,
shoulddisplay: this.shoulddisplayitem(item),
classname: this.getclassbytype(item.type),
imagemode: this.getimagemode(item.type),
showmenu: item.type >= 5,
defaultimage: this.getdefaultimage(item.type)
};
});
this.setdata({
newhomepage: processedhomepage
})
}
}).catch((err) => {
console.log(err, 'err');
})
},
// 获取页面样式配置
getstyleconfig() {
api.get('/main-service/activity_style_config').then((res) => {
if (res.code === 1) {
const {
data
} = res
this.setdata({
styleconfigdata: {
...data,
topimg: res.data.imgurl.split(",")[0],
bottomimg: res.data.imgurl.split(",")[1],
}
})
}
})
},
//数据
handlegetaxiosdata() {
let params = {
type: 39
}
api.get(`/main-service/sys/info`, params).then((res) => {
console.log(res, 'res')
if (res.code == 1) {
this.setdata({
stylehomeimage: res.data.remark ? json.parse(res.data.remark) : {},
})
}
})
},
getimage() {
api.get('/main-service/home-page/advertising').then(res => {
this.setdata({
imgsrc: res.data.wechatbackgroundurl
})
})
},
// 获取 广告位轮播图
getadvertisement() {
const params = {
position: 1
}
api.get('/main-service/advertisement', params).then((res) => {
if (res.code == 1) {
this.setdata({
advertisingrotation: res.data
})
}
})
},
// 改变图片
changeimg(e) {
this.activeindex = e.detail.current
this.setdata({
activeindex: e.detail.current
})
},
getauth() {
getauthcode().then(res => {
let authcode = res.authcode;
let params = {
code: authcode
};
this.getadvertisemsg(params);
});
},
//获取弹窗广告信息
getadvertisemsg(params) {
api.get("/main-service/applet-advertisement/v2", params).then(res => {
if (res.code == 1) {
this.setdata({
advertiseflag: res.data.status == 1 ? true : false,
advertisemsg: res.data,
advertisingpopup: {
...res.data
}
});
if (res.data.id) {
wx.hidetabbar();
}
if (res.data.status == 1) {
timer = setinterval(() => {
if (this.data.defaulttime > 0) {
let time = (this.data.defaulttime -= 1);
this.setdata({
defaulttime: time
});
return;
}
if (!this.data.defaulttime) {
clearinterval(timer);
this.islike();
}
}, 1000);
}
}
});
},
//是否感兴趣
islike() {
let id = this.data.advertisemsg.id;
if (id) {
api.get(`/main-service/applet-advertisement/${id}`).then(res => {
if (res.code == 1) {
console.log("感兴趣");
}
});
}
},
// 跳转详情
handlejumpdetails(item) {
let {
id,
jumptype
} = item.currenttarget.dataset.value
if (jumptype == 13) {
wx.navigateto({
url: `/addressmanagement/pages/richtext/richtext?jumpid=${id}`
});
}
},
// 页面跳转
handlejumptopage(e) {
let {
item
} = e.currenttarget.dataset
this.handlehomeconfigclick(item.id)
wx.hideloading()
let data = {
...item,
richtexttype: 2,
appletadvertisementid: item.type
}
wx.hideloading()
this.handlejumptypepage(data)
},
// 根据跳转类型处理页面跳转
handlejumptypepage(item) {
const jumptype = number(item.jumptype);
this.jumpfn()
const routes = {
1: () => this.handlepageto(),
2: () => wx.navigateto({
url: "/addressmanagement/pages/invitingwithcourtesy/invitingwithcourtesy"
}),
3: () => wx.navigateto({
url: `/addressmanagement/pages/invitationgiftdetail/invitationgiftdetail?id=${item.jumpvalue}`
}),
4: () => wx.switchtab({
url: '/pages/activity/activity'
}),
5: () => wx.navigateto({
url: `/pages/activitydetail/activitydetail?activityid=${item.jumpvalue}`
}),
6: () => wx.navigateto({
url: `/addressmanagement/pages/richtext/richtext?jumpid=${item.appletadvertisementid}&type=${item.richtexttype}`
}),
// 7: () => wx.navigateto({ url: `/pages/content-detail/content-detail?id=${item.jumpvalue}&type=1` }),
// 8: () => wx.navigateto({ url: `/pages/content-detail/content-detail?id=${item.jumpvalue}&type=3` }),
9: () => wx.navigateto({
url: `/addressmanagement/pages/richtext/richtext?jumpid=${item.appletadvertisementid}&status=btn&type=${item.richtexttype}`
}),
10: () => wx.navigateto({
url: `/addressmanagement/pages/marketing/marketing?id=${item.type}&type=home`
}),
};
if (routes[jumptype]) {
routes[jumptype]();
}
},
// 点击数据
handleclickdatasave(id) {
let params = {
appletadvertisementcustomerrecordid: id
}
api.get('/main-service/applet-advertisement/save-click-data', params).then((res) => {
if (res.code == 1) {
console.log(res, 'res');
}
}).catch((err) => {
console.log(err, 'err');
})
},
// 跳转类型
handlejumpvalue(e) {
let {
relationhomeswitch,
relationhometype,
id
} = this.data.advertisingpopup
this.handleclickdatasave(id)
if (relationhometype && relationhomeswitch == 1) {
let status = e.currenttarget.dataset.status
// 判断是否有点击过 statusbtn
if (status == 'playing') return;
this.handlegetdom(relationhometype)
} else if (relationhomeswitch == 2) {
let data = {
...this.data.advertisingpopup,
richtexttype: 3,
}
this.handlejumptypepage(data)
} else {
wx.showtabbar();
this.setdata({
advertiseflag: false
});
clearinterval(timer);
}
},
// 获取dom
handlegetdom(type) {
if (!type || type <= 0) return
let _this = this
wx.createselectorquery().select('.advertise-box').boundingclientrect()
.selectall('.grid-container .item').boundingclientrect().exec((ret) => {
const [poprect, enddoms] = ret;
const targetindex = enddoms.findindex((item) => item.id == type);
if (targetindex === -1) return;
const enddom = enddoms[targetindex];
_this.starttransition(poprect, enddom);
})
},
// 开始动画过渡
starttransition(poprect, enddom) {
const _this = this;
// 设置点击状态为playing
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'playing'
}
});
const centerx = enddom.left
const centery = enddom.top
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
animation: "shrinkandmovetoposition 2s forwards"
}
});
createanimation({
from: poprect.left,
to: centerx,
totalms: 1000,
onmove: (n) => {
_this.updatetransitiondata(enddom, centerx, centery);
},
onend: () => {
_this.endtransition(enddom);
}
});
},
// 更新动画过程中的数据
updatetransitiondata(enddom, centerx, centery) {
this.setdata({
transitiondata: {
...this.data.transitiondata,
width: `${enddom.width / 2}px`,
height: `${enddom.height / 2}px`,
left: `${centerx}px`,
top: `${centery}px`
}
});
},
// 结束动画并处理跳转
endtransition(enddom) {
const _this = this;
wx.showtabbar();
_this.setdata({
transitiondata: {
..._this.data.transitiondata,
statusbtn: 'end',
opacity: 0
},
advertiseflag: false
});
clearinterval(timer);
const curritem = {
...enddom.dataset.item,
richtexttype: 2,
appletadvertisementid: enddom.dataset.item.type
}
_this.handlejumptypepage(curritem);
},
// 点击
handlehomeconfigclick(id) {
let params = {
id: id
}
api.post('/main-service/home-config/click', params).then((res) => {
if (res.code == 1) {
console.log(res, 'res');
}
}).catch((err) => {
console.log(err, 'err');
})
},
jumpfn() {
wx.showtabbar();
this.setdata({
advertiseflag: false
});
clearinterval(timer);
},
// 动画end
handlepageto: function () {
let that = this
wx.showloading({
title: '加载中',
mask: true
})
this.checkattestation()
// that.getuserinfo()
},
// getuserinfo() {
// let that = this
// getauthcode().then((res) => {
// const {
// authcode
// } = res
// let params = {
// logintype: 7,
// isrelatedphonenumber: 0,
// grantcode: authcode
// }
// api.post('/main-service/customer/login', params).then(({
// data
// }) => {
// const {
// isrelatedphonenumber,
// phonenumber,
// token
// } = data
// if (isrelatedphonenumber == 0) {
// wx.hideloading()
// wx.relaunch({
// url: '/pages/login/login?sourcetype=prize'
// });
// return
// }
// if (isrelatedphonenumber == 1) {
// wx.hideloading()
// wx.setstoragesync('token', token)
// that.checkattestation()
// return
// }
// })
// })
// },
checkattestation() {
api.get('/main-service/pregnant-certification/status').then(({
data
}) => {
wx.hideloading()
const {
iswhite,
issellout,
status,
identitytype,
issynctaobao
} = data
if (iswhite == 0) {
if (status == 0) {
// 跳转认证页面
wx.navigateto({
url: '/pages/authentication/authentication'
})
}
if (status == 1) {
// 跳转认证中
wx.navigateto({
url: '/pages/addcustomer/addcustomer'
})
}
if (status == 2) {
// 打开领取链接
if (issellout) {
// 礼品售罄
// "identitytype": 1:孕妈 2:宝妈
if (!issynctaobao) {
wx.navigateto({
url: '/pages/sellout/sellout'
})
// if (identitytype == 1) {
// wx.navigateto({
// url: '/pages/sellout/sellout'
// })
// }
// if (identitytype == 2) {
// wx.navigateto({
// url: `/pages/certificationpassed/certificationpassed`
// })
// }
} else {
// 修改之后的逻辑
wx.navigateto({
url: `/pages/giftguide/giftguide?status=${status}`
})
}
} else {
wx.navigateto({
url: `/pages/giftguide/giftguide?status=${status}`
})
// if (identitytype == 1) {
// wx.navigateto({
// url: `/pages/giftguide/giftguide?status=${status}`
// })
// }
// if (identitytype == 2) {
// wx.navigateto({
// url: `/pages/certificationpassed/certificationpassed`
// })
// }
}
}
if (status == 3) {
// 跳转拒绝页面
wx.navigateto({
url: '/pages/certificationreject/certificationreject'
})
}
if (status == 4) {
// 跳转退回页面重新编辑
wx.navigateto({
url: `/pages/authentication/authentication?status=${status}`
})
}
}
if (iswhite == 1) {
if (issellout) {
wx.navigateto({
url: '/pages/sellout/sellout'
})
} else {
wx.navigateto({
url: `/pages/giftguide/giftguide?status=${status}`
})
}
}
})
},
handleclickbanner() {
wx.navigateto({
url: '/addressmanagement/pages/bannerdetail/bannerdetail',
})
},
// 跳转会场
handletoshare() {
wx.navigateto({
url: `/addressmanagement/pages/taobaovenue/taobaovenue?type=home`,
});
},
// 开启分享
onshareappmessage() {},
// 判断是否展示该 item
shoulddisplayitem(item) {
if (item.type >= 5 && item.showswitch !== 1) return false;
return !!item.url || item.type <= 4; // 当 type 小于等于 4 时总是展示
},
// 根据 type 返回对应的类名
getclassbytype(type) {
switch (type) {
case 1:
return 'new-mom-gift';
case 2:
return 'xhs-volunteer';
case 3:
return 'douyin-volunteer';
case 4:
return 'invitation-gift';
case 5:
return 'advertising-space-one';
case 6:
return 'advertising-space-two';
case 7:
return 'advertising-space-three';
default:
return '';
}
},
// 根据 type 返回图片的 mode
getimagemode(type) {
return type >= 5 ? 'widthfix' : 'scaletofill';
},
// 获取默认图片 url
getdefaultimage(type) {
return 'https://yizhen-mamapai-dev.oss-cn-zhangjiakou.aliyuncs.com/certification/2024-06-13/3cb773c6e3614c389619a24d55f868d4.png';
},
onpulldownrefresh() {
promise.all([this.gethomeconfigpage()]).then(res => {
this.stoppulldownrefresh();
});
},
onunload() {
this.jumpfn()
},
})到此这篇关于微信/支付宝小程序实现弹窗动画缩放到某个位置的文章就介绍到这了,更多相关弹窗动画缩放到某个位置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论