0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Merge pull request #1176 from sveltejs/gh-1175

Using pageYOffset & pageXOffset
This commit is contained in:
Rich Harris 2018-02-25 08:59:59 -05:00 committed by GitHub
commit 8aaf92aca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 13 deletions

View File

@ -19,6 +19,11 @@ const associatedEvents = {
scrollY: 'scroll',
};
const properties = {
scrollX: 'pageXOffset',
scrollY: 'pageYOffset'
};
const readonly = new Set([
'innerWidth',
'innerHeight',
@ -93,15 +98,16 @@ export default class Window extends Node {
if (attribute.name === 'online') return;
const associatedEvent = associatedEvents[attribute.name];
const property = properties[attribute.name] || attribute.name;
if (!events[associatedEvent]) events[associatedEvent] = [];
events[associatedEvent].push(
`${attribute.value.name}: this.${attribute.name}`
`${attribute.value.name}: this.${property}`
);
// add initial value
generator.metaBindings.push(
`this._state.${attribute.value.name} = window.${attribute.name};`
`this._state.${attribute.value.name} = window.${property};`
);
}
});
@ -158,10 +164,10 @@ export default class Window extends Node {
clearTimeout(${timeout});
var x = ${bindings.scrollX
? `#component.get("${bindings.scrollX}")`
: `window.scrollX`};
: `window.pageXOffset`};
var y = ${bindings.scrollY
? `#component.get("${bindings.scrollY}")`
: `window.scrollY`};
: `window.pageYOffset`};
window.scrollTo(x, y);
${timeout} = setTimeout(${clear}, 100);
}
@ -182,7 +188,7 @@ export default class Window extends Node {
#component.observe("${bindings.scrollX || bindings.scrollY}", function(${isX ? 'x' : 'y'}) {
${lock} = true;
clearTimeout(${timeout});
window.scrollTo(${isX ? 'x, window.scrollY' : 'window.scrollX, y'});
window.scrollTo(${isX ? 'x, window.pageYOffset' : 'window.pageXOffset, y'});
${timeout} = setTimeout(${clear}, 100);
});
`);

View File

@ -198,7 +198,7 @@ function create_main_fragment(component, state) {
window_updating = true;
component.set({
y: this.scrollY
y: this.pageYOffset
});
window_updating = false;
}
@ -207,7 +207,7 @@ function create_main_fragment(component, state) {
component.observe("y", function(y) {
window_updating = true;
clearTimeout(window_updating_timeout);
window.scrollTo(window.scrollX, y);
window.scrollTo(window.pageXOffset, y);
window_updating_timeout = setTimeout(clear_window_updating, 100);
});
@ -243,7 +243,7 @@ function create_main_fragment(component, state) {
function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
this._state.y = window.scrollY;
this._state.y = window.pageYOffset;
this._fragment = create_main_fragment(this, this._state);

View File

@ -9,7 +9,7 @@ function create_main_fragment(component, state) {
window_updating = true;
component.set({
y: this.scrollY
y: this.pageYOffset
});
window_updating = false;
}
@ -18,7 +18,7 @@ function create_main_fragment(component, state) {
component.observe("y", function(y) {
window_updating = true;
clearTimeout(window_updating_timeout);
window.scrollTo(window.scrollX, y);
window.scrollTo(window.pageXOffset, y);
window_updating_timeout = setTimeout(clear_window_updating, 100);
});
@ -54,7 +54,7 @@ function create_main_fragment(component, state) {
function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
this._state.y = window.scrollY;
this._state.y = window.pageYOffset;
this._fragment = create_main_fragment(this, this._state);

View File

@ -2,10 +2,10 @@ export default {
skip: true, // JSDOM
test ( assert, component, target, window ) {
assert.equal( window.scrollY, 0 );
assert.equal( window.pageYOffset, 0 );
component.set({ scrollY: 100 });
assert.equal( window.scrollY, 100 );
assert.equal( window.pageYOffset, 100 );
component.destroy();
}