WishMeLz

生活其实很有趣

JS判断是否在当前页面

    var hiddenProperty = 'hidden' in document ? 'hidden' :
        'webkitHidden' in document ? 'webkitHidden' :
            'mozHidden' in document ? 'mozHidden' :
                null;
    var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
    var onVisibilityChange = function () {
        if (!document[hiddenProperty]) {
            document.querySelector('.title').innerHTML = '在这里了';
        } else {
            document.querySelector('.title').innerHTML = '离开了';
        }
    }
    document.addEventListener(visibilityChangeEvent, onVisibilityChange);