Issues arise when attempting to smoothly scroll to an anchor point in a webpage

While working on my website, I have encountered a challenge.
The issue arises when dealing with multiple div items. Upon scrolling slightly, the entire page focuses on the div with a height of 100vh, which works perfectly fine. However, my attempts to implement smooth scrolling when clicking on buttons pointing to anchors using various methods like js, css, and jquery have been unsuccessful. After researching for hours, I discovered that the two lines overflow: scroll; and scroll-behavior: smooth; are incompatible with each other. Removing one line disables either the automatic focus on the full height of the div or the smooth scroll functionality of the buttons.
Do you have any ideas on how to integrate these two effects successfully?
(I hope my problem is clear; feel free to ask if you have any questions^^)

<body>
<div class="container">
  <div class="item" id="accueil">
    <nav> 
      <div class="logo" data-aos="fade-up">
        <img src="elements/illustrations/head.svg" height="100px" width="100px" alt="">
      </div>
      <ul class="nav-links" data-aos="fade-up">
        <li><a class="smoothScroll current" href="#accueil">Accueil</a></li>
        <li><a class="smoothScroll" href="#covid19">Covid-19</a></li>
        <li><a class="smoothScroll" href="#cyberharcelement">Cyberharcèlement</a></li>
        <li><a class="smoothScroll" href="#anticovid">Anti-Covid</a></li>
        <li><a class="smoothScroll" href="#galerie">Galerie Saint Germain</a></li>
      </ul>
    </nav>

    <div class="header">
      <section class="left">
        <div data-aos="fade-up" class="heading">
          <h1>Bonjour &#x1F44B; <br> je m'appelle <span id='jules'>Jules</span>, <br> </h1>
          <div class="typewriter">
            <h1>ravi de vous rencontrer</h1>
          </div>
        </div>
        <a class="button" class="smoothScroll" data-aos="zoom-in-right" href="#covid19">Découvrir</a>
      </section>

      <section class="right" data-aos="fade-left">
        <div class="illustration">
          <img src="elements/illustrations/illustration.svg" alt="">
        </div>
      </section>
    </div>

    

  </div>
  <div class="item" id="covid19">Covid-19</div>
  <div class="item" id="cyberharcelement">Cyberharcèlement</div>
  <div class="item" id="anticovid">Anti-Covid</div>
  <div class="item" id="galerie">Galerie Saint Germain</div>
</div>
html {
  scroll-behavior: smooth !important;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

body {
  font-family: var(--inter);
}

.container {
  max-height: 100vh;
  overflow: scroll;
  scroll-snap-type: y mandatory;
}

.item {
  color:black;
  /* background: #f5f5f5; */
  height: 100vh;
  scroll-snap-align: start;
}

Answer №1

Kindly review the code below. I believe it will be effective for your needs. Simply eliminate the scroll-behavior attribute from the HTML and apply overflow along with scroll-behavior to the container.

For further reference, please visit this link: https://jsfiddle.net/yudizsolutions/wk3scpyr/2/

body {
  font-family: var(--inter);
}

.container {
  max-height: 100vh;
  overflow-y: scroll;
  scroll-behavior: smooth;
}

.item {
  color: black;
  /* background: #f5f5f5; */
  height: 100vh;
  scroll-snap-align: start;
}
<body>
  <div class="container">
    <div class="item" id="accueil">
      <nav>
        <div class="logo" data-aos="fade-up">
          <img src="elements/illustrations/head.svg" height="100px" width="100px" alt="">
        </div>
        <ul class="nav-links" data-aos="fade-up">
          <li><a class="smoothScroll current" href="#accueil">Accueil</a></li>
          <li><a class="smoothScroll" href="#covid19">Covid-19</a></li>
          <li><a class="smoothScroll" href="#cyberharcelement">Cyberharcèlement</a></li>
          <li><a class="smoothScroll" href="#anticovid">Anti-Covid</a></li>
          <li><a class="smoothScroll" href="#galerie">Galerie Saint Germain</a></li>
        </ul>
      </nav>

      <div class="header">
        <section class="left">
          <div data-aos="fade-up" class="heading">
            <h1>Bonjour &#x1F44B; <br> je m'appelle <span id='jules'>Jules</span>, <br> </h1>
            <div class="typewriter">
              <h1>ravi de vous rencontrer</h1>
            </div>
          </div>
          <a class="button" class="smoothScroll" data-aos="zoom-in-right" href="#covid19">Découvrir</a>
        </section>

        <section class="right" data-aos="fade-left">
          <div class="illustration">
            <img src="elements/illustrations/illustration.svg" alt="">
          </div>
        </section>
      </div>



    </div>
    <div class="item" id="covid19">Covid-19</div>
    <div class="item" id="cyberharcelement">Cyberharcèlement</div>
    <div class="item" id="anticovid">Anti-Covid</div>
    <div class="item" id="galerie">Galerie Saint Germain</div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When testing, Redux form onSubmit returns an empty object for values

Is it possible to pass values to the onSubmit handler in order to test the append function? Currently, it always seems to be an empty object. Test Example: const store = createStore(combineReducers({ form: formReducer })); const setup = (newProps) => ...

Switching on click using jQuery

I'm having some difficulties with my code and I can't quite figure out how to solve the problem at hand. To be honest, I'm not even sure what the exact question is here. If it seems a bit confusing, I apologize - I'm still new to Jquery ...

Determine in Node.js whether a variable is present in the request object for each page the user visits

Currently, my authentication system utilizes passportjs to define req.user when the user is logged in. As my website expands beyond its current 5 pages, I have been checking for the existence of req.user at the top of each route. Based on whether it exist ...

Using React and graphql to show information on selected options across components

Currently, I'm facing a challenge in determining my next step. In productList.js, there is a straightforward select dropdown class component that displays all data from Products (id, name, brand,vintage) using graphql(apollo). It successfully sho ...

Error arising from attempting to compile React animations for a specific component, rc

As someone new to React, I decided to try running a sample example from the following link: To do this, I created a file named MonApp.js with the code snippet below: import React, { Component } from 'react'; import { render } from "react-dom"; ...

Leverage the power of AJAX for fetching data from a database in Node.js

As I am utilizing Node.js as the server and Mysql as the database to store logged in usernames, I am curious if it is possible to use AJAX to execute a SQL database query and retrieve a list of usernames. More precisely, I am interested in using XMLHttp r ...

Python Selenium encountering issue with selecting search bar

I have been attempting to use Selenium to search for courses on and scrape the results. However, all the selectors I have tried so far have failed, resulting in empty arrays when printed. I am confused as to why these selectors are failing and what is the ...

Show two <p>'s next to each other

I am trying to arrange 2 paragraphs side by side. <p class = "firstClass">This is the first paragraph.</p> <p class = "secondClass">This is the second paragraph.</p> Result: This is the first paragraph. This is the second paragra ...

Building a hierarchical tree structure using arrays and objects with Lodash Js

I am attempting to create a tree-like structure using Lodash for arrays and objects. I have two arrays, one for categories and the other for products, both with a common key. The goal is to organize them into a tree structure using string indexing. let ca ...

Unacceptable response from AJAX function

My goal is to extract specific information from this ajax function, such as the thumbnail image and the owner of the image. It seems like the function is not recognizing data.images[i].imgurl and data.images[i].username. AJAX call in ajax.php require_o ...

Utilizing Angular.js to extract data from a deeply nested array of objects in JSON

Hello, I am currently learning about Angular.js and working on developing a shopping cart. In this project, I need to display an image, name, and cost of each product for multiple tenants. Each tenant has an array called listOfBinaries which contains listO ...

What is the best way to compare dates in order to obtain the necessary results?

Question : Filter the JSON array to retrieve specific entries 1: All entries with name "Sam". 2: All entries with date "Dec 2019". // JSON Data provided below. var data = [{ "id":"27", "0":{ "name":"Sam", "date":"2021-02-28" ...

What is the reason for not allowing return statements in the ternary operator?

Imagine you have a basic form and you want to determine if the form has been modified. If it has changed, you want to submit it; otherwise, you want to prevent form submission. To tackle this, instead of using an if-else statement, I decided to go for a te ...

The Node.js Express Server runs perfectly on my own machine, but encounters issues when deployed to another server

I've encountered a strange issue. My node.js server runs perfectly fine on my local machine, but when I SSH into a digital ocean server and try to run it there, I get this error. I used flightplan to transfer the files to the new machine. deploy@myse ...

React button synchronization issue with start stop functionality

My React timer module has one input field and three buttons: Start (to begin the timer), Pause (to temporarily stop the timer), and Stop (to completely halt the timer). The issue I am encountering is that when I input a value, start the timer, then press t ...

Overlaying images on top of text

Is there a way to overlay an image on text, like a pattern image? Similar to applying color with a hex value in CSS: color: #ffffff; Any ideas on how this can be achieved? I want to have a pattern over text. https://i.sstatic.net/JaNZU.jpg Appreciate ...

I would like to modify the text color of a disabled input field

I need to adjust the font color of V1, which is a disabled input field. I want to make it darker specifically for Chrome. Any suggestions on how I can achieve this? Here's my HTML code: <mat-form-field appearance="outline" fxFlex=" ...

Setting the height of a Bootstrap radio button

My Bootstrap radio buttons are set to a width of 200px. However, when the text inside the button exceeds this width, it wraps onto two lines leading to changes in button height. How can I ensure that all other buttons displayed scale to the same height? h ...

What is the optimal number of parameters in JavaScript?

After stumbling upon a question on StackOverflow discussing the number of parameters in JavaScript functions (How many parameters are too many?), I started pondering if there is a real limitation on how many parameters a JS function can have. test(65536 ...

Using Node.js and Express to import a simple JavaScript file as a router

Can anyone help me understand how to import the user.json json file into my user.js? I want the json file to be displayed when typing /user but I'm struggling with the new version of Node. index.js import express from 'express'; import body ...