Giter Club home page Giter Club logo

Comments (2)

cihankaracom avatar cihankaracom commented on August 25, 2024 3

I notice this problem for years. I decided to fix it. So i did it in this way:

First of all, use unminified version of js, after fixing minify it again.

Replace "proto.onUnload = function" function with this function:

proto.onVisibilityChange = function() {
  if (!this.scrollPage) return;
  if (this.scrollPage.top === 0) return;
  if (document.visibilityState === 'visible') return;

  // calculate where scroll position would be on refresh
  let scrollY = window.scrollY - this.scrollPage.top + this.top;
  // disable scroll event before setting scroll #679
  this.destroyHistory();
  scrollTo(0, scrollY);
};

also add this function below this function:

proto.bindVisibilityChangeEvent = function(isBind) {
  let addRemove = isBind ? 'addEventListener' : 'removeEventListener';
  window[addRemove]('visibilitychange', this.onVisibilityChange);
};

then find "proto.createHistoryAppend = function()" function, replace it this one:

proto.createHistoryAppend = function() {
  this.updateMeasurements();
  this.updateScroller();
  // array of scroll positions of appended pages
  this.scrollPages = [
    // first page
    {
      top: 0,
      path: location.href,
      title: document.title,
    },
  ];
  this.scrollPage = this.scrollPages[0];
  // events
  this.scrollHistoryHandler = this.onScrollHistory.bind( this );
  this.visibilityChangeHandler = this.onVisibilityChange.bind(this);
  this.scroller.addEventListener( 'scroll', this.scrollHistoryHandler );
  this.on( 'append', this.onAppendHistory );
  this.bindHistoryAppendEvents( true );
  this.bindVisibilityChangeEvent(true);
};

finally, replace "proto.bindHistoryAppendEvents = function(isBind)" function with this one:

proto.bindHistoryAppendEvents = function(isBind) {
  let addRemove = isBind ? 'addEventListener' : 'removeEventListener';
  this.scroller[addRemove]('scroll', this.scrollHistoryHandler);
  this.bindVisibilityChangeEvent(isBind);
};

I did not encounter any problems when I made changes this way. If you encounter any problems, I would appreciate it if you let me know.

from infinite-scroll.

Garnet-Fox avatar Garnet-Fox commented on August 25, 2024

I made a small fix because it’s urgent, it might be useful to someone. Do not judge strictly.
The "beforeunload" event seems to correctly calc the scrolling, but the scrolling itself is visible - unfortunately.
Event "pagehide" - failed to make friends with Safari, i.e. For the Chromium line everything is great, but in Safari the scroll calculation fails the first time you go back.
I've settled on "visibilitychange" for now, no critical bugs seem to be visible, currently browser support is 95%.
Instead of lines:
window[ addRemove ]( 'unload', this.unloadHandler );
Fix:
var _this = this;
function fix(e) {
if (document.visibilityState === "hidden") {
_this.unloadHandler.call(document, e);
}
}
document[ addRemove ]( 'visibilitychange', fix);

from infinite-scroll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.