On many sites (example VKontakte, Habr, Peekaboo) I see how when you click on a button, we first go up, and when we click again, we go back to the position we were in when we clicked the first time on the button, and when scrolling her new position. And when you repeat the operation, the function will return us to a new position, but the first click from the current position always raises us to the top.
I tried to write the following script, where the current position of the element with each click is recorded in the data-effset , and everything almost works, but if you interrupt the function by scrolling with the mouse wheel or simply by clicking the button, without waiting for the scrolling to end, the function will work with an error (moves us between the current position and where we started to turn the wheel). Perhaps you need to put a flag, but not at all think how ...
$('#button').click(function() { scrtop = $(window).scrollTop() offsetnow = $(this).attr('data-effset'); $(this).attr('data-effset', scrtop); $("body").animate({ "scrollTop": offsetnow }, "slow"); }); .folder { display: block; position: relative; width: 80%; margin: 0 auto; height: 25000px; background: #80CBC4; } #yack { display: block; position: fixed; top: 0; } #button { display: block; position: fixed; top: 33%; background: #E65100; color: #fff; padding: 8px 25px; cursor: pointer; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class='folder'></div> <div id='button' data-effset='0'>ΠΠ²Π΅ΡΡ
/Π²Π½ΠΈΠ·</div>