スクロール途中からリンクボタンの形状が変化

ページトップリンク
-スクロール途中からリンクボタンの形状が変化-

デモページを見る

See the Pen 8-1-7 スクロール途中からリンクボタンの形状が変化 by 動くWebデザインアイディア帳 (@ugokuweb) on CodePen.

「Result」内のリンクをクリックして動きを確かめてね!

動きを実現する仕組み

スクロール用のリンクとページトップ用のリンクの2 つのHTMLを書いておく。
リンクそのものの動きはCSS のアニメーションのループを使って表現。

jQuery で指定のid の位置までスクロールをしたら、スクロール用のHTML からクラス名を除去し、CSS のアニメーションで非表示にする。

同時に、非表示だったページトップ用のリンクにクラス名を付与し、CSS のアニメーションで表示する。


[使用するライブラリ]
* jQuery

HTMLの書き方

  1. head終了タグ直前に自作のCSSを読み込みます。
    <link rel="stylesheet" type="text/css" href="css/8-1-7.css">
    </head>
  2. body内にページトップリンクとコンテンツのHTMLを記載します。
    <header id="header">
    <h1>Logo</h1>
    </header>
    
    <main>
    <section id="area-1">
    <h2>Area 1</h2>
    <p>内容が入ります。</p>
    <!--/area1--></section>
    
    <section id="area-2"><!--area2の範囲に入ったらページトップリンクが出現-->
    <h2>Area 2</h2>
    <p>内容が入ります。</p>
    <!--/area2--></section>
    
    <section id="area-3">
    <h2>Area 3</h2>
    <p>内容が入ります。</p>
    <!--/area3--></section>
    
    <section id="area-4">
    <h2>Area 4</h2>
    <p>内容が入ります。</p>
    <!--/area4--></section>
    <!--/main--></main>
    
    <footer id="footer">
    <p class="js-scroll scroll-top scrollview"><a href="#area-2">Scroll</a></p>
    <p class="js-pagetop scroll-top"><a href="#">Page Top</a></p>
    <small>&copy; copyright.</small>
    </footer>
  3. body 終了タグ直前に jQuery、動きを制御する自作のJS の2 つを読み込みます。
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <!--自作のJS-->
    <script src="js/8-1-7.js"></script>
    </body>

自作のCSS内の書き方

/*スクロールリンクの形状*/
.scroll-top {
	/*表示位置*/
	position: fixed;
	right: 20px;
	bottom: 10px;
	z-index: 2;
	/*はじめは非表示*/
	opacity: 0;
	visibility: hidden; 
	transition: opacity .5s, visibility .5s; /*それぞれに0.5秒の変化のアニメーション*/
	/*縦書き*/
	-webkit-writing-mode: vertical-rl;
    -ms-writing-mode: tb-rl;
    writing-mode: vertical-rl;
	/*改行禁止*/
    white-space: nowrap;
	/*矢印の動き*/
	animation: arrowmove 1s ease-in-out infinite;
}

@keyframes arrowmove{
      0%{bottom:20px;}
      50%{bottom:25px;}
     100%{bottom:20px;}
 }


/*.scroll-viewクラスがついたら出現*/
.scroll-top.scroll-view {
	opacity: 1;
	visibility: visible;
}

/*リンク全体の aタグの形状*/
.scroll-top a {
	text-decoration: none;
	color: #666;
	text-transform: uppercase;
	font-size:0.9rem;
    display: block;
}

/*スクロールリンクの形状*/

.js-scroll a::after{
	content:"";
	position: absolute;
	top:0;
	right:0;
	width:1px;
	height: 50px;
	background:#666;
}

.js-scroll a::before {
    content: "";
    position: absolute;
    top: 30px;
    right: -6px;
    width: 1px;
    height: 20px;
    background: #666;
    transform: skewX(-31deg);
}

/*Edge IE11 hack*/
_:-ms-lang(x), .js-scroll a::before{
	right:-11px;
}

/*ページトップリンクの形状*/

.js-pagetop a::after{
	content:"";
	position: absolute;
	top:0;
	right:0;
	width:1px;
	height: 50px;
	background:#666;
}

.js-pagetop a::before {
    content: "";
    position: absolute;
    top: 0;
    right: -6px;
    width: 1px;
    height: 20px;
    background: #666;
    transform: skewX(31deg);
}

/*Edge IE11 hack*/
_:-ms-lang(x), .js-pagetop a::before{
	right:0;
}

/*検証のためのCSS*/
section{
padding: 500px 0;
}

自作のJS内の書き方

//スクロールした際の動きを関数でまとめる
function PageTopCheck(){
    var winScrollTop = $(this).scrollTop();
    var secondTop =  $("#area-2").offset().top - 150; //#area-2の上から150pxの位置まで来たら
    if(winScrollTop >= secondTop){
		$('.js-scroll').removeClass('scroll-view');//.js-scrollからscroll-viewというクラス名を除去
		$('.js-pagetop').addClass('scroll-view');//.js-pagetopにscroll-viewというクラス名を付与
	} else {//元に戻ったら
		$('.js-scroll').addClass('scroll-view');//.js-scrollからscroll-viewというクラス名を付与
		$('.js-pagetop').removeClass('scroll-view');//.js-pagetopからscroll-viewというクラス名を除去
	}

}

//クリックした際の動き
$('.scroll-top a').click(function () {
	var elmHash = $(this).attr('href'); //hrefの内容を取得
	if (elmHash == "#area-2") {//もし、リンク先のhref の後が#area-2の場合
		var pos = $(elmHash).offset().top;
		$('body,html').animate({scrollTop: pos}, pos); //#area-2にスクロール
	}else{
		$('body,html').animate({scrollTop: 0}, 500); //それ以外はトップへスクロール。数字が大きくなるほどゆっくりスクロール
	}
    return false;//リンク自体の無効化
});

// 画面をスクロールをしたら動かしたい場合の記述
$(window).scroll(function () {
	PageTopCheck();/* スクロールした際の動きの関数を呼ぶ*/
});

// ページが読み込まれたらすぐに動かしたい場合の記述
$(window).on('load', function () {
	PageTopCheck();/* スクロールした際の動きの関数を呼ぶ*/
});

この技術を使ったサンプルサイト

バリエーション
を見る

「機能」に関わる
動き一覧

書籍情報

紙面だからこそできるまとめ方でコードを説明し、
全体を俯瞰して調べることが出来る構成になっています。

もちろん、パーツのサンプルコードもzipでまとめてダウンロードできます。
購入をしてくださった方には特典がありますので是非チェックしてみてください!

書籍紙面サンプル

出版社:ソシム株式会社
2021/2/27 発売

書籍紙面サンプル

出版社:ソシム株式会社
2021/7/31 発売

Page
Top