Is it possible to write an “Infinite Scroll” javascript that can handle the back button?

后端 未结 2 1016
离开以前
离开以前 2021-02-06 04:45

Go here: http://www.infinite-scroll.com/ Play with the infinite scroll. Notice that you can\'t click a link and then hit \"back\". It has glitches when you do that.

So,

2条回答
  •  悲&欢浪女
    2021-02-06 05:26

    The only way to be able to handle the back button is to alter the location hash (the bit after the # symbol). Handling pressing of the back and forward buttons can be done in two ways:

    1. Have an which is unique to each section added, and change the location.hash to match it.
    2. Use a setInterval with a short time like 100ms to watch the location.hash for changes and go to the required part of the page. You have to do this because you can't actually detect when the back/forward buttons are clicked.

    This is an implementation of the second method to show you how it works. In this, each ajax request contains and ajax-section-2 etc.

    function addToBottom(htmlToAdd) {
        $('#main-div').append(htmlToAdd);
        window.location.hash = $('#main-div a[name^=ajax-section-]').last().attr('name');
    }
    

提交回复
热议问题