Is there a way to create a multi-page website simulation using jQuery?

My current project involves creating a single page website, but I am looking to give the illusion of a multi-page site by using CSS/jQuery to hide and reveal different sections when specific links in the navigation menu are clicked.

Here is the code snippet that I am currently experimenting with on JSFiddle.

HTML

<header id="top">
 <nav>
  <ul>
   <li> 
    <a href="#about">About</a>
   </li>
   <li>
    <a href="#skills">Skills</a>
   </li>
   <li>
    <a href="#projects">Projects</a>
   </li>
   <li>
    <a href="#contact">Contact</a>
   </li>
  </ul>
 </nav>
</header>

<main role="main" id="main">
 <section id="about">
 </section>
 <section id="skills">
 </section>
 <section id="projects">
 </section>
 <section id="contact">
 </section>
</main>

CSS

.active {
  background: $lightest-blue;
  color: $blue;
}

.hide-section {
  display: none;
}

section {
  height: 1em;
  background: blue;
}

JavaScript

// Creating variables to reference the Nav links and About section link.
var siteNav = $("#top li").children();
var aboutLink = siteNav[0];

// Creating variables to reference all Sections and the About section.
var siteSections = $("#main").children();
var aboutSection = siteSections[0];

// Hiding all major sections by adding a CSS class.
siteSections.addClass("hide-section");

// When the document is ready, set the user's initial "arrival" at the About section by removing the hidden class from it.
// Also add an active class to the About section link.
$(function() {
  $(aboutLink).addClass("active");
  $(aboutSection).removeClass("hide-section");
});

// Event listener for clicking on a Nav anchor.
$("#top a").click(function() {
  // Remove active class from all links.
  siteNav.removeClass("active");
  // Add active class to the clicked link.
  $(this).addClass("active");
  // Identify the corresponding section.
  var hrefLink = $(this).attr("href");
  // Hide all other sections.
  siteSections.addClass("hide-section");
  // Reveal the appropriate section.
});

Answer №1

Hey there, let's take a step back - no need to recreate the wheel! :)

Check out the Jquery tabs library for an easy solution:

http://jqueryui.com/tabs/

Simply paste the code into your website, remove the style sheet link, and you're good to go! :D

Answer №2

Utilize this JavaScript code to switch between different screens with a cool visual effect using CSS transitions.

$("#top a").click(function() {
 // 1. Remove active class from all links.
 siteNav.removeClass("active");
 // 2. Add active class to clicked link.
 $(this).addClass("active");
 // 3. Identify the target section.
 var hrefLink = $(this).attr("href");
 // 4. Hide all other sections.
 $('#'+siteSections[0].id).hide();
 $('#'+siteSections[1].id).hide();
 $('#'+siteSections[2].id).hide();
 $('#'+siteSections[3].id).hide(); 
 // 5. Display the target section.
 $(hrefLink).show();
});

Here is some accompanying CSS:

section{
background:red;
}
#about{
 background-color:blue;  
}
#skills{
 background-color:yellow;  
}

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

The object's type remains a mystery

While working on implementing jwt authentication in Ionic, React with TypeScript, I faced a typescript error when trying to add a check in my App.tsx file after successful implementation. The error stated: Object is of type 'unknown' Below is ...

Unlock the power of automatic code reloading in Rails with these simple steps

Looking for a way to implement 'hot code reloading' in a development Rails application? Let's say you're working on a Rails project and make changes to a stylesheet. Currently, you have to refresh the page using cmd-r or by clicking th ...

Using Angular 5 to integrate a jQuery plugin

Recently, I've started learning Angular and am currently using version 5. I need to integrate a plugin called 'jquery-circle-progress' into my project. The plugin can be found at this link: I managed to install the plugin using npm and adde ...

Upload an image converted to `toDataURL` to the server

I've been attempting to integrate the signature_pad library, found at signature_pad, but I am struggling to grasp its functionality. Can someone guide me on how to retrieve an image value and send it to my server? Edit: I have experimented with dec ...

Adding tween.js seems to have caused the button click event to stop triggering

After adding the code line to tween my display box, the click event is no longer triggered. There are no errors in the console. How can I resolve this issue and what might have caused it? createjs.Tween.get(directionsbox, { loop: false }).to({ x:800, y: ...

Making an HTTP request using AngularJS takes significantly more time than directly accessing the endpoint through a web browser

I'm currently working on improving the functionality of a WordPress website using AngularJS. To achieve this, I am utilizing the WP-API (v2) plugin to create custom endpoints and consuming them on the front end with the Angular $http service. Below i ...

Setting up a Web application testing environment on its own

Embarking on my journey in web application development and testing, I am currently involved in a project that requires me to create a standalone environment for testing the web application. The main goal is to ensure the web application is easily testable ...

How can I efficiently retrieve the "value" of a cookie and store it in a .txt file using Python?

Seeking to automate certain requests, I am currently using selenium's get_cookie function to extract cookies from a website. The retrieved cookie data is returned as a dict structured like this: {'domain': 'website-name-here', &a ...

Retrieve the information from a fetch call and save it for later use

Greetings to the wonderful StackOverFlow community. As a beginner in web development, I have countless questions swirling in my mind. The focal point of my query is regarding a fetch request in JavaScript. I am struggling to extract the data (userId) fr ...

The daterangepicker reigns supreme above all other elements

Just a heads up, I am utilizing daterangepicker from this link: . Recently, I encountered an issue where the input field was overlapping with other elements like bootstrap Modals or menus. <div class="col-md-3 form-group"> <div class=&qu ...

Enclose a pair of divs within a fresh div wrapper for better organization

Imagine you have the following: <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv"></div> <div class="somediv" ...

Using v-bind to dynamically set styles in Vue.js

While utilizing v-bind:style to apply dynamic values, I encountered an issue where v-bind:style did not seem to work. However, in the console, I verified that v-bind:style was receiving the correct value (:style='{ color : red (or any other value) }&a ...

What is the best method for simultaneously listening to several events?

For instance, I am interested in setting up a situation where a callback is triggered only when ALL specified events occur: on(['aEvent', 'bEvent', 'cEvent'], callback) The callback function should receive an array (or objec ...

Deactivate button using Javascript

Can anyone assist me with this issue I am having? I currently have a button set up as follows: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> I need to disable the button using jQuery, standard javascript ...

Side by side CSS list item with before element

Currently, I am working on displaying a list of 3 items where the list item number is larger than the actual text. I have been successful in achieving this part, but now my goal is to place them side by side. This is the code snippet I have implemented: ...

Having trouble retrieving the API URL id from a different API data source

For a small React project I was working on, I encountered a scenario where I needed to utilize an ID from one API call in subsequent API calls. Although I had access to the data from the initial call, I struggled with incorporating it into the second call. ...

Rails: jQuery - page refreshes unexpectedly during event execution

After successfully implementing a jQuery script for my custom dropdown menu on a standalone webpage, I encountered issues when integrating it into my Rails application. I am unsure if the problem is related to Turbolinks or if there is another underlying c ...

The button colors in Material Ui do not update properly after switching themes

React Material UI is being utilized in this project. Although the theme is being overridden, the colors of buttons and inputs remain unchanged. Here is how the Material UI theme is created in theme.js // @flow import { createMuiTheme } from '@materi ...

There seems to be an issue with the integration of Bootstrap in my JavaScript file

Is there a way to ensure that each new data entry in the array appears next to each other rather than stacking beneath one another? This is a JavaScript file with HTML written in Bootstrap format. const collegeData = [{ name: "University of Penn ...

What are the risks associated with allowing user-generated HTML or Javascript code on a website?

Is it really dangerous to allow users to edit HTML with Jinja2 templates and access some server-side variables that will be rendered later? I know Google uses Caja Compiler to sanitize and sandbox HTML served from Google Apps Script. Should I be concerned ...