Skip to content

Commit

Permalink
Fixed issue with overflowing parent elements. Fixes jackmoore#298.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmoore committed Jul 13, 2016
1 parent 1e94f8a commit b7f7306
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

##### v.3.0.16 - 2016-7-13
* Fixed issue with overflowing parent elements. Fixes #298.

##### v.3.0.15 - 2016-1-26
* Used newer Event constructor, when available. Fixes #280.

Expand Down
30 changes: 25 additions & 5 deletions dist/autosize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
Autosize 3.0.15
Autosize 3.0.16
license: MIT
http://www.jacklmoore.com/autosize
*/
Expand Down Expand Up @@ -108,10 +108,25 @@
resize();
}

function getParentOverflows(el) {
var arr = [];

while (el && el.parentNode && el.parentNode instanceof Element) {
if (el.parentNode.scrollTop) {
arr.push({
node: el.parentNode,
scrollTop: el.parentNode.scrollTop });
}
el = el.parentNode;
}

return arr;
}

function resize() {
var htmlTop = window.pageYOffset;
var bodyTop = document.body.scrollTop;
var originalHeight = ta.style.height;
var overflows = getParentOverflows(ta);
var docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)

ta.style.height = 'auto';

Expand All @@ -129,8 +144,13 @@
clientWidth = ta.clientWidth;

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
overflows.forEach(function (el) {
el.node.scrollTop = el.scrollTop;
});

if (docTop) {
document.documentElement.scrollTop = docTop;
}
}

function update() {
Expand Down
4 changes: 2 additions & 2 deletions dist/autosize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "autosize",
"description": "Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.",
"version": "3.0.15",
"version": "3.0.16",
"keywords": [
"textarea",
"form",
Expand Down
29 changes: 25 additions & 4 deletions src/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,26 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
resize();
}

function getParentOverflows(el) {
const arr = [];

while (el && el.parentNode && el.parentNode instanceof Element) {
if (el.parentNode.scrollTop) {
arr.push({
node: el.parentNode,
scrollTop: el.parentNode.scrollTop,
})
}
el = el.parentNode;
}

return arr;
}

function resize() {
const htmlTop = window.pageYOffset;
const bodyTop = document.body.scrollTop;
const originalHeight = ta.style.height;
const overflows = getParentOverflows(ta);
const docTop = document.documentElement && document.documentElement.scrollTop; // Needed for Mobile IE (ticket #240)

ta.style.height = 'auto';

Expand All @@ -101,8 +117,13 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) {
clientWidth = ta.clientWidth;

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
overflows.forEach(el => {
el.node.scrollTop = el.scrollTop
});

if (docTop) {
document.documentElement.scrollTop = docTop;
}
}

function update() {
Expand Down

0 comments on commit b7f7306

Please sign in to comment.