Skip to content

Commit

Permalink
Merge pull request #177 from agileware-jp/back-to-top-button
Browse files Browse the repository at this point in the history
トップへ戻るボタンの実装
  • Loading branch information
morioka-hibiki authored Sep 2, 2024
2 parents 746e853 + cb39ebb commit c3a2bbc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/js/backToTop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* スクロールでトップへ戻る
*/
let backToTopBtn = null

function backToTop(speed) {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 0) {
window.requestAnimationFrame(() => backToTop(speed));
window.scrollTo(0, scrollTop - scrollTop / speed);
}
}

export function createBackToTopBtn() {
if(!backToTopBtn) {
backToTopBtn = document.createElement('button')
backToTopBtn.classList.add('aw_backToTop')
backToTopBtn.textContent = 'トップへ戻る'
backToTopBtn.style.display = 'none'

backToTopBtn.addEventListener('click', () => backToTop(2))

document.body.appendChild(backToTopBtn)
}
}

// 一定量スクロールされた場合のみボタンを表示したい
export function toggleBackToTopBtn() {
if(!backToTopBtn) return
const SCROLL_THRESHOLD = 150

const scrollPosition = document.documentElement.scrollTop
if(scrollPosition > SCROLL_THRESHOLD) {
backToTopBtn.style.display = 'block'
} else {
backToTopBtn.style.display = 'none'
}
}
3 changes: 3 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { addDefaultTopMenStyle, initToggleTopMenu } from './topMenu'
import { addNoScrollClass, saveMainMenuScrollPosition, restoreMainMenuScrollPosition, dragScroll } from './mainMenu'
import { addDefaultSidebarStyle, initToggleSidebar } from './sidebar'
import { waitForBilling, checkTrial, copyBillingContainer } from './billing'
import { createBackToTopBtn, toggleBackToTopBtn } from './backToTop'

/* ちらつき防止のため、topMenuの初期スタイルを追加する */
addDefaultTopMenStyle()
Expand Down Expand Up @@ -59,7 +60,9 @@ window.addEventListener('load', restoreMainMenuScrollPosition);
/**
* その他一般的な処理
*/
window.addEventListener('scroll', toggleBackToTopBtn)
window.addEventListener('DOMContentLoaded', () => {
createBackToTopBtn()
hiddenTabsButtons()
addFeedbackLink()

Expand Down
7 changes: 7 additions & 0 deletions src/sass/components/buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
@apply mt-2
}

// topへ戻るボタン(LIFよりは下に配置するためのz-index指定)
.aw_backToTop {
// @extend .BTN_PRIMARY;
background-image: url(images/arrow_upward_onFill.svg);
@apply cursor-pointer inline-block overflow-hidden indent-[100%] whitespace-nowrap w-8 h-8 bg-center align-middle bg-background-dark/80 bg-no-repeat border-none p-0 rounded-md fixed right-4 bottom-4 z-[499] hover:bg-background-dark/70 shadow-md
}

// Sidebarの開閉ボタン
.aw_toggleSidebar {
@extend .BTN_TERTIARY_ICON_ONLY_OUTLINED;
Expand Down
3 changes: 3 additions & 0 deletions stylesheets/images/arrow_upward_onFill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3a2bbc

Please sign in to comment.