0

jQuery 的新手,无法让这段代码的最后一段工作。我试图在用户通过单击链接离开页面时显示一个简单的警报,如果他们没有通过滚动到底部看到所有页面内容。

我不确定它是否应该阻止用户进入下一页,但现在我只想获取检测用户是否已到达底部并使其与我拥有的代码一起工作的部分。我已经有了检测是否有滚动条的部分,并且在用户到达底部时卡住了如何注册:

jQuery:

        <script>
        $(document).ready(function() {
            var verticalScrollPresent = function() {
                return document.documentElement.scrollHeight !== document.documentElement.clientHeight;
            }

            $('#nav').click(function() {
                if (verticalScrollPresent) {
//imagining that this is where the code will go, but how can I register that the user     has scrolled to the bottom?
                        alert("There's a whole bunch of stuff you haven't seen yet!");
                }
            });
        });

    </script>

HTML

<a id="nav" href="http://google.com">Next page</a>
4

1 回答 1

3

将变量设置为 false,当用户滚动到底部时,将变量设置为 true,在用户单击链接时检查该变量:

<script type = "text/javascript"> 
    $(document).ready(function() {
        var beenToBottom = false;
        $(window).on('scroll', function() {
            //guesstimated scrolled to bottom, adjust as neccessary
            if (($(this).scrollTop() + $(this).height() + 200) > $(document).height()) {
                beenToBottom = true;
            }
        });

        $('#nav').click(function(e) {
            e.preventDefault();
            if (!beenToBottom) {
                alert("There's a whole bunch of stuff you haven't seen yet!");
            }else{
                document.location.href = this.href;
            }
        });
    });
</script>​
于 2012-12-11T21:26:15.027 回答