Div scrolled to the top of the page when anchor link inside an horizontal-scroll element is clicked

62 views Asked by At

I am playing with creating an horizontal slider with pure CSS for some reason clicking on the anchor link to scroll the elements on the x axis also scroll the entire page on the y axis, placing the slider at the top of the page

Can this behavior be avoided (as you can see here https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior it work as it should)

see GIF below and the code I wrote enter image description here

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html {
      scroll-timeline: --progress-bar
    }
    
    body {
      height: 300vh;
      margin: 0;
    }
    
    .progress-bar {
      position: fixed;
      z-index: 10;
      left: 0;
      top: 0;
      width: 100%;
      height: 0.25em;
      transform-origin: 0 50%;
      background: red;
      animation: grow-progress;
      animation-duration: auto;
      animation-timing-function: linear;
      animation-timeline: --progress-bar;
      transform: scaleX(0);
    }
    
    .gallery-wrap {
      width: 100%;
      height: 600px;
      background: pink;
      padding: 100px 20%;
      box-sizing: border-box;
    }
    
    .gallery-frame {
      width: 100%;
      height: 100%;
      position: relative;
    }
    
    .gallery {
      width: 100%;
      height: 100%;
      display: flex;
      overflow-x: scroll;
      scroll-snap-type: x mandatory;
      scroll-timeline: --progress-bar inline;
      align-items: center;
      scroll-behavior: smooth;
      scrollbar-width: none
    }
    
    .gallery::-webkit-scrollbar {
      display: none;
    }
    
    .index-bar {
      height: 6px;
      width: 100%;
      background: red;
      position: absolute;
      top: 0;
      left: 0;
      animation: grow-progress;
      animation-duration: auto;
      animation-timing-function: linear;
      animation-timeline: --progress-bar;
      transform-origin: left;
      transform: scaleX(calc(1 / 3));
      z-index: 1;
    }
    
    .nav {
      width: 100%;
      background: #ffffff69;
      position: absolute;
      display: flex;
      justify-content: space-between;
      padding: 16px;
      box-sizing: border-box;
      z-index: 1;
    }
    
    .nav a {
      width: 36px;
      display: block;
      aspect-ratio: 1 / 1;
      background: black;
      border-radius: 100%;
    }
    
    .slide {
      width: 100%;
      flex-shrink: 0;
      scroll-snap-align: center;
      align-self: stretch;
      position: relative;
      display: flex;
      align-items: center;
    }
    
    .slide .nav {
      opacity: 0;
    }
    
    .screen1 {
      background: #fff0ce;
    }
    
    .screen2 {
      background: #e4f1fd;
    }
    
    .screen3 {
      background: #ccfff5;
    }
    
    @keyframes grow-progress {
      to {
        transform: scaleX(1);
      }
    }
  </style>
</head>

<body>

  <div class="progress-bar"></div>
  <div class="gallery-wrap">
    <div class="gallery-frame">
      <div class="gallery">
        <div class="index-bar"></div>
        <div class="nav" style="pointer-evet:none">
          <a href="#slide1"></a>
          <a href="#slide2"></a>
        </div>
        <div id="slide1" class="slide screen1">
          <div class="nav">
            <a href="#slide3"></a>
            <a href="#slide2"></a>
          </div>
        </div>
        <div id="slide2" class="slide screen2">
          <div class="nav">
            <a href="#slide1"></a>
            <a href="#slide3"></a>
          </div>
        </div>
        <div id="slide3" class="slide screen3">
          <div class="nav">
            <a href="#slide2"></a>
            <a href="#slide1"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

1

There are 1 answers

4
Yash Borda On

 const scrollLinks = document.querySelectorAll('.scroll-link');

    scrollLinks.forEach(link => {
      link.addEventListener('click', (event) => {
        event.preventDefault(); // Prevent the default behavior of anchor links
        const targetId = link.getAttribute('data-target');
        const targetElement = document.getElementById(targetId);
        if (targetElement) {
          targetElement.scrollIntoView({ behavior: 'smooth' });
        }
      });
    });
    .nav a {
      width: 36px;
      display: block;
      aspect-ratio: 1 / 1;
      background: black;
      border-radius: 100%;
      cursor: pointer; /* Add cursor pointer to make it clear it's clickable */
    }

 html {
      scroll-timeline: --progress-bar
    }
    
    body {
      height: 300vh;
      margin: 0;
    }
    
    .progress-bar {
      position: fixed;
      z-index: 10;
      left: 0;
      top: 0;
      width: 100%;
      height: 0.25em;
      transform-origin: 0 50%;
      background: red;
      animation: grow-progress;
      animation-duration: auto;
      animation-timing-function: linear;
      animation-timeline: --progress-bar;
      transform: scaleX(0);
    }
    
    .gallery-wrap {
      width: 100%;
      height: 600px;
      background: pink;
      padding: 100px 20%;
      box-sizing: border-box;
    }
    
    .gallery-frame {
      width: 100%;
      height: 100%;
      position: relative;
    }
    
    .gallery {
      width: 100%;
      height: 100%;
      display: flex;
      overflow-x: scroll;
      scroll-snap-type: x mandatory;
      scroll-timeline: --progress-bar inline;
      align-items: center;
      scroll-behavior: smooth;
      scrollbar-width: none
    }
    
    .gallery::-webkit-scrollbar {
      display: none;
    }
    
    .index-bar {
      height: 6px;
      width: 100%;
      background: red;
      position: absolute;
      top: 0;
      left: 0;
      animation: grow-progress;
      animation-duration: auto;
      animation-timing-function: linear;
      animation-timeline: --progress-bar;
      transform-origin: left;
      transform: scaleX(calc(1 / 3));
      z-index: 1;
    }
    
    .nav {
      width: 100%;
      background: #ffffff69;
      position: absolute;
      display: flex;
      justify-content: space-between;
      padding: 16px;
      box-sizing: border-box;
      z-index: 1;
    }
    
    .nav a {
      width: 36px;
      display: block;
      aspect-ratio: 1 / 1;
      background: black;
      border-radius: 100%;
    }
    
    .slide {
      width: 100%;
      flex-shrink: 0;
      scroll-snap-align: center;
      align-self: stretch;
      position: relative;
      display: flex;
      align-items: center;
    }
    
    .slide .nav {
      opacity: 0;
    }
    
    .screen1 {
      background: #fff0ce;
    }
    
    .screen2 {
      background: #e4f1fd;
    }
    
    .screen3 {
      background: #ccfff5;
    }
    
    @keyframes grow-progress {
      to {
        transform: scaleX(1);
      }
    }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="progress-bar"></div>
  <div class="gallery-wrap">
    <div class="gallery-frame">
      <div class="gallery">
        <div class="index-bar"></div>
        <div class="nav" style="pointer-events: none">
          <a class="scroll-link" data-target="slide1"></a>
          <a class="scroll-link" data-target="slide2"></a>
        </div>
        <div id="slide1" class="slide screen1">
          <div class="nav">
            <a class="scroll-link" data-target="slide3"></a>
            <a class="scroll-link" data-target="slide2"></a>
          </div>
        </div>
        <div id="slide2" class="slide screen2">
          <div class="nav">
            <a class="scroll-link" data-target="slide1"></a>
            <a class="scroll-link" data-target="slide3"></a>
          </div>
        </div>
        <div id="slide3" class="slide screen3">
          <div class="nav">
            <a class="scroll-link" data-target="slide2"></a>
            <a class="scroll-link" data-target="slide1"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

In this modified code, I've added a data-target attribute to the anchor links and given them a class of scroll-link. The JavaScript code intercepts the click event on these links, prevents the default behavior, and scrolls to the target element with a smooth animation. This way, clicking the links won't scroll the entire page on the y-axis.