Web Designer - Creative Internet Profession
From the author: in this article we will talk about the profession of web designer. Consider who it is, what knowledge and skills it should own, what tasks it solves,…

Continue reading →

Button State Design
From the author: button design is the main element of any design system. The best of the buttons are simple, versatile, perhaps even a little fun. But there are many…

Continue reading →

How to become a web designer, and what qualities should a really cool specialist have?
From the author: many of you were probably looking for the answer to the question: how to become a web designer? Of course, this is a difficult question, to which…

Continue reading →

JQuery preloader

There is a delay when loading the page content asynchronously or sending a request to add / edit. A preloader is an animation during this delay, which allows you to show the user that the request has been sent and you need to wait for it to complete.

There are a huge number of services that allow you to download GIF or SVG preloader. However, in any case, when sending a request, you need to add a preloader to the page structure, and after it is completed, delete it. Doing it manually with each request is inconvenient. In this article, we will look at how to make the process of adding / removing a preloader more convenient using a simple jQuery plugin.

Plugin blank
The plugin will work as shown in the picture:

The plugin should work as follows: to add the preloader to the container, we will call the method:

Javascript
$ (‘# container’). preloader (‘start’);
Accordingly, to remove the preloader, you will need to call:

$ (‘# Container’). Preloader (‘stop’);
Create a jQuery plugin preset:

Javascript
(function ($) {
$ .fn.preloader = function (action) {
return this;
};
} (jQuery));
Here, action is a parameter that indicates whether the preloader is added to the container or deleted. Depending on what action is transferred, you need to call the corresponding function.

Preloader on / off functions
Let’s write the start function:

Javascript
var actions = {
start: function () {
var $ preloader = $ (“<div id = ‘jpreloader’ class = ‘preloader-overlay’> <div class = ‘loader’ style = ‘position: absolute; left: 50%; top: 50%; margin-left: -24px; margin-top: -24px; ‘> <svg width =’ 48px ‘height =’ 48px ‘xmlns =’ http: //www.w3.org/2000/svg ‘viewBox =’ 0 0 100 100 ‘preserveAspectRatio = ‘xMidYMid’ class = ‘uil-default’> … </svg> </div> </div> “);
$ preloader.css ({
‘background-color’: ‘# 4c4c4c’,
‘width’: ‘100%’,
‘height’: ‘100%’,
‘left’: ‘0’,
‘top’: ‘0’,
‘opacity’: ‘0.3’,
‘z-index’: ‘100000’,
‘position’: ‘absolute’
});
this.append ($ preloader);
}
};
The actions object will contain the available action functions. In the start function, we create a new jQuery object containing the preloader, set the necessary styles for it and add it to the container. The preloader itself is a <div> block, absolutely positioned relative to the container and stretched to the full width and height of the container. We give it a deliberately large z-index value so that it overlaps the contents of the container, as well as an opacity value of 0.3.

Please note that the container must have a position property other than static so that the preloader is correctly positioned.

Inside the <svg> … </svg> tags in the preloader is the contents of the animated svg preloader. I do not give his code, because on the Internet you can find a huge number of SVG preloaders at your discretion. Just download your favorite preloader, open it with any text editor and add content to the plugin instead of the <svg> </svg> tags.

Now add the stop function, which will remove the preloader:

Javascript
stop: function () {
this.find (‘. preloader-overlay’). remove ();
}
Everything is extremely simple here: we look inside the container for elements with the preloader-overlay class and delete it.

Calling the corresponding function
Now, depending on the value of action, you need to call the desired function. To do this, use the apply function.

Javascript
$ .fn.preloader = function (action) {
if (actions [action] && typeof actions [action] === ‘function’) {
actions [action] .apply (this);
}
return this;
};
We check whether there is a correspondence in the actions object for the passed parameter and call the desired function.

Consider the full plugin code:

Javascript
(function ($) {
var actions = {
start: function () {
var $ preloader = $ (“<div id = ‘jpreloader’ class = ‘preloader-overlay’> <div class = ‘loader’ style = ‘position: absolute; left: 50%; top: 50%; margin-left: -24px; margin-top: -24px; ‘> <svg width =’ 48px ‘height =’ 48px ‘xmlns =’ http: //www.w3.org/2000/svg ‘viewBox =’ 0 0 100 100 ‘preserveAspectRatio = ‘xMidYMid’ class = ‘uil-default’> … </svg> </div> </div> “);
$ preloader.css ({
‘background-color’: ‘# 4c4c4c’,
‘width’: ‘100%’,
‘height’: ‘100%’,
‘left’: ‘0’,
‘top’: ‘0’,
‘opacity’: ‘0.3’,
‘z-index’: ‘100’,
‘position’: ‘absolute’
});
this.append ($ preloader);
},

stop: function () {
this.find (‘. preloader-overlay’). remove ();
}
};

$ .fn.preloader = function (action) {
actions [action] .apply (this);
return this;
};
} (jQuery));
Using plugin
Using the plugin is extremely simple. Let’s look at an example: let’s say we have some form that is sent to the server using an AJAX request.

Rubber web design. What and where are we going to stretch?
When developing a website, the webmaster, first of all, should be based on the goals of the project. If it is important for the customer that the web page can…

...

Flat Design Sites (Flat Design): history, principles, examples for inspiration
What do you think is the reason for such a frenzied popularity of flat design? I will answer you in a nutshell: it really works! In this article, I will…

...

What every web designer should know and be able to not face the dirt
What every web designer should know and be able to not face the dirt What every web designer should know and be able to From the author: I welcome you,…

...

Effective web design - what is it?
If you want to achieve serious heights and build a successful career as a web designer, then you definitely need to know the key criteria for evaluating the effectiveness of…

...