简体   繁体   English

如何仅在移动设备上禁用滚动回到顶部

[英]How to disable scroll back to top only on mobile device

I'm using this Back To Top code on my page to get the scroll progress and to get back to top.我在我的页面上使用此返回顶部代码来获取滚动进度并返回顶部。 My question is how to easily add if to this code to hide this element on mobile?我的问题是如何轻松地将 if 添加到此代码以在移动设备上隐藏此元素?

Thanks for helping out!感谢您的帮助!

(function($) {
  "use strict";

  $(document).ready(function() {
    "use strict";

    var progressPath = document.querySelector('.progress-wrap path');
    var pathLength = progressPath.getTotalLength();
    
    progressPath.style.transition = progressPath.style.WebkitTransition = 'none';
    progressPath.style.strokeDasharray = pathLength + ' ' + pathLength;
    progressPath.style.strokeDashoffset = pathLength;
    progressPath.getBoundingClientRect();
    progressPath.style.transition = progressPath.style.WebkitTransition = 'stroke-dashoffset 10ms linear';
    
    var updateProgress = function() {
      var scroll = $(window).scrollTop();
      var height = $(document).height() - $(window).height();
      var progress = pathLength - (scroll * pathLength / height);
      progressPath.style.strokeDashoffset = progress;
    }
    updateProgress();    
    $(window).scroll(updateProgress);
    
    var offset = 50;
    var duration = 550;
    
    jQuery(window).on('scroll', function() {
      if (jQuery(this).scrollTop() > offset) {
        jQuery('.progress-wrap').addClass('active-progress');
      } else {
        jQuery('.progress-wrap').removeClass('active-progress');
      }
    });
    
    jQuery('.progress-wrap').on('click', function(event) {
      event.preventDefault();
      jQuery('html, body').animate({
        scrollTop: 0
      }, duration);
      return false;
    })
  });
})(jQuery);

You have your main desktop styles in the body of the CSS file (1024px and above) and then for specific screen sizes I'm using:您的主桌面 styles 在 CSS 文件(1024px 及以上)的正文中,然后对于我正在使用的特定屏幕尺寸:

@media all and (min-width:960px) and (max-width: 1024px) {
  /* put your css styles in here */
}

@media all and (min-width:801px) and (max-width: 959px) {
  /* put your css styles in here */
}

@media all and (min-width:769px) and (max-width: 800px) {
  /* put your css styles in here */
}

@media all and (min-width:569px) and (max-width: 768px) {
  /* put your css styles in here */
}

@media all and (min-width:481px) and (max-width: 568px) {
  /* put your css styles in here */
}

@media all and (min-width:321px) and (max-width: 480px) {
  /* put your css styles in here */
}

@media all and (min-width:0px) and (max-width: 320px) {
  /* put your css styles in here */
}

If you would only like the devices bigger than 568px to show the scroll button, put the css to hide the rest in the lower media queries.如果您只希望大于 568px 的设备显示滚动按钮,请将 css 放在较低的媒体查询中以隐藏 rest。

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

相关问题 仅在移动设备上滚动到顶部 - Scroll to top only on mobile 如何在不检查设备的情况下禁用移动浏览器上的滚动? - How to disable scroll on mobile browser without checking device? 禁用仅在移动Javascript上滚动 - disable scroll only on mobile Javascript 如何仅使用 Javascript 滚动回左上角? - How to scroll back to the top left using only Javascript? 仅在滚动窗口时才可以使用“返回页首”按钮(但在移动设备上无法使用) - “Back to Top” button visibile only when the window is scroll (but with mobile doesn't work) 仅在移动设备中禁用垂直滚动fory body(不适用于具有溢出的div:auto) - Disable vertical scroll in mobile device only fory body (not also for div with overflow: auto) Squarespace:仅在移动设备上禁用视差滚动 - Squarespace: disable parallax scroll on mobile only 当移动菜单打开时,我设法禁用了身体滚动,但是当我打开移动菜单时,桌面/正文页面总是回到顶部 - I manage to disable scroll on body when menu mobile is open , but desktop / body page always going back to the top when i open menu mobile 滚动后将div固定在顶部-iOS移动设备上的问题 - fixing a div on top after scroll - issue on ios mobile device 在某些页面上禁用设备后退按钮[jquery mobile / android] - Disable device back button on SOME page [jquery mobile / android]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM