亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當頁腳出現時重新定位滾動到頂部按鈕(所以它永遠不會重疊)

當頁腳出現時重新定位滾動到頂部按鈕(所以它永遠不會重疊)

喵喔喔 2022-07-21 20:54:15
我目前在我的網站上實現了一個“單擊以向上滾動”按鈕,該按鈕本身工作正常,但我唯一的問題是,當它到達頁腳時,它會與它重疊。我希望按鈕不與項目重疊,并在設定距離處停在頁腳頂部。這是我當前的按鈕 CSS:.back-to-top {position: fixed;right: 25px;display: none;z-index: 99;    }這是我的 JS:$(document).ready(function(){$(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 () {        $('body,html').animate({            scrollTop: 0        }, 400);        return false;    }); });這是在代碼筆中:https ://codepen.io/Darlton29/pen/ZEbyNXe我當然可以調整填充/邊距,使其完全避開頁腳,但這不是我想要的解決方案,就好像我要擴展頁腳一樣,我的按鈕會在頁面上太遠。如您所見,向上滾動按鈕與頁腳重疊。非常感謝任何幫助。謝謝。編輯:頁腳 CSS.footer {  position: absolute;  width: 100%;       height: 4rem;   /* Set the fixed height of the footer here */  line-height: 4rem; /* Vertically center the text there */  background-color:#292929;  text-align: right;  color: #fff;   padding-right: 2rem;  bottom: 0;      }
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

我想這就是你要找的。讓我知道!代碼筆


$(document).ready(function(){

// Should cache elements here for continuous access

const footer = $(".footer");

const scrollBtn = $("#back-to-top"); 

const padding = 25; // So you can change this is one value

  $(window).scroll(function () {

    

    // Where we're gonna set the button's height

    var distanceFromBottom = Math.floor($(document).height() - $(document).scrollTop() - $(window).height());

    // Check to see if we're within the footer range

    if ( distanceFromBottom <= footer.height() ) {

        console.log(distanceFromBottom);

        scrollBtn.css("bottom", (footer.height() - distanceFromBottom) + padding);

      } else {

        scrollBtn.css("bottom", padding);

      }

      if ($(this).scrollTop() > 50) {

        $('#back-to-top').fadeIn();

      } else {

        $('#back-to-top').fadeOut();

      }

    });

    // scroll body to 0px on click

    $('#back-to-top').click(function () {

      $('body,html').animate({

        scrollTop: 0

      }, 400);

      return false;

    });

});

html {

  position: relative;

  min-height: 100%;

}



body {

  background-color: #3498db;

  color: #ecf0f1;

}


.back-to-top {

    position: fixed;

    bottom: 25px;

    right: 25px;

    display: none;

    z-index: 99;

}


.footer {

  position: absolute;

  width: 100%;     

  height: 4rem;   /* Set the fixed height of the footer here */

  line-height: 4rem; /* Vertically center the text there */

  background-color:#292929;

  text-align: right;

  color: #fff; 

  padding-right: 2rem;

  bottom: 0;    

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">

  <div class="row justify-content-center text-center">

    <div class="col-8 my-5">

      <p class="h5">Hello!</p>

      <img src="http://via.placeholder.com/900x300" class="img-fluid rounded mb-3" alt="">

      <img src="http://via.placeholder.com/900x300" class="img-fluid rounded mb-3" alt="">

      <img src="http://via.placeholder.com/900x300" class="img-fluid rounded mb-3" alt="">

      <img src="http://via.placeholder.com/900x300" class="img-fluid rounded alt="">      

    </div>

  </div>

</div>

<a id="back-to-top" href="#" class="btn btn-light btn-lg back-to-top" role="button"><i class="fas fa-chevron-up"></i></a>

                                                                    <footer class="footer">Copyright </footer>


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 85 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號