Skip to content

Commit ed29e37

Browse files
committed
www(доработка имеющегося функционала): fix autoscroll to anchor
fix autoscroll to anchor
1 parent 52b72d1 commit ed29e37

File tree

5 files changed

+62
-25
lines changed

5 files changed

+62
-25
lines changed

www/components/driver-content-nav/DriverContentNav.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ export const DriverContentNav = observer<IDriverContentNav>( ( { className } ) =
2929
}
3030

3131
// event.preventDefault();
32-
let link = event.target as HTMLLinkElement;
33-
let elementId = link.href.replace( /(.*)?#/, `` );
34-
let element = document.getElementById( decodeURIComponent( elementId ) ) as HTMLElement;
35-
let clientRect = element.getBoundingClientRect();
3632

37-
window.scrollTo( {
38-
top: window.pageYOffset + clientRect.top,
39-
left: 0
40-
} )
33+
console.log( `CHANGE_PAGE_POSITION` );
34+
// let link = event.target as HTMLLinkElement;
35+
// let elementId = link.href.replace( /(.*)?#/, `` );
36+
// let element = document.getElementById( decodeURIComponent( elementId ) ) as HTMLElement;
37+
// let clientRect = element.getBoundingClientRect();
38+
//
39+
// window.scrollTo( {
40+
// top: window.pageYOffset + clientRect.top,
41+
// left: 0
42+
// } )
4143
}
4244

4345

www/hooks/content-autoscroll-hook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {useEffect, useLayoutEffect} from "react";
22
import {useRouter} from "next/router";
3+
import { goToAnchor } from "../utils/scroll.utls";
34

45

56
export function useContentAutoscroll(){
@@ -23,6 +24,8 @@ export function useContentAutoscroll(){
2324
}
2425

2526
function startHashChangeHandler() {
27+
let hash = location.hash.slice( 1 );
28+
goToAnchor( hash );
2629
window.addEventListener("scroll", scrollHandler);
2730
}
2831

@@ -37,4 +40,4 @@ export function useContentAutoscroll(){
3740
window.removeEventListener("scroll", scrollHandler);
3841
}
3942
}, []);
40-
}
43+
}

www/hooks/use-doc-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const useDocSearch = () => {
3939

4040
url = url.replace(origin, ``);
4141

42-
router.push(url);
42+
router.push( url );
4343
}
4444
}
4545
});

www/services/ContentNavService.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {BOTTOM_CONTENT_INTERSECTION, TOP_CONTENT_INTERSECTION} from "../consts/intersection-observer";
22
import {action, computed, makeObservable, observable} from "mobx";
33
import {ContentNavItem} from "../models/ContentNav";
4-
import {Scroll} from "./Scroll";
4+
import { goToAnchor } from "../utils/scroll.utls";
55

66
type ContentNavMetadata = { index: number, elementId: string, path: string, key: string };
77
type SetId = string | null | undefined;
@@ -99,20 +99,29 @@ export class ContentNavService {
9999
private getLastMetadata = () => this.contentNavMetadataAll[this.contentNavMetadataAll.length - 1];
100100

101101
private goToAnchor = (anchor: string) => {
102-
function scrollHandler(event) {
103-
window.removeEventListener("scroll", scrollHandler);
104-
105-
window.scrollTo({
106-
left: 0,
107-
});
108-
}
109-
110-
window.addEventListener("scroll", scrollHandler);
111-
112-
let sectionId = decodeURIComponent(anchor);
113-
let element = document.getElementById<HTMLElement>(`${sectionId}`);
114-
115-
element.scrollIntoView();
102+
goToAnchor( anchor );
103+
// function scrollHandler(event) {
104+
// window.removeEventListener("scroll", scrollHandler);
105+
//
106+
// window.scrollTo({
107+
// left: 0,
108+
// });
109+
// }
110+
//
111+
// window.addEventListener("scroll", scrollHandler);
112+
//
113+
// let sectionId = decodeURIComponent(anchor);
114+
// let element = document.getElementById(`${sectionId}`);
115+
//
116+
// console.log( `SCROLL_POSITION` );
117+
// element.scrollIntoView();
118+
//
119+
// let clientRect = element.getBoundingClientRect();
120+
//
121+
// window.scrollTo( {
122+
// top: window.pageYOffset + clientRect.top,
123+
// left: 0
124+
// } )
116125
}
117126

118127
private getPrevAnchor = (currentelementId: string) => {

www/utils/scroll.utls.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function goToAnchor(anchor: string) {
2+
function scrollHandler(event) {
3+
window.removeEventListener("scroll", scrollHandler);
4+
5+
window.scrollTo({
6+
left: 0,
7+
});
8+
}
9+
10+
window.addEventListener("scroll", scrollHandler);
11+
12+
let sectionId = decodeURIComponent(anchor);
13+
let element = document.getElementById(`${sectionId}`);
14+
15+
// element.scrollIntoView();
16+
17+
let clientRect = element.getBoundingClientRect();
18+
19+
window.scrollTo( {
20+
top: window.pageYOffset + clientRect.top,
21+
left: 0
22+
} )
23+
}

0 commit comments

Comments
 (0)