美文网首页
前端小记

前端小记

作者: pipa | 来源:发表于2019-05-21 20:41 被阅读0次

获取屏幕内所有元素

此处演示只后去文本元素

function isElementInViewport(el) {
    var rect = el.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
        rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
    );
};
var visibleEles = new Array();
function findVisibleElement(el, containerArray) {
    // console.log("type = " + Object.prototype.toString.call(el));
    var array = el.childNodes;
    if (array.length < 1 ) {
        // 去除不需要的 dom style
        if (Object.prototype.toString.call(el) === '[object Text]') {
            var parentnode = el.parentNode;
            if (Object.prototype.toString.call(parentnode) === '[object HTMLScriptElement]'
            ||
            Object.prototype.toString.call(parentnode) === '[object HTMLHeadElement]'
            ||
            Object.prototype.toString.call(parentnode) === '[object HTMLStyleElement]'
            ||
            Object.prototype.toString.call(parentnode) === '[object HTMLBodyElement]' 
            ) {
                return;
            }
            // 过滤空文本
            var content = parentnode.textContent;
            if (content == null || content.length <= 0) {
                // console.log('空字符串');
                return ;
            }
            console.log(parentnode.innerText);
            var iscontain = isElementInViewport(parentnode);
            // console.log(iscontain? "-- 内":"-- 外");       
            if (iscontain) {
                containerArray.push(content);
            }
        }
    } else {
        for (let index = 0; index < array.length; index++) {
            const element = array[index];
            this.findVisibleElement(element, containerArray);
        }
    }
}
findVisibleElement(document.documentElement, visibleEles);
visibleEles.join("");

相关文章

  • 移动端webapp,在进入某页面后,input框自动获取焦点并弹

    “前端小记”---- ----- ****************紧急更新********************...

  • Web扫雷开发小记(1)

    目录Web扫雷开发小记(2)Web扫雷开发小记(3)Web扫雷开发小记(4) 刚好今天做阿里前端笔试问到扫雷了,那...

  • Mac下全局安装uve环境

    前端JS小记 HTMLCollection 与 NodeList 的区别 HTMLCollection是 HTML...

  • 前端小记

    盒子大小包括 margin,border,padding,本身大小 竖直高度= height + padding-...

  • 前端小记

    “简单地记一下这段时间所遇到的前端知识点”1.用于重新计算iframe高度 2.PC端窗口缩放,页面布局不发生改变...

  • 前端小记

    从开始到现在历时一个半月已经做了两个前端项目了,第一个是比较简单的H5单页面应用,只做好一个页面就行了,唯一的点击...

  • 前端小记

    今天在朋友圈看了几个招聘帖子发现前端招聘果然也偏向原理化了,动不动就是深入理解vue,react,angular原...

  • 前端小记

    获取屏幕内所有元素 此处演示只后去文本元素

  • echarts-for-react 2018-04-10

    前端实习小记 echarts-for-react ECharts,一个使用 JavaScript 实现的开源可视化...

  • 前端框架小记

    框架: Material Design Lite (Google 的 Material Design 框架) Ad...

网友评论

      本文标题:前端小记

      本文链接:https://www.haomeiwen.com/subject/foxumqtx.html