tip.js 920 Bytes
/**
 *  tip
 *  @author chenxuan <xuan.chen@yoho.cn>
 *
 *  @example
 *  tip('lalalalal~')
 *
 * @example
 *  tip({
 *     txt: 'lalalalal',
 *     delay: 2000
 *  })
 */

const $ = require('jquery');

function tip(param) {
    let timer;
    const viewData = {
        mask: false,
        delay: 2000,
        txt: ''
    };

    if (typeof param === 'string') {
        Object.assign(viewData, {
            txt: param
        });
    } else {
        Object.assign(viewData, param);
    }

    const tipView = `<div class="tip-box"><div class="tip">${viewData.txt}</div></div>`;
    const $oldTip = $('.tip-box');

    if ($oldTip.length) {
        timer = $oldTip.data('timer');
        clearTimeout(timer);
        $oldTip.remove();
    }

    const $tip = $(tipView).appendTo(document.body);

    $tip.data('timer', setTimeout(function() {
        $tip.remove();
    }, viewData.delay));
}

module.exports = tip;