Maintain the current position in the document when zooming

This is my third, and hopefully final, attempt to fix #1521 (previous failed attempts were PRs #3446 and #3513).

This PR is slightly more complicated than the previous versions, but I don't think that can be avoided in order to handle all edge cases correctly in different browsers.

I've tested this patch successfully in Firefox, Chrome and IE. Since the previous PRs seemed to cause different issues on different systems, this needs to be tested __*thoroughly*__ before we even consider merging it!

/cc @brendandahl

*Note that this PR depends on #4034, since it includes the code contained in that patch.*