Commit 15656e9d authored by 范雪寒's avatar 范雪寒

fix: 滚动条算法

parent 9796ee64
This diff is collapsed.
...@@ -2130,30 +2130,25 @@ export class ScrollView extends MySprite { ...@@ -2130,30 +2130,25 @@ export class ScrollView extends MySprite {
let maxW = 0; let maxW = 0;
let maxH = 0; let maxH = 0;
const contentRect = { left: 0, right: 0, up: 0, down: 0 };
for (let i=0; i<children.length; i++) { for (let i=0; i<children.length; i++) {
if (children[i] == this.content) { if (children[i] == this.content) {
continue; continue;
} }
const box = children[i].getBoundingBox(); const box = children[i].getBoundingBox();
// console.log('box: ', box);
const boxEdgeX = box.x + box.width;
const boxEdgeY = box.y + box.height;
// console.log('boxEdgeY: ', boxEdgeY); contentRect.left = Math.min(contentRect.left, box.x);
if (boxEdgeX > maxW) { contentRect.right = Math.max(contentRect.right, box.x + box.width);
maxW = boxEdgeX; contentRect.up = Math.min(contentRect.up, box.y);
} contentRect.down = Math.max(contentRect.down, box.y + box.height);
if (boxEdgeY > maxH) {
maxH = boxEdgeY;
}
} }
// console.log('maxW: ', maxW); // console.log('maxW: ', maxW);
// console.log('maxH: ', maxH); // console.log('maxH: ', maxH);
// console.log('this.y: ', this.y); // console.log('this.y: ', this.y);
// console.log(this.getBoundingBox().height); // console.log(this.getBoundingBox().height);
this.content.width = maxW; this.content.width = contentRect.right - contentRect.left;
this.content.height = maxH ; this.content.height = contentRect.down - contentRect.up;
...@@ -2197,12 +2192,11 @@ export class ScrollView extends MySprite { ...@@ -2197,12 +2192,11 @@ export class ScrollView extends MySprite {
} }
refreshScrollBarPos() { refreshScrollBarPos() {
const rect1 = this.getBoundingBox(); const scrollRect = this.getBoundingBox();
const rect2 = this.content.getBoundingBox(); const contentRect = this.content.getBoundingBox();
let rate = rect1.height / rect2.height; let rate = scrollRect.height / (contentRect.height - scrollRect.height);
this._scrollBar.y = -rect2.y * (rate ); this._scrollBar.y = -contentRect.y * (rate );
} }
...@@ -2248,9 +2242,9 @@ export class ScrollView extends MySprite { ...@@ -2248,9 +2242,9 @@ export class ScrollView extends MySprite {
const rect = this.getBoundingBox(); const rect = this.getBoundingBox();
if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) { if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) {
this.content.y = between(this.touchStartContentPos.y + offsetY, 0, -rect.y + rect.height - this.content.height); this.content.y = between(this.touchStartContentPos.y + offsetY, 0, rect.height - this.content.height);
} else { } else {
this.content.x = between(this.touchStartContentPos.x + offsetX, 0, -rect.x + rect.width - this.content.width); this.content.x = between(this.touchStartContentPos.x + offsetX, 0, rect.width - this.content.width);
} }
this.refreshScrollBarPos(); this.refreshScrollBarPos();
} }
...@@ -2267,9 +2261,9 @@ export class ScrollView extends MySprite { ...@@ -2267,9 +2261,9 @@ export class ScrollView extends MySprite {
const rect = this.getBoundingBox(); const rect = this.getBoundingBox();
if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) { if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) {
this.content.y = between(this.content.y + 40, 0, -rect.y + rect.height - this.content.height); this.content.y = between(this.content.y + 40, 0, rect.height - this.content.height);
} else { } else {
this.content.x = between(this.content.x + 40, 0, -rect.x + rect.width - this.content.width); this.content.x = between(this.content.x + 40, 0, rect.width - this.content.width);
} }
this.refreshScrollBarPos(); this.refreshScrollBarPos();
...@@ -2283,9 +2277,9 @@ export class ScrollView extends MySprite { ...@@ -2283,9 +2277,9 @@ export class ScrollView extends MySprite {
const rect = this.getBoundingBox(); const rect = this.getBoundingBox();
if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) { if (this.scrollSide == ScrollView.ScrollSideType.VERTICAL) {
this.content.y = between(this.content.y - 40, 0, -rect.y + rect.height - this.content.height); this.content.y = between(this.content.y - 40, 0, rect.height - this.content.height);
} else { } else {
this.content.x = between(this.content.x - 40, 0, -rect.x + rect.width - this.content.width); this.content.x = between(this.content.x - 40, 0, rect.width - this.content.width);
} }
this.refreshScrollBarPos(); this.refreshScrollBarPos();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment