点赞、收藏,放大缩小动画三端通用
感谢npro提供的animation函数方法!
XML/HTML Code复制内容到剪贴板
- <div class="n-flex-row n-flex-one n-align-center n-justify-center" @click="handleDig">
- <image ref="action-dig" class="bottom-icon" :src="detail.is_dig?'/static/img/icon-like2.png':'/static/img/icon-like.png'" mode="widthFix" :style="digAni"></image>
- <text :class="'bottom-text ' + (detail.is_dig?'active':'')">{{detail.dig_num}}</text>
- </div>
- <div class="n-flex-row n-flex-one n-align-center n-justify-center" @click="handleFave">
- <image ref="action-fave" class="bottom-icon" :src="detail.is_fave?'/static/img/icon-shoucang2.png':'/static/img/icon-shoucang.png'" mode="widthFix" :style="faveAni"></image>
- <text class="bottom-text">{{detail.fava_num}}</text>
- </div>
XML/HTML Code复制内容到剪贴板
- import {animate} from '@/nPro/utils/animate.js'
- export default {
- data() {
- return {
- detail:{
- dig_num: 5160,
- is_dig:false, // 是否点赞
- fava_num:510,
- is_fave:false, // 是否收藏
- },
- faveAni:'', // 收藏动画的样式
- faveAnimation: false, // 收藏动画
- digAni:'',
- digAnimation: false
- }
- },
- methods: {
- async handleDig(){
- if (!this.digAnimation) {
- this.digAnimation = true
- await this.timingScale("action-dig","digAni")
- this.digAnimation = false
- this.digAni = ''
- this.detail.is_dig = !this.detail.is_dig
- if(this.detail.is_dig){
- this.detail.dig_num++
- }else{
- this.detail.dig_num--
- }
- } else {
- }
- },
- async handleFave(){
- if (!this.faveAnimation) {
- this.faveAnimation = true
- await this.timingScale('action-fave','faveAni')
- this.faveAnimation = false
- this.faveAni = ''
- this.detail.is_fave = !this.detail.is_fave
- if(this.detail.is_fave){
- this.detail.fava_num++
- }else{
- this.detail.fava_num--
- }
- } else {
- }
- },
- timingScale(ref,ani) {
- const _this = this
- return new Promise((resolve, reject) => {
- const el = this.$refs[ref]
- animate(el,{
- styles: {
- transform: 'scale(1.5)'
- },
- duration: 200,
- timingFunction: 'ease-in-out'
- },_this, ani).then(res => {
- setTimeout(e => {
- animate(el,{
- styles: {
- transform: 'scale(1)'
- },
- duration: 200,
- timingFunction: 'ease-in-out'
- },_this, ani).then(res => {
- // console.log(res)
- resolve()
- }).catch( err => {
- console.log(2)
- })
- }, 10);
- }).catch(err => {
- console.log(1)
- reject(err)
- })
- })
- }
- },
animate.js
JavaScript Code复制内容到剪贴板
- // #ifdef APP-NVUE
- const animation = uni.requireNativePlugin('animation')
- const bindingX = uni.requireNativePlugin('bindingx');
- // #endif
- // https://weex.apache.org/zh/docs/modules/animation.html#transition
- export function animate(ref, options, ins, name, extra) {
- if (!options) return Promise.reject("请设置动画内容")
- // #ifdef APP-NVUE
- if (!ref) return Promise.reject("请指定元素")
- return new Promise((resolve, reject) => {
- animation.transition(ref, {
- styles: options.styles,
- duration: options.duration || 0,
- delay: options.delay || 0,
- timingFunction: options.timingFunction || 'ease',
- needLayout: options.needLayout || false
- }, (res)=>{
- resolve(res || {})
- })
- })
- // #endif
- // #ifndef APP-NVUE
- if (!ins || !name) return Promise.reject("请指明实例和变量")
- const styles = options.styles || {}
- const keys = Object.keys(styles)
- const tps = []
- keys.forEach(item => {
- tps.push(toLine(item))
- })
- let sty = ''
- sty += `transition-property:${tps.join(',')};`
- for (const i in keys) {
- const theK = keys[i]
- sty += `${tps[i]}:${styles[theK]};`
- }
- sty += `transition-duration: ${options.duration||0}ms;transition-delay: ${options.delay||0}ms;transition-timing-function:${options.timingFunction || 'ease'};`
- ins[name] = sty + (extra || '')
- const dur = (options.duration || 0) * 1 + (options.delay || 0) * 1
- if (dur > 0) {
- return new Promise((resolve, reject) => {
- // setTimeout(resolve, dur, {})
- setTimeout(()=>{
- resolve({})
- }, dur)
- })
- } else {
- return Promise.resolve({})
- }
- // #endif
- }
- // 驼峰转中横线
- function toLine(name) {
- return name.replace(/([A-Z])/g, '-$1').toLowerCase()
- }
上一篇 小程序-微信登录