首页 > 解决方案 > 如何访问函数内部的变量。js中的ES6模块?

问题描述

我正在尝试重构我的代码并将淡出动画提取到 ES6 模块中。但是,我需要在我的主文件中访问一个属性。这怎么可能实现。

我需要能够访问animationindex.js 中的 fadeOut 函数内的变量,以便能够收听动画并继续我的其余代码。

// index.js
import * as Utils from './utilities.js';

Utils.fadeOut(triviaContainerEl, '.trivia-item')
const animation = triviaDiv.animate(keyframes, options)
animation.addEventListener('finish', () => { ... } //Currently, I'm unable to access animation because the fadeOut fn is in another file
// utilities.js
export function fadeOut(parentElement, selector) {
    const animatedElement = parentElement.querySelector(selector)
    const keyframes = [{
        opacity: 1
    }, {
        opacity: 0
    }]
    const options = {
        duration: 500,
        easing: 'ease-out',
        delay: 500
    }
    const animation = animatedElement.animate(keyframes, options)
}

标签: javascriptanimationes6-modules

解决方案


推荐阅读