简体   繁体   English

可滚动到视图中的 div 内的滚动元素

[英]Scroll element inside div that's scrollable into view

I have a little bit of a problem I'm a beginner programmer and writing my first web app, I came across a little bit of a problem.我有一点问题我是一名初级程序员,在编写我的第一个 web 应用程序时遇到了一点问题。

I have a parent div with a height of 300px and inside that div I have 20 child divs and every single one of them has different id attribute and one div gets a special class that changes his appearance.我有一个高度为 300px 的父 div,在那个 div 里面我有 20 个子 div,每个子 div 都有不同的 id 属性,一个 div 得到一个特殊的 class 改变他的外观。 I want that div always be seen by user so they don't have to scroll to find it.我希望该 div 始终被用户看到,这样他们就不必滚动来找到它。

I tried:我试过:

const renderStandings = (team,teamID) =>{
    let cssClass = "team-standing";
    if(team.team.id === teamID){
        cssClass = "team-standing  favouriteTeam";
    }

HTML markup HTML 标记

const markup = `
<div class="${cssClass}" data-teamID="${team.team.id}">
    <div class="team-position"><p>${team.position}</p></div>
    <div class="team-crest"><img src="src/img/logos/${team.team.id}.svg"></div>
    <div class="team-name"><p>${team.team.name}</p></div>
    <div class="team-gamesplayed"><p>${team.playedGames}</p></div>
    <div class="team-goaldiffrence"><p>${team.goalDifference}</p></div>
    <div class="team-points"><p>${team.points}</p></div>
</div>
    `;
    document.querySelector('.league-standings').insertAdjacentHTML('beforeend',markup);

Scroll into view滚动到视图

     if(document.querySelector('.favouriteTeam')){
            var el = document.querySelector('.favouriteTeam');
            el.scrollIntoView({
            behavior: 'auto',
            block: 'center',
            inline: 'center'
        });
    }
}
            
} 

That scrolls that div into the view exactly how I wanted it to, but it also scrolls the whole website to the parent div and I don't want that.这完全按照我想要的方式将该 div 滚动到视图中,但它也将整个网站滚动到父 div,我不希望这样。

QUESTION

So the question is how do I scroll that div into view without scrolling the whole website.所以问题是如何在不滚动整个网站的情况下将那个 div 滚动到视图中。 Preferably I would love to have answer in vanilla JS because that's what I'm practicing at the moment, and I don't plan to learn any frameworks until I'm comfortable with JS.最好我希望在 vanilla JS 中得到答案,因为那是我目前正在练习的,并且在我对 JS 感到满意之前,我不打算学习任何框架。

Illustrated result of what I want to achieve:我想要达到的结果的图示:

This is now and it scrolls the whole website这是现在,它滚动整个网站

This is how I want it to be [2]: https://i.stack.imgur.com/Ldg3N.jpg这就是我想要的 [2]: https://i.stack.imgur.com/Ldg3N.jpg

Inside my problem the answer was to use (scrollTop).在我的问题中,答案是使用 (scrollTop)。 Since every child div had 50px height I added atribute postition that gave me the position of element that I wanted to center (1-20), thanks to that I could calculate value of scrollTop for the element to be exacly in the center of scrollable div.由于每个子 div 的高度为 50px,我添加了属性 position,它给了我想要居中的元素的 position(1-20),因此我可以计算 scrollTop 的值,使元素恰好位于可滚动 div 的中心. Since 7 of them is already visible i started at 8th position.由于其中 7 个已经可见,我从第 8 个 position 开始。

if(document.querySelector('.favouriteTeam').getAttribute('data-teamposition')>7){
document.querySelector('.league-standings').scrollTop = 200 + ( 50 * (document.querySelector('.favouriteTeam').getAttribute('data-teamposition') - 8 ));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM