Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window scroll bindings are bidirectional #461

Merged
merged 1 commit into from
Apr 11, 2017
Merged

Conversation

Rich-Harris
Copy link
Member

Ref #404. With this PR, in a situation like this...

<:Window bind:scrollY='y'/>

...changing the value of y programmatically will result in the window scrolling. (The value of y remains constrained to the scrollable range.)

In a lot of situations this kind of bidirectional binding is unnecessary, but it would violate expectations if it didn't work this way so I think it's worth the modest overhead, which basically amounts to this:

+var window_updating = false;
  
var onwindowscroll = function ( event ) {
+  window_updating = true;
  
  component.set({
    y: this.scrollY
  });
  
+  window_updating = false;
};
window.addEventListener( 'scroll', onwindowscroll );

+component.observe( 'y', function ( y ) {
+  if ( window_updating ) return;
+  window.scrollTo( window.scrollX, y );
+});

Combined with an animate or tween method (#236), this would provide a very straightforward way of smoothly scrolling to a section of the page, for example.

@Rich-Harris Rich-Harris merged commit 229d9f0 into master Apr 11, 2017
@Rich-Harris Rich-Harris deleted the gh-404-scroll branch April 11, 2017 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant