The step-by-step guide on creating a unique transition effect with JavaScript

Currently, I am in the process of building this website from scratch for the first time. The overall layout has been designed based on the PSD file I created, and everything seems to be going well. Now, I am looking to enhance the site by incorporating some javascript effects and functions.

On the index page, there is a banner, sidebar, and content (articles). When clicking on an article, it redirects to that specific article. However, I would like to implement a custom loading page similar to the one shown at 0:22 in this video: https://www.youtube.com/watch?v=k1q6Y_snURw#t=20

I need to either create or find a gif for this purpose, but once I have that ready, I am unsure of how to integrate it into the site.

Each article has its own unique class, so I assume I will need to utilize the ID for individual articles to ensure they are directed to the correct location.

Answer №1

UPDATE: Insert the following code into a file and include a reference to it on each webpage with a link:

function loadXMLDoc(name) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.addEventListener("load", transferComplete, false);
  xmlhttp.open("GET", name, true);
  document.getElementById("Loading").style.display = "block";
  xmlhttp.send();

  function transferComplete() {
    document.write(xmlhttp.responseText);
    history.replaceState(null, null, name);
  }
}

function AJAXcallback(e) {
  var e = window.e || e;
  var href = e.target.getAttribute("href");
  if (e.target.tagName !== 'A' || href[0] == "#" || href.substring(0, 11).toUpperCase() == "JAVASCRIPT:" || e.target.class == "noloadpage")
    return;
  e.preventDefault();
  loadXMLDoc(href);
}
if (document.addEventListener)
  document.addEventListener('click', AJAXcallback, false);
else
  document.attachEvent('onclick', AJAXcallback);

Include the following code to display a loading indicator while the next page is loading (be sure to assign the id "Loading") as shown below:

<div id="Loading" style="display:none">Loading</div>

This script enables every link that is not an anchor (#id) or javascript function (javascript:whatever) and does not have the "noloadpage" class to load using AJAX and display the Loading element during the process.

Answer №2

To achieve a smooth scrolling effect to your article, you can utilize the animate and scrollTop functions in jQuery.

For more information on animating scrollTop with jQuery, check out this link: Animating ScrollTop with jQuery

Next, you'll need to select your article using an appropriate selector. Learn more about selectors here: jQuery Selectors Documentation

If your article has a specific class assigned to it, you can use the following code snippet:

$("sidebar").click(function () { 
    // Assuming the sidebar value matches the class of the article
    $("html, body").animate({ scrollTop: $("." + $(this).val()).position().top }); 
});

I hope this explanation helps clarify things for you.

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 organize the size of multiple files into a single HTML card?

Is there a way to automatically arrange multiple items (photos/videos) within a single HTML card, similar to how it is done on Facebook or Instagram? I am looking for a library or JavaScript code that can help me achieve this without extending the size of ...

Reverting the box color changes in the opposite order of clicking them using HTML, CSS, and Jquery

As someone new to front end development, I am experimenting with creating multiple boxes using HTML, CSS, and jQuery. My goal is to have the boxes change color to blue when clicked, then revert back to their original color in the reverse order they were cl ...

Methods for displaying form errors in index.html.erb in Rails 4

Managing Suppliers with Ruby on Rails class SuppliersController < ApplicationController before_action :set_supplier, only: [:show, :edit, :update, :destroy] # GET /suppliers # GET /suppliers.json def index @suppliers = Supplier.all e ...

Changing the font-family on the dropdown menu provided by Codrops seems to be difficult

Looking for help with the Tympanus Codrops Dropdown Menu - SimpleDropDownEffects. I'm having trouble changing the font-family on my website. I am using Icomoon for the icons, and while that works fine, I can't seem to properly format the text ho ...

The clash between CSS's 'snap-scroll' and jQuery's '.animate scrollLeft' functionality

My HTML and CSS slider utilizes the scroll-snap feature for manual scrolling along with jQuery buttons for automated scrolling. However, I have encountered an issue where using scroll-snap-type: x mandatory; causes severe lag in the jQuery scrollLeft anima ...

Is there a way to eliminate a div and all its contents from the DOM?

As I work on my web application, I am trying to implement a system where error messages can be returned via ajax upon success or failure. The ajax script is functioning correctly in terms of returning errors or successes. However, my challenge lies in com ...

Percentage-based height is malfunctioning across all browsers when utilizing bootstrap framework

I have provided my code below. How can I effectively use percentage height? <div class="container" > <div class="row"> <div class="col-" id="t1"></div> </div> </div> Css body{ background-color:aqua ...

Assigning a variable within a parent function from a child function in JavaScript

Struggling to assign the value of "result" in the inner function. Any suggestions on how to do this? I am able to console log the result variable inside the function, but a friend recommended using promises. However, I have no clue how to implement that ...

establish the data type for the key values when using Object.entries in TypeScript

Task Description: I have a set of different areas that need to undergo processing based on their type using the function areaProcessor. Specifically, only areas classified as 'toCreate' or 'toRemove' should be processed. type AreaType ...

Add a click event listener to the body element using a button click, without causing it to trigger

What is the current situation: A button is clicked by the user The menu opens (list items display = block) The function to close the menu is connected to the <body> The function to close the menu is immediately triggered, causing the menu to close ...

Exploring the depths of design in material-ui

I've just started diving into material-ui and decided to create a simple app in the SandBox: https://codesandbox.io/s/eager-ride-cmkrc The styling using jss is a bit unusual for me, but with your help on these two exercises, I'm sure I'll ...

Setting up the jscolor chooser to work with Bootstrap buttons is a breeze

https://i.sstatic.net/rxICD.png I'm attempting to implement the jscolor chooser as a bootstrap button element. When retrieving the color code from the database, the input element displays the correct color chosen by the user. However, the bootstrap b ...

Error: The method api.createContextKey is not defined while running the Next.js development server

I am currently facing an issue while attempting to start the development server for my Next.js project. The problem arises when I try to incorporate MUI into my project, resulting in these specific errors. The error message points to a TypeError related to ...

Issue with CanvasJS Carousel compatibility on Bootstrap 4 platform

I am struggling to display charts with a carousel feature using CanvasJS. Despite my efforts, I am unable to see any graphs on the page. My framework is Bootstrap 4.1.3. In my attempts to achieve this functionality, I previously experimented with Google C ...

Connecting Angular TextArea functionality to the Ctrl-Enter keyboard shortcut

Recently, I've been working on creating a custom functionality for the <textarea> element. My goal is to have the enter key trigger a specific function, while using ctrl+enter will add a new line in the textarea. I've tried looking through ...

What is the best way to make this relative path function in JavaScript?

My file structure is organized in the following way: multiple folders a subfolder _includes getStatisticsTable.php _templates StatisticsWrapper.html Within StatisticsWrapper.html, I am using jQuery's .get() method to fetch external data which ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

I'm curious about how to initiate a fresh Rails 7 project while incorporating Bootstrap 5 and jQuery assistance

Looking to start a new Rails project with version 7.3.1 and integrate the latest Bootstrap framework. However, I've heard that jQuery may not be compatible with the latest version of Bootstrap. I am also considering downloading the latest Bootstrap fi ...

Encountering a Component Exception error in React Native while attempting to implement a Victory chart component

Currently, I am exploring the Victory library for react native to create a line chart for my budgeting application. Below is the code snippet from my Graph component: import React from 'react'; import { View, StyleSheet } from 'react-native& ...

The ngOnInit function is not triggered upon instantiation of an Injectable class

What could be causing the ngOnInit() method not to be called upon resolution of an Injectable class? Code import {Injectable, OnInit} from 'angular2/core'; import { RestApiService, RestRequest } from './rest-api.service'; @Injectable ...