jQuery Tutorial: How to load and animate content using jQuery
王林
Release: 2023-09-03 21:21:07
Original
1298 people have browsed it
Clicking on any link on a web page will usually load the contents of that URL in our browser. This is how most links and websites on the internet work. However, you can also change this default behavior with some code to load the content of the new URL into a specific element of the current web page without reloading the entire page.
This can be achieved with a little help from JavaScript. We will use the jQuery library to do the heavy lifting related to animation and AJAX content loading.
You can also use plain JavaScript to load and animate content.
Prepare mark
We will use a very simple web page to demonstrate how the effect works. However, the principles you learn here apply to other websites as well. This is the markup for the home page of the website that we will load and animate.
Home
Embrace Pawsitivity, Transform Lives!
Welcome to Pawsitive Care Foundation, a dedicated organization working towards the well-being and protection of animals.
Our animal Welfare NGO provides a range of services to support animal welfare:
Rescue and rehabilitation of abused and abandoned animals
Adoption programs to find loving homes for animals in need
Education and awareness campaigns to promote responsible pet ownership
Lobbying and advocacy for stronger animal protection laws
Collaboration with local communities to implement spay/neuter programs
Copy after login
The
tag links to the style.css file, which contains the CSS used to style all web pages. The body of the web page contains the nav element, which contains a list of links that the user can access. There is a span element and a loader class. Whenever the user clicks one of the links in the navigation, we will show and hide this loader element. The loader will indicate that the page is currently loading.
After that, we have a section element with the id set to content. Every page on our website will have this section. The content in this section is what we will load using AJAX. We also have two script tags near the end of the body element. The first script tag loads jQuery, while the second tag loads our own JavaScript file.
With the help of some CSS, our page looks like this:
You can create similar pages named about.html, team.html, and contact.html.
Set styles for loaders and content
We will now learn how to use CSS to animate a loader so that it spins while loading new content. This is the CSS that keeps our loader spinning.
There are several points to note here. First, loaders have absolute positioning. This takes it out of the normal flow of the document so that we can place it wherever we want without interrupting the flow of other content.
We use the animation property to continuously animate the loader based on spin keyframe values, where each animation loop completes in 0.5 seconds.
Using border-radius: 50% makes our loader rounded since it has the same width and height. Using different border colors at the top and bottom is just a style preference.
We will also use the following CSS to ensure that the content we are loading covers the entire width of the body.
section#content {
width: 100% !important;
}
Copy after login
This will become important when we animate the main content.
How you want to style the general content on all of these pages is up to you.
Handling link clicks and content loading
If you load any webpage at this time, one thing you will notice is that the loader will keep showing up. We only want it to appear when content is loading. Once our page is ready, we can use the following code-behind loader:
Since we want to control what happens when the user clicks any link in the navigation menu, we need to attach a listener to these links. The listener's handler function will contain all the code we want to execute every time the link is clicked. Here is the code for our click handler:
The first thing we do in the click handler is prevent the default action from happening. The default action in this example is for the user to navigate to the linked URL.
Since we have blocked the linked URL from loading in the browser, it is our responsibility to manually load this content for the viewer. To do this, we first get the value of the href attribute of the clicked link. We also append #content to the end of the URL because that’s the content we actually want to load.
We use the hide() method in jQuery to hide the #content part. We hide this section because it currently contains the markup for the page the user is trying to leave. hide() The method accepts a string or number as its first parameter. This value determines how long it takes to hide the selected element. Set it to Fast to hide content in 200 milliseconds.
hide() The method animates the width, height, and opacity of the selected element until they become 0. Once they reach zero, the display property is set to none.
The second parameter is a callback function, which is triggered after the hiding animation is completed. We call loadContent() in the callback function.
The above is the detailed content of jQuery Tutorial: How to load and animate content using jQuery. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn