gpt4 book ai didi

javascript - scrollIntoView 整个页面向下移动

转载 作者:行者123 更新时间:2023-12-04 09:45:05 27 4
gpt4 key购买 nike

我想建立一个索引字母。当我单击字母(例如 A、B、C)时。它应该滚动到所属的标题。我在这里使用了 scrollIntoView 。除了“block:start”,一切都很好。当我想滚动到开始的标题时,整个页面随容器一起滚动。

这是我关于问题的动图

enter image description here

这是我的 html & css & javascript 代码。

const scrollTo = id => {
let ref = document.getElementById(id)
if (ref /* + other conditions */) {
ref.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "start",
})
}

<div className="glossary">
<div className="glossary-items grid">
<div className="d-flex ">
<div className="glossary-letters ">
<ul>
{letter.map(item => (
<li onClick={() => scrollTo(item)}>
<a> {item}</a>
</li>
))}
</ul>
</div>
<div className="glossary-titles">
<ul>
{glossary.map((item, index) => (
<div key={item.group}>
<li id={item.group}>{item.group}</li>
{item.children.map((item2, i) => (
<li key={i}>
<Link
activeClassName="glossary-titles-active"
to={`/en/sozluk/${item2.first_name + item2.id}`}
>
{item2.first_name}
</Link>
</li>
))}
</div>
))}
</ul>
</div>
</div>
<div className="glossary-content ml-5">
<Router>
<Glossary path="en/sozluk/:id" />
</Router>
</div>
</div>
</div>

最佳答案

There is related question to that

您可以使用父元素的scrollTo 函数来滚动到特定的子元素。

示例

const { useState, useEffect, useRef } = React;
const App = () => {
const [list, setList] = useState([]);
const [keywords, setKeywords] = useState([]);
const refs = useRef(null);
const parentRef = useRef(null);

useEffect(() => {
const newList = Array(25).fill(0).map((pr, index) => String.fromCharCode(65 + index));
const newKeywords = newList.flatMap(pr => [pr, ...newList.map(npr => pr + npr)]);

refs.current = newList.reduce((acc, letter) => {
acc[letter] = React.createRef();
return acc;
}, {});

setList(newList);
setKeywords(newKeywords);
}, [])

const onClick = ({target: {dataset: {letter}}}) => {
const element = refs.current[letter].current;
const parent = parentRef.current;

const onScroll = () => {
const relativeTop = window.scrollY > parent.offsetTop ? window.scrollY : parent.offsetTop

parent.scrollTo({
behavior: "smooth",
top: element.offsetTop - relativeTop
});
}

window.removeEventListener("scroll", onScroll);
window.addEventListener("scroll", onScroll);

onScroll();

/*
element.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "start",
})
*/
}

const tryAssignRef = (letter) => {
return list.indexOf(letter) > -1 ? {ref: refs.current[letter]} : {};
}

/// ...{ref: {refs.current['A']}}
return <div className="container">
<div className="header">
</div>
<div className="body">
<div className="left">
<div className="letters">{list.map(pr => <div className="letter" onClick={onClick} data-letter={pr} key={pr}>{pr}</div>)}</div>
<div ref={parentRef} className="keywords">{keywords.map(pr => <div {...tryAssignRef(pr)} key={pr}>{pr}</div>)}</div>
</div>
<div className="right">
</div>
</div>
</div>
}

ReactDOM.render(
<App />,
document.getElementById('root')
);
.container, .body, .left {
display: flex;
}

.container {
flex-direction: column;
}

.header {
flex: 0 0 100px;
border: 1px solid black;
}

.body {
height: 1000px;
}

.left {
flex: 1;
}

.right {
flex: 2;
}

.left {
justify-content: space-between;
}

.letter {
padding: 2px 0;
cursor: pointer;
}

.letters, .keywords {
padding: 0 10px;
}

.keywords {
overflow-y: scroll;
}
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<div id="root"></div>

关于javascript - scrollIntoView 整个页面向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62169922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com