$(document).ready(function(){ $("#topBtn").hide(); $(window).on("scroll", function() { if ($(this).scrollTop() > 100) { $("#sp_links").fadeIn("fast"); } else { $("#sp_links").fadeOut("fast"); } scrollHeight = $(document).height(); //ドキュメントの高さ scrollPosition = $(window).height() + $(window).scrollTop(); //現在地 footHeight = $("footer").innerHeight(); //footerの高さ(=止めたい位置) if ( scrollHeight - scrollPosition <= footHeight ) { //ドキュメントの高さと現在地の差がfooterの高さ以下になったら $("#sp_links").css({ "position":"absolute", //pisitionをabsolute(親:wrapperからの絶対値)に変更 "bottom": footHeight + 100 //下からfooterの高さ + 20px上げた位置に配置 }); } else { //それ以外の場合は $("#sp_links").css({ "position":"fixed", //固定表示 "bottom": "0" //下から20px上げた位置に }); } }); });