jquery スクロールするとニュルっと出てくるこのページのトップへリンク

2022-07-17Web

後でコピペする用に。

 

html

<body id="top">
<div class="pagetop">
  <a href="#top"><i></i>TOP</a>
</div>
</body>

だいたい bodyにid="top"つけてます。

<div class="pagetop">~</div>は </body>タグ直前あたりに

SCSS

.pagetop{
	position:fixed;
	display: none;
	right:10px;
	bottom:20px;
	width:44px;
	height:44px;

	a{
		display: block;
		padding:7px 0;
		width:44px;
		height:44px;
		border-radius:50% 50%;
		background:rgba(255,255,255,.65);
		font-size:12px;
		text-align: center;
		text-decoration:none;
		line-height: 1.3;
		i{
			display:block;
			margin:0 auto;
			vertical-align: middle;
			width:0;
			height:0;
			border:4px solid transparent;
			border-bottom:4px solid #000;
			content:"";
		}
		&:hover{
			background:rgba(255,255,255,1);
			color:#f00;
			i{
				border-bottom:4px solid #f00;
			}
		}
	}
}

CSS

.pagetop {
  position: fixed;
  display: none;
  right: 10px;
  bottom: 20px;
  width: 44px;
  height: 44px; }
  .pagetop a {
    display: block;
    padding: 7px 0;
    width: 44px;
    height: 44px;
    border-radius: 50% 50%;
    background: rgba(255, 255, 255, 0.65);
    font-size: 12px;
    text-align: center;
    text-decoration: none;
    line-height: 1.3; }
    .pagetop a i {
      display: block;
      margin: 0 auto;
      vertical-align: middle;
      width: 0;
      height: 0;
      border: 4px solid transparent;
      border-bottom: 4px solid #000;
      content: ""; }
    .pagetop a:hover {
      background: white;
      color: #f00; }
      .pagetop a:hover i {
        border-bottom: 4px solid #f00; }

 

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;
    });
});

 

 

Web

Posted by ともろう