如何滚动到div内的元素?

Amr*_*rhy 134 html javascript scroll

我有一个滚动div,我希望有一个链接,当我点击它将强制它div滚动查看里面的元素.我写了这样的JavasSript:

document.getElementById(chr).scrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)

但这会在滚动div自己的同时滚动所有页面.如何解决?

我想这样说

MyContainerDiv.getElementById(chr).scrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)

小智 299

您需要获取要滚动到视图中的元素的顶部偏移量,相对于其父元素(滚动div容器):

var myElement = document.getElementById('element_within_div');
var topPos = myElement.offsetTop;
Run Code Online (Sandbox Code Playgroud)

变量topPos现在设置为滚动div的顶部与您希望可见的元素之间的距离(以像素为单位).

现在我们告诉div使用scrollTop以下方式滚动到该位置:

document.getElementById('scrolling_div').scrollTop = topPos;
Run Code Online (Sandbox Code Playgroud)

如果您正在使用原型JS框架,那么您将执行以下相同的操作:

var posArray = $('element_within_div').positionedOffset();
$('scrolling_div').scrollTop = posArray[1];
Run Code Online (Sandbox Code Playgroud)

同样,这将滚动div,以便您希望看到的元素正好位于顶部(或者如果不可能,则尽可能向下滚动以使其可见).

  • 记得用css设置滚动父级:`position:relative`否则你将花费大量时间进行调试,就像我刚才那样. (18认同)
  • 这真的很有用!如果要多次设置滚动,则需要按当前滚动位置进行偏移.这是我在jQuery中的表现:$('#scrolling_div').scrollTop($('#scrolling_div').scrollTop()+ $('#element_within_div').position().top); (7认同)
  • 请注意,请求`myElement.offsetTop`将触发重排(布局颠簸),这可能是[性能瓶颈](https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-和布局颠簸?HL = EN#避免布局,颠簸) (2认同)
  • 我必须将父元素(`scrolling_div`)的`overflow-y`属性设置为`scroll`,否则它不起作用。“ overflow”属性的默认css值为“ auto”,即使它也使手动滚动成为可能,但js代码将不起作用(即使使用“ {psition:relative}”也是如此)。 (2认同)
  • 在 2017 年仍然有效。附加信息:.offsetTop 可能返回 0。然后您应该引用父元素并重试。我对标签 `h4` 然后是 `div` 然后是 `article` 标签做了这个,只有 `article` 对我有用。 (2认同)

Gle*_*lar 64

您必须在要滚动到的DIV中找到元素的位置,并设置scrollTop属性.

divElem.scrollTop = 0;
Run Code Online (Sandbox Code Playgroud)

更新:

向上或向下移动的示例代码

  function move_up() {
    document.getElementById('divElem').scrollTop += 10;
  }

  function move_down() {
    document.getElementById('divElem').scrollTop -= 10;
  }
Run Code Online (Sandbox Code Playgroud)

  • 我想滚动它的视图,看它没有滚动一定的值 (3认同)

vsy*_*ync 30

方法1 - 平滑滚动到元素内的元素

var box = document.querySelector('.box'),
    targetElm = document.querySelector('.boxChild'); // <-- Scroll to here within ".box"

document.querySelector('button').addEventListener('click', function(){
   scrollToElm( box, targetElm , 600 );   
});


/////////////

function scrollToElm(container, elm, duration){
  var pos = getRelativePos(elm);
  scrollTo( container, pos.top , 2);  // duration in seconds
}

function getRelativePos(elm){
  var pPos = elm.parentNode.getBoundingClientRect(), // parent pos
      cPos = elm.getBoundingClientRect(), // target pos
      pos = {};

  pos.top    = cPos.top    - pPos.top + elm.parentNode.scrollTop,
  pos.right  = cPos.right  - pPos.right,
  pos.bottom = cPos.bottom - pPos.bottom,
  pos.left   = cPos.left   - pPos.left;

  return pos;
}
    
function scrollTo(element, to, duration, onDone) {
    var start = element.scrollTop,
        change = to - start,
        startTime = performance.now(),
        val, now, elapsed, t;

    function animateScroll(){
        now = performance.now();
        elapsed = (now - startTime)/1000;
        t = (elapsed/duration);

        element.scrollTop = start + change * easeInOutQuad(t);

        if( t < 1 )
            window.requestAnimationFrame(animateScroll);
        else
            onDone && onDone();
    };

    animateScroll();
}

function easeInOutQuad(t){ return t<.5 ? 2*t*t : -1+(4-2*t)*t };
Run Code Online (Sandbox Code Playgroud)
.box{ width:80%; border:2px dashed; height:180px; overflow:auto; }
.boxChild{ 
  margin:600px 0 300px; 
  width: 40px;
  height:40px;
  background:green;
}
Run Code Online (Sandbox Code Playgroud)
<button>Scroll to element</button>
<div class='box'>
  <div class='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

方法2 - 使用Element.scrollIntoView:

请注意,浏览器支持不适合这个

var targetElm = document.querySelector('.boxChild'),  // reference to scroll target
    button = document.querySelector('button');        // button that triggers the scroll
  
// bind "click" event to a button 
button.addEventListener('click', function(){
   targetElm.scrollIntoView()
})
Run Code Online (Sandbox Code Playgroud)
.box {
  width: 80%;
  border: 2px dashed;
  height: 180px;
  overflow: auto;
  scroll-behavior: smooth; /* <-- for smooth scroll */
}

.boxChild {
  margin: 600px 0 300px;
  width: 40px;
  height: 40px;
  background: green;
}
Run Code Online (Sandbox Code Playgroud)
<button>Scroll to element</button>
<div class='box'>
  <div class='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

方法3 - 使用CSS 滚动行为:

.box {
  width: 80%;
  border: 2px dashed;
  height: 180px;
  overflow-y: scroll;
  scroll-behavior: smooth; /* <--- */
}

#boxChild {
  margin: 600px 0 300px;
  width: 40px;
  height: 40px;
  background: green;
}
Run Code Online (Sandbox Code Playgroud)
<a href='#boxChild'>Scroll to element</a>
<div class='box'>
  <div id='boxChild'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @DoMiNeLa10 - 这是一个假设。OP 可能提供了一个任意的例子来说明他/她的问题。此外,OP 不是主要关注点,而是来自搜索引擎寻找健康解决方案的人们,并且很可能专门回答 OP 需求不会对他们有帮助,该网站的目的是创建**可靠的**答案可以帮助尽可能多的人。*我的答案提供了两者*。 (3认同)
  • @sheats - 它在我以大字体放置的 MDN 文档链接中完全说明了这一点。即使它不适用于那些浏览器,也不意味着您不应该使用它。没有“规则”,所有浏览器上的所有行为都必须相同。如果现代浏览器可以变魔术,那就让它们变魔术吧。 (2认同)

Nik*_*hak 18

原生 JS、跨浏览器、平滑滚动(2020 年更新)

设置ScrollTop确实给出了所需的结果,但滚动非常突然。使用jquery平滑滚动不是一种选择。所以这里有一种支持所有主要浏览器的本地方式来完成工作。参考 - caniuse

// get the "Div" inside which you wish to scroll (i.e. the container element)
const El = document.getElementById('xyz');

// Lets say you wish to scroll by 100px, 
El.scrollTo({top: 100, behavior: 'smooth'});

// If you wish to scroll until the end of the container
El.scrollTo({top: El.scrollHeight, behavior: 'smooth'});
Run Code Online (Sandbox Code Playgroud)

就是这样!


这是一个值得怀疑的工作片段 -

// get the "Div" inside which you wish to scroll (i.e. the container element)
const El = document.getElementById('xyz');

// Lets say you wish to scroll by 100px, 
El.scrollTo({top: 100, behavior: 'smooth'});

// If you wish to scroll until the end of the container
El.scrollTo({top: El.scrollHeight, behavior: 'smooth'});
Run Code Online (Sandbox Code Playgroud)
document.getElementById('btn').addEventListener('click', e => {
  e.preventDefault();
  // smooth scroll
  document.getElementById('container').scrollTo({top: 175, behavior: 'smooth'});
});
Run Code Online (Sandbox Code Playgroud)
/* just some styling for you to ignore */
.scrollContainer {
  overflow-y: auto;
  max-height: 100px;
  position: relative;
  border: 1px solid red;
  width: 120px;
}

body {
  padding: 10px;
}

.box {
  margin: 5px;
  background-color: yellow;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#goose {
  background-color: lime;
}
Run Code Online (Sandbox Code Playgroud)

更新:正如您在评论Element.scrollTo()中所见,IE11似乎不支持。所以如果你不关心 IE11(你真的不应该),请随意在你的所有项目中使用它。请注意,存在对 Edge 的支持!所以你并没有真正把你的 Edge/Windows 用户抛在后面;)

参考


spi*_*ero 12

我们可以在不使用 JQuery 和其他库的情况下解决这个问题。

为此我编写了以下代码:

你有类似的结构->

<div class="parent">
  <div class="child-one">

  </div>
  <div class="child-two">

  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

scrollToElement() {
  var parentElement = document.querySelector('.parent');
  var childElement = document.querySelector('.child-two');

  parentElement.scrollTop = childElement.offsetTop - parentElement.offsetTop;
}
Run Code Online (Sandbox Code Playgroud)

我们可以轻松地重写此方法,将父级和子级作为参数传递


小智 10

代码应该是:

var divElem = document.getElementById('scrolling_div');
var chElem = document.getElementById('element_within_div');
var topPos = divElem.offsetTop;
divElem.scrollTop = topPos - chElem.offsetTop;
Run Code Online (Sandbox Code Playgroud)

您想要滚动子顶部位置和div顶部位置之间的差异.

使用以下方式访问子元素:

var divElem = document.getElementById('scrolling_div'); 
var numChildren = divElem.childNodes.length;
Run Code Online (Sandbox Code Playgroud)

等等....


mpe*_*pen 9

要将元素滚动到div的视图中,只有在需要时,才能使用此scrollIfNeeded函数:

function scrollIfNeeded(element, container) {
  if (element.offsetTop < container.scrollTop) {
    container.scrollTop = element.offsetTop;
  } else {
    const offsetBottom = element.offsetTop + element.offsetHeight;
    const scrollBottom = container.scrollTop + container.offsetHeight;
    if (offsetBottom > scrollBottom) {
      container.scrollTop = offsetBottom - container.offsetHeight;
    }
  }
}

document.getElementById('btn').addEventListener('click', ev => {
  ev.preventDefault();
  scrollIfNeeded(document.getElementById('goose'), document.getElementById('container'));
});
Run Code Online (Sandbox Code Playgroud)
.scrollContainer {
  overflow-y: auto;
  max-height: 100px;
  position: relative;
  border: 1px solid red;
  width: 120px;
}

body {
  padding: 10px;
}

.box {
  margin: 5px;
  background-color: yellow;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#goose {
  background-color: lime;
}
Run Code Online (Sandbox Code Playgroud)
<div id="container" class="scrollContainer">
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div id="goose" class="box">goose</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
  <div class="box">duck</div>
</div>

<button id="btn">scroll to goose</button>
Run Code Online (Sandbox Code Playgroud)

  • 这真的很有帮助。我有点挣扎,因为我错过了位置:容器上的相对位置。这很关键! (2认同)

ulo*_*lou 8

其他答案都没有解决我的问题。

我玩弄了scrollIntoView参数并设法找到了解决方案。设置inlinetostartblocktonearest可以防止父元素(或整个页面)滚动:

document.getElementById(chr).scrollIntoView({
   behavior: 'smooth',
   block: 'nearest',
   inline: 'start'
});
Run Code Online (Sandbox Code Playgroud)


dla*_*zon 6

如果使用的是jQuery,则可以使用以下内容滚动动画:

$(MyContainerDiv).animate({scrollTop: $(MyContainerDiv).scrollTop() + ($('element_within_div').offset().top - $(MyContainerDiv).offset().top)});
Run Code Online (Sandbox Code Playgroud)

动画是可选的:您还可以采用上面计算的scrollTop值,并将其直接放在容器的scrollTop属性中。


小智 5

使用 jQuery 和 animate 的另一个示例。

var container = $('#container');
var element = $('#element');

container.animate({
    scrollTop: container.scrollTop = container.scrollTop() + element.offset().top - container.offset().top
}, {
    duration: 1000,
    specialEasing: {
        width: 'linear',
        height: 'easeOutBounce'
    },
    complete: function (e) {
        console.log("animation completed");
    }
});
Run Code Online (Sandbox Code Playgroud)