简体   繁体   English

回页首滚动浏览器宽度和高度的var偏移量

[英]Back to top scroll var offset for different browser widths|heights

I implemented a "back to top" or "scroll to top" button in a WordPress site which works perfectly: 我在WordPress网站中实现了“返回顶部”或“滚动至顶部”按钮,该按钮可完美运行:

jQuery(document).ready(function($) {
  var offset = 500;
  var speed = 10;
  var duration = 250;

  $(window).scroll(function() {
    if ($(this).scrollTop() < offset) {
      $('.topbutton').fadeOut(duration);
    } else {
      $('.topbutton').fadeIn(duration);
    }
  });

  $('.topbutton').on('click', function() {
    $('html, body').animate({
      scrollTop: 0
    }, speed);
    return false;
  });
});

My dilemma is that I can't find a method to change the var offset to a flexible value according to browser width|height. 我的困境是我找不到根据浏览器的宽度将var offset更改为灵活值的方法。 Thus the narrower the browser/device screen, the further down the 500px trigger point occurs, causing the "back to top" button to appear too late. 因此,浏览器/设备屏幕越窄,触发点越往下500px ,导致“返回顶部”按钮显示得太晚。 Here is my CSS: 这是我的CSS:

.site-footer {
  background-color: #0a0a0a;
}

.topbutton {
  height: 100px;
  width: 100px;
  position: fixed;
  right: 5px;
  bottom: 5px;
  z-index: 1;
  background-image: url("https://sheknowsphotography.co.za/wp-content/uploads/2018/12/Arrow-up-blue-ezgif.com-resize.gif");
  background-repeat: no-repeat;
  display: none;
}

Here is the gif image inside the footer.php, just before the </body> tag: 这是footer.php内部的gif图片,位于</body>标记之前:

<?php wp_footer(); ?>
<a href="#" class="topbutton"><img src="https://sheknowsphotography.co.za/wp-content/uploads/2018/12/Arrow-up-blue-ezgif.com-resize.gif"></a>

Everything is done in child theme. 一切都以儿童为主题。

To whoever responds: thank you for your time! 对于任何回应:谢谢您的宝贵时间!

The solution that works: 有效的解决方案:

 jQuery(document).ready(function($){ if ($(window).width() < 960) { var offset = 500; } else { var offset = 1000; } var speed = 10; var duration = 250; $(window).scroll(function(){ if ($(this).scrollTop() < offset) { $('.topbutton') .fadeOut(duration); } else { $('.topbutton') .fadeIn(duration); } }); $('.topbutton').on('click', function(){ $('html, body').animate({scrollTop:0}, speed); return false; }); }); 

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

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