Created
December 4, 2015 11:37
-
-
Save nathanhornby/11fad799dc2ab82476c8 to your computer and use it in GitHub Desktop.
Scroll to active field in form (typeform style animation) using velocity.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Insert into some kind of 'focus' action | |
var parent = $(this).parent().parent('.fieldWrap'); // Target a wrapper | |
// Reset active state | |
$('.fieldWrap').removeClass('active'); | |
// Add active state to current field | |
parent.addClass('active'); | |
var fieldHeight = parent.outerHeight(); | |
var windowHeight = $(window).height(); | |
var scrollOffset = (windowHeight - fieldHeight)/2; | |
parent.velocity('scroll', { | |
duration: 300, | |
offset: -scrollOffset, | |
easing: 'easeOutSine' | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can someone please provide a working example of how to use the above code?