LoginSignup
0
0

More than 3 years have passed since last update.

JavaScript ページのトップへスクトールさせるボタン

Posted at
script.js

const PageTopBtn = document.getElementById('js-scroll-top');
if (PageTopBtn) {
  PageTopBtn.addEventListener('click', () => {
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    });
  });
}

index.html

<div id="js-scroll-top" class="scroll-top"></div>
style.css

.scroll-top {
  display: block;
  position: fixed;
  right: 40px;
  bottom: 50px;
  width: 50px;
  height: 50px;
  border-radius: 25px;
  color: #fff;
  background-color: gray;
  z-index: 500;
  text-align: center;
  vertical-align: bottom;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.5);
  transition: 0.5s;
}
.scroll-top:hover {
  box-shadow: 0 4px 7px 0 rgba(0, 0, 0, 0.5);
  transform: translateY(-5px);
  cursor: pointer;
  opacity: 0.7;
}
.scroll-top:before {
display: block;
margin: -13px auto auto;
content: "";
position: absolute;
top: 50%;
right: 0;
left: 0;
width: 10px;
height: 10px;
z-index: 1;
border-top: solid 3px #ffffff;
border-right: solid 3px #ffffff;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
.scroll-top::after {
  display: block;
  margin: 25px auto auto;
  content: "TOP";
  font-size: 13px;
  line-height: 17px;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0