How to smoothly scroll to a specific div on click and hide the top div using Jquery

Snippet of Code:

HTML Markup:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <title>Title</title>
</head>
<body>

<div id="intro">
    <a href="#" id="showfullsite"></a>
</div>

<div id="fullsite"></div>

</body>
</html>

Using jQuery:

$("#showfullsite").click(function() {

    $('html, body').animate({
        scrollTop: $("#fullsite").offset().top
    }, 2000);

    $('html, body').css('overflow', 'auto'); 
    $('#intro').hide(2000);

});

Simple CSS Style:

body {
overflow: hidden;
}

Greetings, I am fairly new to jQuery and require assistance. I have two div containers on my website - one for the introduction and one for the full site. I want the top #intro div to be visible initially, with the full site hidden. To achieve this, I am using overflow: hidden; to hide content beyond the screen. There is a control within the #intro div that should reveal the full site when clicked, scrolling to the #full-site section. I have applied jQuery to accomplish this, including

$('html, body').css('overflow', 'auto');
to show the hidden content and $('#intro').hide(2000); to hide the intro section gradually. However, I am encountering issues where the content of #intro shifts left or right when hidden, and the scrolling effect does not directly target the desired section. How can I rectify this issue?

Answer №1

Your JavaScript code includes #fullsite, but your HTML code has

<div class="fullsite"></div>
instead of
<div id="fullsite"></div>
.

The same issue exists for intro.

Remember: use # to access IDs and . for classes when selecting elements in JavaScript.

If you want to learn more about selectors, you can visit this link: http://api.jquery.com/category/selectors/basic-css-selectors/

In this scenario, it's better to use "id" rather than "classes" because multiple elements can have the same class, but each ID should be unique. Therefore, $(".aaa") will target all elements with class aaa, while $("#aaa") will target the specific element with ID aaa.

Answer №2

Are you looking to execute the code in this manner?

$("#showfullsite").click(function() {

  $('html, body').animate({
    scrollTop: $(".fullsite").offset().top
  }, 2000);

  $('html, body').css('overflow', 'auto');
  $('.intro').hide(2000);

});
.container {
  overflow: hidden;
  height: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="wrapper">
    <div class="intro">
      <a href="#" id="showfullsite">go to fullsite</a>
    </div>

    <div class="fullsite">this is fullsite</div>
  </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

What is the best way to center a CSS arrow vertically within a table cell?

I'm having trouble with aligning a CSS arrow next to some text inside a table cell... <td><div class="arrow-up"></div> + 1492.46</td> My goal is to have the arrow positioned to the left of the text and centered vertically wit ...

Dynamic links with Node Acl

Utilizing the npm module Acl to establish an ACL system has been my current focus. You can check out the homepage of this module at: https://github.com/OptimalBits/node_acl. While the documentation provides straightforward examples for granting role acces ...

Having difficulty accessing information from Firebase database using the .once() function

When a button is clicked on the page, I need to fetch data from a Firebase database using the once() function. Despite setting up the necessary references and variables, the data retrieval seems to be unsuccessful as the global variable numElections keeps ...

PHP enables real-time control of a pop-up message box

I am currently working on a real-time project and looking for a way to send live announcements or pop-up alerts that can be controlled by admins to all users on the webpage. Although I found a solution called Realtime announcer online, our company policy ...

Creating PNG and JPEG image exports in Fabric.js with the help of Node.js

Exploring the wonders of Fabric.JS specifically designed for Node.JS, not intended for web applications, I've successfully created a Static Canvas and added a rectangle to it. Now, it's time to export the image. Take a look at my code snippet bel ...

Can a variable name be created using a function input?

It seems like my title might be a bit confusing, but oh well. I'm currently working on developing a game and I have several arrays named things like ItemsInG5Array, ItemsInB2Array. These names correspond to different nodes on the map. What I'm ai ...

"Efficiently fetch data with an Express GET request without the

My goal is to send the client the HTML page (create.html) in response to a GET request triggered by a button click using fetch. I am intentionally avoiding the use of a form due to formatting and potential scalability issues. The code acknowledges that the ...

Arrow function utilized in string rendered component

Hello everyone, I could use some help with the following code snippet: rowRenderer = (e) => { debugger; return e.data?.fileName ? '<a class="documentLink" onclick={() => \'' + console.log('\ ...

Problem with Autofill function in ASP.NET MVC 5

I have recently set up an ASP.NET MVC5 project and have included some links in order to enable an autocomplete jQuery plugin. _Layout page <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-s ...

Changing jQuery to plain JavaScript in order to show a div element more effectively

I am attempting to toggle a checkbox to display a header or div that is currently hidden. @media screen and (max-width: 639.98px){ #menuPanel{ display: none !important; } } I am encountering an unresolved method error on ready in JQuery, ...

React Routes displays single route at a time

Every time I attempt to link components with routes and input more than one route within <Routes>, React seems to disregard them and does not respond at all. I even tried using <StrictMode> to examine what is going on within the program. Here i ...

Running React Boilerplate with forever is a great way to ensure your application stays

I plan on keeping the react-boilerplate application running indefinitely on the server. I recently came across forever, but I'm uncertain about how to pass parameters to it. The command to start the server looks like this: PORT=80 npm run start:produ ...

Is it feasible to incorporate one iOS app within another iOS app through in-app purchasing or subscription?

Simply put, we are creating interactive content in Flash that we want to distribute through an iOS app either by in-app purchase or subscription. In our testing, we have found that Flash can produce standalone iOS apps that work well for our needs. Instea ...

Dividing the screen into multiple sections to display cards

I have a question about integrating product cards into a split screen layout. Despite my efforts, the cards are stacking on top of each other instead of appearing side-by-side. Does anyone have a solution or suggestion? Below is the code I have been work ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

Is it possible to trace the data flow in the console to see the destination when you submit a form on a website?

Can you track the destination of data when submitting a form on a website through the console? ...

Switching between divs using buttons

After researching examples and tutorials on HTML/CSS/jQuery, I was able to create a set of buttons that toggle content based on user interaction. However, I believe there must be a more efficient method of writing the code by using element identifiers or a ...

Adjust the width and mode of an Angular 6 sidebar in real-time

Currently, I am in the process of developing an Angular 6 app that requires a responsive sidebar. To achieve this, I decided to utilize Angular Material's sidebar module. However, I encountered a challenge: I needed the sidebar to have a collapsed mod ...

Adjust: Return scale to default

By applying transform: rotate(-2deg); to a section, I have created an effect where the section rotates slightly. Additionally, when the user hovers over the same section, it increases in size due to transform: scale(1.1);. However, on one specific page of ...

Tips for adjusting the maximum height of the column panel within the DataGrid element

I'm trying to adjust the max-height of a column panel that opens when clicking the "Columns" button in the Toolbar component used in the DataGrid. I am working with an older version of @material-ui/data-grid: "^4.0.0-alpha.8". https://i.stack.imgur.c ...