简体   繁体   English

滚动按钮使人向下或向上

[英]Scroll buttons which bring person to down or up

I wanted to make two buttons, one was to move up the pages and show up when it exceeds 300px and the other was to be shown immediately and move the person who clicks to the bottom我想做两个按钮,一个是页面上移,超过300px显示,一个是立即显示,把点击的人移到底部

I will add that I am new in programming我要补充一点,我是编程新手

I made one button that takes a person down the page with Javascript and when I added the second it only displayed the last button HTML我用 Javascript 制作了一个按钮,可以将一个人带到页面下方,当我添加第二个按钮时,它只显示最后一个按钮 HTML

div id="TotopButton">^<span id="test"></span></div>
        <div id="ToDownButton">^<span id="test2"></span></div>
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

CSS CSS

#TotopButton
{
    background-color: red;
    color: black;
    font-size: 35px;
    padding:10px;
    position:fixed;
    bottom:15px;
    right:15px;
    height: 50px;
    width: 150px;
    border:1px solid black;
    text-align: center;
    display:none;
}
#TotopButton:hover
{
    color:white;
    cursor:pointer;
   
}
#ToDownButton
{
    Background-color: blue;
    color: black;
    font-size:35px;
    padding:10px;
    position:fixed;
    bottom:15px;
    left:15px;
    height:50px;
    width:150px;
    border: 1px solid black;
    text-align: center;
    display: none;
}
#ToDownButton:hover
{
    color:white;
    cursor:pointer;
   
}

Javascript Javascript

  window.onload = function()
   {
     var TotopButton = document.getElementById("TotopButton");
     var test = document.getElementById("test");
     window.onscroll = function ()
     {
        var TotopButton = document.getElementById("TotopButton");
         var yScrollAxis = window.pageYOffset;

        var test = document.getElementById("test");
        if (yScrollAxis > 300)
        {
            TotopButton.style.display = 'block'
        }
        else
        {
            TotopButton.style.display = 'none'
        }
        test.innerHTML = " "  + window.pageYOffset
     }
     TotopButton.onclick = function()
     {
         window.scrollBy(0, -1 * window.pageYOffset);
     }
   };
   //Secon button
   window.onload = function()
   {
     var ToDownButton = document.getElementById("ToDownButton");
     var test2 = document.getElementById("test2");
     window.onscroll = function()
     {
        var ToDownButton = document.getElementById("ToDownButton");
         var yScrollAxis = window.pageYOffset;

        var test2 = document.getElementById("test2");
        if (yScrollAxis > 50)
        {
            ToDownButton.style.display = 'block'
        }
        else
        {
            ToDownButton.style.display = 'none'
        }
        test2.innerHTML = " "  + window.pageYOffset
     }
     ToDownButton.onclick = function()
     {
         window.scrollBy(0, 1000 * window.pageYOffset);
     }
   };

I use a similar button, and this is my setup for the top button you described:我使用类似的按钮,这是我对您描述的顶部按钮的设置:

HTML: HTML:

[code]
<section class="floating-button">
    <div class="btn-wrapper">
        <a class="primary-btn" id="floating-btn" href="#bottom-form">Apply Now</a>
    </div>
</section>
[more code]
<section class="final-form" id="bottom-form">
    [form here]
</section>

jQuery jQuery

$(document).scroll(function() {
    var y = $(this).scrollTop();
    if ((y > 490) && (y < 5698)) {
        $('#floating-btn').css('visibility','visible').fadeIn();
    } else {
        $('#floating-btn').fadeOut();
    }
});

For the above script, adjust 490 to where you want the button to fade in;对于上述脚本,将 490 调整为您希望按钮淡入的位置; you can check the right place adding console.log(y) ;您可以检查添加console.log(y)的正确位置; you may also want to remove the y < 5698 if you don't want to fade out the button at the bottom of the page如果您不想淡出页面底部的按钮,您可能还想删除y < 5698

SCSS SCSS

.floating-button {
    z-index: 1;
    position: fixed;
    bottom: 34px;
    width: 100%;
}

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

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