topfans/frontend/pages/support-activity/components/bubble-animation.wxs
2026-04-07 23:08:49 +08:00

81 lines
2.2 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 气泡动画 WXS 模块
* 在视图层独立线程中执行,不占用 JS 主线程
*/
/**
* 生成随机的左边距位置(百分比)
* @param {number} textLength - 文本长度
* @param {number} viewportWidth - 视口宽度
* @returns {number} 左边距百分比
*/
function generateLeftPosition(textLength, viewportWidth) {
// 预估气泡宽度
var estimatedBubbleWidth = textLength * 12 + 48
var bubbleWidthPercent = (estimatedBubbleWidth / viewportWidth) * 100
// 确保气泡完全在可视区域内
var minLeft = 5
var maxLeft = 95 - bubbleWidthPercent
if (maxLeft < minLeft) {
maxLeft = minLeft
}
var range = maxLeft - minLeft
var random = Math.random()
return random * range + minLeft
}
/**
*
* @returns {number}
*/
function generateAnimationDuration() {
var random = Math.random()
return random * 2 + 4 // 4-6
}
/**
*
* @param {string} text -
* @param {number} viewportWidth -
* @returns {string}
*/
function getBubbleStyle(text, viewportWidth) {
var textLength = text.length
var leftPosition = generateLeftPosition(textLength, viewportWidth)
var duration = generateAnimationDuration()
return 'left: ' + leftPosition + '%; animation-duration: ' + duration + 's;'
}
/**
* transform
* @param {number} progress - 0-1
* @returns {string} transform
*/
function calculateTransform(progress) {
//
var translateY = -100 - (progress * 200) // -100rpx -300rpx
//
var translateX = progress * 20 // 0 20rpx
//
var opacity = 1
if (progress < 0.1) {
opacity = progress * 10 // 0-0.1:
} else if (progress > 0.9) {
opacity = (1 - progress) * 10 // 0.9-1: 淡出
}
return 'transform: translate(' + translateX + 'rpx, ' + translateY + 'rpx); opacity: ' + opacity + ';'
}
module.exports = {
generateLeftPosition: generateLeftPosition,
generateAnimationDuration: generateAnimationDuration,
getBubbleStyle: getBubbleStyle,
calculateTransform: calculateTransform
}