前端基础类学习

懒加载

2017-04-26  本文已影响32人  饥人谷_米弥轮

1.如何判断一个元素是否出现在窗口可视范围(浏览器的上边缘和下边缘之间,肉眼可视)。写一个函数 isVisible实现

  function isVisible($node){
        var winHeight = $(window).height();
        var scrollTop = $(window).scrollTop();
        var $offset = $node.offset();
        var $height = $node.outHeight(true);

        if(winHeight + scrollTop > $offset && $offset + $height > scrollTop){
            return true;
        }else{
            return false;
        }
    }

2.当窗口滚动时,判断一个元素是不是出现在窗口可视范围。每次出现都在控制台打印 true 。用代码实现

    var $div = $('div');

    $(window).on('scroll',function(){
        $div.not('.show').each(function(){
            if(isVisible($(this))){
                showDiv($(this))
            }else{
                console.log('false')
            }
        })
        
    })

    function showDiv ($div) {
        $div.each(function(){
            console.log('true')
        })
    }

    function isVisible($node){
        var winHeight = $(window).height();
        var scrollTop = $(window).scrollTop();
        var $offset = $node.offset().top;
        var $height = $node.outerHeight(true);

        if(winHeight + scrollTop > $offset && $offset + $height > scrollTop){
            console.log('winHeight:',winHeight,'scrollTop:',scrollTop,'$offset:',$offset,'$height:',$height)
            return true;
        }else{
            return false;
        }
    }

3.当窗口滚动时,判断一个元素是不是出现在窗口可视范围。在元素第一次出现时在控制台打印 true,以后再次出现不做任何处理。用代码实现

    var $div = $('div');

    $(window).on('scroll',function(){
        $div.not('.show').each(function(){
            if(isVisible($(this))){
                showDiv($(this))
            }else{
                console.log('false')
            }
        })
        
    })

    function showDiv ($div) {
        $div.each(function(){
            $(this).addClass('show')
            console.log('true')
        })
    }

    function isVisible($node){
        var winHeight = $(window).height();
        var scrollTop = $(window).scrollTop();
        var $offset = $node.offset().top;
        var $height = $node.outerHeight(true);

        if(winHeight + scrollTop > $offset && $offset + $height > scrollTop){
            console.log('winHeight:',winHeight,'scrollTop:',scrollTop,'$offset:',$offset,'$height:',$height)
            return true;
        }else{
            return false;
        }
    }

4.图片懒加载的原理是什么?

5. 实现视频中的图片懒加载效果

6.实现视频中的新闻懒加载效果

上一篇 下一篇

猜你喜欢

热点阅读