A tiny module to fix resize problem on responsive webpages. See Demo
With bower
bower install rescroll
Include dist/rescroll.js
in your page.
With browserify
npm install rescroll
Rescroll exposes a global function InitRescroll
which accepts the following parameters:
resizeTarget
: element whose resize event will cause changes or reflow to other elements, default iswindow
scrollTarget
: element to be scrolled back, default isdocument.body
anchors
: anchors to be used as anchor to fix viewport's position on, default isnull
. When initialized, on each resize event onresizeTarget
, the processor will find the anchor closest tofocusCenterY * viewportHeight
and try to scroll back to the right position based on the ratio of height change of that anchor.focusCenterY
: expected user's reading position, default is0.5
, which is half of viewport's height.
anchors
is an array of objects of following type
{
element: [HTMLElement],
type: ['Box'|'Text']
}
To fix viewport on any p
element closest to the middle of viewport, do the following:
var anchors = [];
var boxes = document.querySelectorAll('p');
for (var i = 0; i < boxes.length; i++)
anchors.push({
element: boxes[i],
type: 'Box'
});
InitRescroll({
anchors: anchors
});