Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.

Commit 5e1b125

Browse files
committed
added ability to specify parent element for scrolling
1 parent 826b0ab commit 5e1b125

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Your `playerOptions` should be an object with the following properties:
3737
- `source`: Your story's ficdown code. Either store it right in the html document, or make an XHR to pull the story content in from an external file, and put its content here.
3838
- `id`: The id of a div on the page to inject the game into. For example if your html is `<div id='game'/>` then you would pass `game` here.
3939
- `scroll` (optional): Set this to `true` if you want the player's full game history to remain on the screen and automatically scroll the page down whenever a new scene is added to the bottom. By default each scene will replace the previous one and the page will be scrolled to the top.
40+
- `scrollParent` (optional): The container to scroll. Used even if `scroll=false` to reset the page position to the top whenever a new scene is displayed. Defaults to the document or `body` element of the page.
4041
- `html` (optional): Set this to true if your ficdown file contains raw html that you want rendered. By default html will not be rendered.
4142
- `startText` (optional): Set this to override the link text that begins the game.
4243
- `endMarkdown` (optional): Set this to override the "game over" content that is displayed when the player reaches a scene with no more links to follow. Include an anchor with the href set to `restart()` if you want a link that will start the story over from the beginning.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ficdown.js",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "A parser and player for Interactive Fiction written in Ficdown",
55
"scripts": {
66
"build": "rm -rf ./build && tsc",

src/Model/PlayerOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type PlayerOptions = {
22
source: string, // ficdown story source
33
id: string, // id of div to inject game into
44
scroll?: boolean, // continuous scroll mode
5+
scrollParent?: string, // id of the parent container to scroll
56
html?: boolean, // allow html in story source
67
startText?: string, // custom link text to start game
78
endMarkdown?: string, // custom markdown when game ends

src/Player.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ export class Player {
104104
const scrollId = `move-${ this.moveCounter++ }`;
105105
this.container.append($('<span/>').attr('id', scrollId));
106106
this.container.append(newHtml);
107-
$([document.documentElement, document.body]).animate({
107+
const scrollParent = this.options.scrollParent
108+
? $(`#${ this.options.scrollParent }`)
109+
: $([document.documentElement, document.body]);
110+
scrollParent.animate({
108111
scrollTop: $(`#${ scrollId }`).offset()!.top,
109112
}, 1000);
110113
} else {
111114
this.container.html(newHtml);
112-
window.scrollTo(0, 0);
115+
if (this.options.scrollParent) {
116+
$(`#${ this.options.scrollParent }`)[0].scrollTop = 0;
117+
} else {
118+
window.scrollTo(0, 0);
119+
}
113120
}
114121
this.wireLinks();
115122
this.checkGameOver();

0 commit comments

Comments
 (0)