为detect-element-resize.js编写TypeScript定义文件(.d.ts)

时间:2016-07-29 06:20:58

标签: javascript typescript

这个JS文件的d.ts文件是什么样的? Here是项目页面。我一直在试图弄清楚如何编写定义文件,但它看起来很复杂,我觉得在这里问一下会更容易,因为我只会使用两种函数。

我会使用的功能如下:

window.addResizeListener = function(element, fn){
    if (attachEvent) element.attachEvent('onresize', fn);
    else {
        if (!element.__resizeTriggers__) {
            if (getComputedStyle(element).position == 'static') element.style.position = 'relative';
            createStyles();
            element.__resizeLast__ = {};
            element.__resizeListeners__ = [];
            (element.__resizeTriggers__ = document.createElement('div')).className = 'resize-triggers';
            element.__resizeTriggers__.innerHTML = '<div class="expand-trigger"><div></div></div>' +
                                                                                    '<div class="contract-trigger"></div>';
            element.appendChild(element.__resizeTriggers__);
            resetTriggers(element);
            element.addEventListener('scroll', scrollListener, true);

            /* Listen for a css animation to detect element display/re-attach */
            animationstartevent && element.__resizeTriggers__.addEventListener(animationstartevent, function(e) {
                if(e.animationName == animationName)
                    resetTriggers(element);
            });
        }
        element.__resizeListeners__.push(fn);
    }
};

window.removeResizeListener = function(element, fn){
    if (attachEvent) element.detachEvent('onresize', fn);
    else {
        element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
        if (!element.__resizeListeners__.length) {
                element.removeEventListener('scroll', scrollListener);
                element.__resizeTriggers__ = !element.removeChild(element.__resizeTriggers__);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

declare interface Window {
    addResizeListener(element: HTMLElement, fn: () => any);
    removeResizeListener(element: HTMLElement, fn: () => any);
}
declare function addResizeListener(element: HTMLElement, fn: () => any);
declare function removeResizeListener(element: HTMLElement, fn: () => any);