diff --git a/README.md b/README.md index c9b3a33..81df9e0 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Your `playerOptions` should be an object with the following properties: - `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. - `id`: The id of a div on the page to inject the game into. For example if your html is `
` then you would pass `game` here. - `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. +- `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. - `html` (optional): Set this to true if your ficdown file contains raw html that you want rendered. By default html will not be rendered. - `startText` (optional): Set this to override the link text that begins the game. - `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. diff --git a/package.json b/package.json index 72353c6..fb2ce14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ficdown.js", - "version": "2.0.3", + "version": "2.0.4", "description": "A parser and player for Interactive Fiction written in Ficdown", "scripts": { "build": "rm -rf ./build && tsc", diff --git a/src/Model/PlayerOptions.ts b/src/Model/PlayerOptions.ts index 8f17cee..baacb85 100644 --- a/src/Model/PlayerOptions.ts +++ b/src/Model/PlayerOptions.ts @@ -2,6 +2,7 @@ export type PlayerOptions = { source: string, // ficdown story source id: string, // id of div to inject game into scroll?: boolean, // continuous scroll mode + scrollParent?: string, // id of the parent container to scroll html?: boolean, // allow html in story source startText?: string, // custom link text to start game endMarkdown?: string, // custom markdown when game ends diff --git a/src/Player.ts b/src/Player.ts index 999968b..ff26c3b 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -104,12 +104,19 @@ export class Player { const scrollId = `move-${ this.moveCounter++ }`; this.container.append($('').attr('id', scrollId)); this.container.append(newHtml); - $([document.documentElement, document.body]).animate({ + const scrollParent = this.options.scrollParent + ? $(`#${ this.options.scrollParent }`) + : $([document.documentElement, document.body]); + scrollParent.animate({ scrollTop: $(`#${ scrollId }`).offset()!.top, }, 1000); } else { this.container.html(newHtml); - window.scrollTo(0, 0); + if (this.options.scrollParent) { + $(`#${ this.options.scrollParent }`)[0].scrollTop = 0; + } else { + window.scrollTo(0, 0); + } } this.wireLinks(); this.checkGameOver();