简体   繁体   English

滚动到底部按钮仅在滚动超过 200 像素时显示

[英]Scroll to bottom Button show only if scroll more than 200px

Button:按钮:

<button onclick="scrollFunctionBottom()" id="hulk"><i class="ion-chevron-down"></i></button>

CSS CSS

button#hulk {
    position: fixed;
    top: 55%;
    right: 35%;
    display: none;
}

JS: JS:

$("#hulk").click(function() {
  $("html, body").animate({ scrollTop: $(document).height() }, "slow");
});

Now to display block after 200 px, i am using this code but it does not work:现在要在 200 像素后显示块,我正在使用此代码但它不起作用:

function scrollFunction() {
  if (document.body.scrollTop > 200) {
   document.getElementById("#hulk").style.display = "block";
  }
}
  • Don't use inline on* JavaScript handlers, same as you hopefully you don't use inline style CSS.不要on* JavaScript 处理程序on*使用内联,就像您希望不要使用内联style CSS 一样。
  • Use $(window).scrollTop() to get the current scrollTop of your page使用$(window).scrollTop()获取页面的当前 scrollTop
  • Use .toggleClass() to toggle styles使用.toggleClass()切换样式

 const $hulk = $("#hulk"); const toggleHulk = () => { $hulk.toggleClass("is-active", $(window).scrollTop() > 200); }; $hulk.on("click", function() { $("html, body").animate({ scrollTop: $(document).height() }, "slow"); }); $(window).on("scroll", toggleHulk);
 body {height: 300vh; border: 4px dashed;} #hulk { position: fixed; top: 55%; right: 35%; display: none; } #hulk.is-active { display: block; }
 <button id="hulk">Scroll Down</button> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

You can use您可以使用

document.documentElement.scrollTop 

but you have to test on your target system/browser.但您必须在目标系统/浏览器上进行测试。 And/or add also your version like和/或还添加您的版本,如

if ((document.documentElement.scrollTop || document.body.scrollTop)>200) {
}

Some additiona info can be found at enter link description here可以在此处输入链接描述中找到一些附加信息

ps vote for the other question/answers if they work for you :) ps如果其他问题/答案对您有用,请投票给他们:)

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

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