简体   繁体   English

带返回功能的顶部按钮

[英]To top button with return back function

I try to make "To top" button with return back function: 我尝试使用返回功能使“返回顶部”按钮:

  1. User press button 用户按下按钮
  2. Page scrolls up 页面向上滚动
  3. Button does not disappear 按钮不消失
  4. If press button again, page scrolls down to position, where user press button first time (step 1). 如果再次按下按钮,页面将向下滚动到用户第一次按下按钮的位置(步骤1)。

 $(window).scroll(function() { if ($(this).scrollTop() > 50) { $('#back-to-top').fadeIn(); } else { $('#back-to-top').fadeOut(); } }); // scroll body to 0px on click $('#back-to-top').click(function() { $('#back-to-top').tooltip('hide'); $('body,html').animate({ scrollTop: 0 }, 800); return false; }); $('#back-to-top').tooltip('show'); 
 .back-to-top { cursor: pointer; position: fixed; bottom: 20px; right: 20px; display: none; } 
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <br>test <a id="back-to-top" href="#" class="btn btn-primary btn-lg back-to-top" role="button" title="Click to return on the top page" data-toggle="tooltip" data-placement="left"><span class="glyphicon glyphicon-chevron-up"></span></a> </div> </div> 

How can I return back ones? 我该如何退货?

The below will cover all this important UX cases: 下面将介绍所有这些重要的UX情况:

  • User manually scrolls down & up : Button disappears if scroll less than 50 用户手动上下滚动 :滚动小于50时按钮消失
  • User scrolls down and clicks "To Top" : page animates to top and stores the last known max position. 用户向下滚动并单击“至顶部” :页面将动画设置为顶部,并存储最后一个已知的最大位置。 The button once clicked stays always visible. 单击该按钮后始终保持可见状态。
  • User clicks "Back down" : Page animates to the latest known bottom position 用户单击“后退” :页面动画到最新的已知底部位置
  • User clicks "To top" and than manually scrolls down surpassing the latest known max position : The "Back down" button automatically renames to "To top" - behaving as expected. 用户单击“至顶部”,然后手动向下滚动以超过最新的已知最大位置 :“后退”按钮自动重命名为“至顶部”-行为符合预期。
  • User clicks "To top", than manually surpasses the max position and than scrolls back manually to top : The button behaves as never clicked. 用户单击“至顶部”,然后手动超过最大位置,然后手动向后滚动到顶部 :按钮的行为就像从未单击一样。
  • (As said, the button automatically changes text) (如前所述,该按钮会自动更改文本)

Try them all: 全部尝试:

 const $win = $(window); const $toTop = $('#back-to-top'); const visibleAt = 50; let Y = 0; let clickY = 0; $win.on('scroll', function() { Y = $win.scrollTop(); if(clickY && Y >= clickY) clickY = 0; // In case user surpasses the clickY by manual scroll $toTop .toggleClass('is-visible', !!clickY || Y > visibleAt) .text(clickY ? 'Back down' : 'To top'); }); $toTop.on('click', function(evt) { evt.preventDefault(); $('html, body').stop().animate({scrollTop: clickY ? clickY : 0}); clickY = Y; // Store last click position }); 
 body { height: 400vh; border: 4px dashed #000; } #back-to-top { position: fixed; bottom: 20px; right: 20px; visibility: hidden; opacity: 0; transition: 0.24s; background: #0bf; user-select: none; /* prevent "button" text highlight */ } #back-to-top.is-visible { visibility: visible; opacity: 1; } 
 <a href="#top" id="back-to-top">To top</a> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> 

Additionally you should better use a <button type="button" id="back-to-top">To top</button> instead of a draggable anchor. 另外,您最好使用<button type="button" id="back-to-top">To top</button>而不是使用可拖动的锚点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM