search

Home  >  Q&A  >  body text

Javascript scroll IntoView only when in viewport

I made a mockup (note that it uses spacing because it scrolls to a new line of text while the video is playing). I'm using scrollIntoView with block: 'nearest'.

The problem is that since the entire page has scrolling, it still scrolls the page. I just want it to scroll to the active text line in the lines div (it doesn't have to be in view). Because if I'm looking at the "video" div, it continues to scroll down (which is unwanted behavior).

var text = document.querySelector('.text')


setInterval(function() {
  text.scrollIntoView({
    behavior: 'smooth',
    block: 'nearest'
  });

}, 3000)
.a {
  height: 200px;
}

.elem {
  position: relative;
}

.video {
  width: 200px;
  height: 400px;
  background: #ccc;
  margin-bottom: 100px;
}

.lines {
  overflow-y: auto;
  height:120px;
}
<div class="a"></div>


<div class="elem">
  <div class="video"></div>


  <div class="lines">
    <div>lorem ipsum dolir sit amet</div>
    <div>Vestibulum nulla justo</div>
    <div>Fusce egestas, est ut fringilla facilisis</div>
    <div>Maecenas eu erat condimentum </div>
    <div>Quisque risus</div>
    <div>fames ac turpis egestas</div>
    <div>lorem ipsum dolir sit amet</div>
    <div>Vestibulum nulla justo</div>
    <div class="text">Fusce egestas, est ut fringilla facilisis</div>
    <div>Maecenas eu erat condimentum </div>
    <div>Quisque risus</div>
    <div>fames ac turpis egestas</div>

  </div>


</div>

<div class="a"></div>

P粉011684326P粉011684326447 days ago490

reply all(1)I'll reply

  • P粉691958181

    P粉6919581812023-09-12 11:30:18

    As you've discovered, scrollIntoView() is not suitable for what you are trying to do here - it aggressively scrolls multiple view panes in an attempt to get your element into view. < /p>

    A more appropriate one would be element.scrollTo(..) on the parent element (in this case .lines). You'll need to do some high-level arithmetic to center the element in the viewport, but it's relatively simple:

    var text = document.querySelector('.text')
    
    setInterval(function() {
      const parent       = text.parentElement;
      const parentHeight = parent.clientHeight;
      
      const textTop      = text.offsetTop - parent.offsetTop;
      const textMiddle   = textTop + text.offsetHeight / 2;
    
      parent.scrollTo({ top: textMiddle - parentHeight / 2, behavior: "smooth" });
    }, 3000)
    .a {
      height: 200px;
    }
    
    .elem {
      position: relative;
    }
    
    .video {
      width: 200px;
      height: 400px;
      background: #ccc;
      margin-bottom: 100px;
    }
    
    .lines {
      overflow-y: auto;
      height:120px;
    }
    
    .text {
      background: yellow;
    }
    <div class="a"></div>
    
    
    <div class="elem">
      <div class="video"></div>
    
    
      <div class="lines">
        <div>lorem ipsum dolir sit amet</div>
        <div>Vestibulum nulla justo</div>
        <div>Fusce egestas, est ut fringilla facilisis</div>
        <div>Maecenas eu erat condimentum </div>
        <div>Quisque risus</div>
        <div>fames ac turpis egestas</div>
        <div>lorem ipsum dolir sit amet</div>
        <div>Vestibulum nulla justo</div>
        <div class="text">Fusce egestas, est ut fringilla facilisis</div>
        <div>Maecenas eu erat condimentum </div>
        <div>Quisque risus</div>
        <div>fames ac turpis egestas</div>
    
      </div>
    
    
    </div>
    
    <div class="a"></div>

    reply
    0
  • Cancelreply