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 に答える