Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

javascript - JavaScript:滚动和获取项目位置会触发错误(JavaScript: Scrolling and getting item position triggers error)

I'm checking if an element is visible at the bottom of the page to apply auto pagination with scroll.

(我正在检查元素是否在页面底部可见,以通过滚动应用自动分页。)

It works fine and ajax gets fired but I keep getting this in the log

(它工作正常,ajax被解雇,但我一直在日志中获取)

allNews:418 Uncaught TypeError: Cannot read property 'top' of undefined
at isScrolledIntoView (allNews:418)

Here's the code and note that img#loader is not visible by default for sure

(这是代码,请注意,确保默认情况下img#loader不可见)

<script>
    $(document).ready(function() {

       $(window).scroll(function () {
            if (isScrolledIntoView("img#loader")) {
                var cur_page = parseInt($("#cur_page").text());
                var last_page = parseInt($("#last_page").text());
                $("img#loader").remove();
                if (cur_page < last_page) {
                    $(".blog_loading").css('display','block');
                    cur_page++;
                    $("#cur_page").text(cur_page);
                    $.ajax({
                        type: 'GET',
                        url: '/newsPaginate/<?=$lang?>/' + cur_page,
                        success: function (response) {
                            //getting data and append it here
                            $("#bottomLinks").append('<img id="loader" src="/images/loader.svg" style="visibility: hidden; display: block">');
                            $(".blog_loading").css('display','none');
                        }
                    });
                }

            }
            else {
                //console.log('no');
            }
        });

    });

    function isScrolledIntoView(elem)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
        var elemTop = $(elem).offset().top;
        return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
    }
</script>

How to fix this to stop logging this to the console

(如何修复此问题以停止将其记录到控制台)

  ask by PHP User translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are saying that the ' elem ' is not always on the page, you change the isScrolledIntoView function to this:

(如果您说的不是' elem '总是在页面上,则将isScrolledIntoView函数更改为:)

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    if ($(elem).length) {
        var elemTop = $(elem).offset().top;
        return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
    }
    return false;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...