<aside> 💡 末尾でjQueryを読み込むこと
</aside>
🔽HTML
<div class="pagetop"><a href=".header"></a></div>
※下の方に設置(bodyの閉じタグか、footerの閉じタグの前くらい)
🔽JavaScript(jQuery)
// トップに戻るボタン
$(document).ready(function () {
var pagetop = $(".pagetop");
$(window).scroll(function () {
if ($(this).scrollTop() > 500) {
pagetop.fadeIn();
} else {
pagetop.fadeOut();
}
});
pagetop.click(function () {
$("body, html").animate({ scrollTop: 0 }, 500);
return false;
});
});
🔽CSS
/* トップに戻るボタンの設定 -------------*/
.pagetop {
display: none;
width: 90px;
height: 90px;
position: fixed;
bottom: 30px;
right: 15px;
background-color: #962020;
height: 90px;
width: 90px;
border-radius: 50%;
opacity: 0.7;
}
.pagetop a {
position: relative;
display: block;
width: 90px;
height: 90px;
text-decoration: none;
transition: 0.3s;
border-radius: 50%;
}
.pagetop a:hover {
background-color: #fd5757;
opacity: 0.5;
border-radius: 50%;
height: 90px;
width: 90px;
}
.pagetop a::before {
font-family: "Font Awesome 5 Free";
font-weight: 600;
content: "\\f077";
font-size: 25px;
color: #fff;
position: absolute;
width: 25px;
height: 25px;
top: -60px;
bottom: 0;
right: 0;
left: 0;
margin: auto;
text-align: center;
}
.pagetop a::after {
content: "Page\\Atop";
line-height: 1.5;
white-space: pre;
font-size: 13px;
color: #fff;
position: absolute;
top: 40px;
bottom: 0;
right: 0;
left: 0;
margin: auto;
text-align: center;
}
@media screen and (max-width: 767px) {
.pagetop {
width: 50px;
height: 50px;
bottom: 10px;
right: 8px;
}
.pagetop a {
width: 50px;
height: 50px;
}
.pagetop a:hover {
height: 50px;
width: 50px;
}
.pagetop a::before {
font-size: 14px;
width: 14px;
height: 14px;
top: -40px;
}
.pagetop a::after {
line-height: 1.2;
font-size: 11px;
top: 18px;
}
}