$(document).ready(function(){ Splitting(); topScroll(); $(".top_btn, .back_btn").css("display", "block"); // 항상 표시 $("#bg,.close_btn").click(function(){ $(".popup,#bg").hide(); }); }); function topScroll(){ $(".top_btn").click(function(){ $("html, body").animate({scrollTop: 0}, 300); }); $(".back_btn").click(function(){ window.history.back(); }); $(".scroll_btn").click(function(){ const $mainTop = $(".sub_all").offset().top - 40; $("html, body").animate({scrollTop: $mainTop}, 300); }); $(window).on('scroll', function(){ checkOffset(); // 항상 offset만 체크 }); checkOffset(); // 초기에도 실행 } function checkOffset() { const pageHeight = $(document).height(); const winHeight = $(window).height(); const footerHeight = $('footer').outerHeight() || 0; const scrollTop = $(window).scrollTop(); const maxScroll = pageHeight - winHeight; const nearFooter = scrollTop > (maxScroll - footerHeight); if (nearFooter) { // 푸터에 닿을 때는 bottom 위치를 footer 위에 부드럽게 조정 const offset = 20; $(".top_btn").css({ position: "absolute", bottom: footerHeight + offset + "px" }); $(".back_btn").css({ position: "absolute", bottom: footerHeight + offset + 60 + "px" }); } else { // 평소에는 fixed로 고정 $(".top_btn").css({ position: "fixed", bottom: "40px" }); $(".back_btn").css({ position: "fixed", bottom: "100px" }); } }