Skip to content

Commit

Permalink
Fix smooth scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnlog committed Jul 10, 2024
1 parent 2e3e221 commit d5554ac
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
86 changes: 84 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,91 @@
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="https://rawcdn.githack.com/hasnainalsyed/smooth-scroll-js/74ffa6c38d56463ab259d267a2492a67b3e807a1/smooth-scroll.js"></script>
<script>
function init() {
new SmoothScroll(document, 120, 12);
}

function SmoothScroll(target, speed, smooth) {
if (target === document)
target = (document.scrollingElement
|| document.documentElement
|| document.body.parentNode
|| document.body); // cross browser support for document scrolling

var moving = false;
var pos = target.scrollTop;
var frame = target === document.body
&& document.documentElement
? document.documentElement
: target; // safari is the new IE

target.addEventListener("mousewheel", scrolled, { passive: false });
target.addEventListener("DOMMouseScroll", scrolled, { passive: false });

function scrolled(e) {
// console.log(e);
e.preventDefault(); // disable default scrolling

var delta = normalizeWheelDelta(e);

if (pos === -1) {
pos = target.scrollTop;
}
pos += -delta * speed;
pos = Math.max(0, Math.min(pos, target.scrollHeight - frame.clientHeight)); // limit scrolling

if (!moving) update();
}

function normalizeWheelDelta(e) {
if (e.detail) {
if (e.wheelDelta)
return e.wheelDelta / e.detail / 40 * (e.detail > 0 ? 1 : -1); // Opera
else
return -e.detail / 3; // Firefox
} else
return e.wheelDelta / 120; // IE,Safari,Chrome
}

function update() {
if (pos === -1) {
requestFrame(update);
return;
}

moving = true;

var delta = Math.ceil((pos - target.scrollTop) / smooth);

target.scrollTop += delta;
// console.log(pos, target.scrollTop, delta);

if (Math.abs(pos - target.scrollTop) >= 1)
requestFrame(update);
else {
target.scrollTop = pos;
moving = false;
pos = -1;
}
}

var requestFrame = function() { // requestAnimationFrame cross browser
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(func) {
window.setTimeout(func, 1000 / 50);
}
);
}();
}
</script>
</head>
<body>
<body onload="init()">
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
Please enable it to continue.</strong>
Expand Down
1 change: 0 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<script lang="ts" setup>
import IntroComponent from "@/components/IntroComponent.vue";
import { onMounted, ref } from "vue";
import LoadingComponent from "@/components/LoadingComponent.vue";
let isLoaded = ref(false);
Expand Down
6 changes: 6 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const routes: Array<RouteRecordRaw> = [
const router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior: (to, from, savedPosition) => {
if (savedPosition) return savedPosition;
return {
top: 0,
};
},
});

export default router;
2 changes: 2 additions & 0 deletions src/views/NotFoundView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default defineComponent({
a {
cursor: pointer;
font-size: 1.5rem;
margin-top: 1rem;
}
.mdi {
Expand Down

0 comments on commit d5554ac

Please sign in to comment.