optimize jquery progress bar

  Рет қаралды 31,243

kudvenkat

kudvenkat

Күн бұрын

Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our KZbin channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss how to enhance and optimize the jquery progress bar that we created in Part 48 of jQuery tutorial.
At the moment the progress bar always counts from 1. For example
1. When you select 30%, it starts to count from 1 to 30 which is good.
2. Now if you select 70%, it starts again from 1 and counts all the way till 70, instead of continuing to count from 30 to 70.
The following code counts from the previous point. For example
1. If you select 30% first, it starts to count from 1 to 30. Now if you select 90, it counts from 30 to 90.
2. At this point, if you select 20%, it counts down from 90 to 20.
Replace < with LESSTHAN symbol and > with GREATERTHAN symbol
$(document).ready(function () {
var currentPercentage = 0;
var previousPercenage = 0;
$('#myButton').click(function () {
previousPercenage = currentPercentage;
currentPercentage = $('#ddlPercent').val();
animateProgressBar(previousPercenage, currentPercentage);
});
function animateProgressBar(previousPercenage, currentPercentage) {
$('#innerDiv').animate({
'width': (500 * currentPercentage) / 100
}, 2000);
if (previousPercenage > currentPercentage)
currentPercentage = currentPercentage - 1;
$({ counter: previousPercenage }).animate({ counter: currentPercentage }, {
duration: 2000,
step: function () {
$('#innerDiv').text(Math.ceil(this.counter) + ' %');
}
});
}
});
The above code can be optimized as shown below. This optimization is suggested by Aptem A, one of our youtube subscribers. This is great, thanks to him for his valuable contribution.
$(document).ready(function () {
$('#myButton').click(function () {
animateProgressBar($('#ddlPercent').val());
});
function animateProgressBar(currentPercentage) {
$("#innerDiv").animate({ "width": (currentPercentage * 500) / 100 }, {
duration: 3000,
step: function (now, fx) {
$("#innerDiv").text(Math.ceil((now / 500) * 100) + ' %');
}
});
}
});
step option of the animate function can be used to define a function that gets called after each step of the animation. This method has 2 parameters - now & tween.
1. now - contains the value being animated
2. tween - is a complex object and contains several properties. A few are listed below. For the complete list set a break point and inspect the object
elem - The DOM element being animated
now - The value the animation is currently at
end - The value the animation will end at
jQuery animate method documentation
api.jquery.com/...

Пікірлер: 7
jquery show hide password
11:00
kudvenkat
Рет қаралды 38 М.
jquery animation queue
17:15
kudvenkat
Рет қаралды 35 М.
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54
Какой я клей? | CLEX #shorts
0:59
CLEX
Рет қаралды 1,9 МЛН
ASP.NET Core Crash Course - C# App in One Hour
1:00:44
freeCodeCamp.org
Рет қаралды 1,6 МЛН
Simple jquery progress bar
11:05
kudvenkat
Рет қаралды 58 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 908 М.
jquery ajax load
14:07
kudvenkat
Рет қаралды 123 М.
How to create a Download Progress Bar with JavaScript
31:48
Code With Bubb
Рет қаралды 4,5 М.
Difference between $ each and  each
10:13
kudvenkat
Рет қаралды 64 М.
jquery image slideshow with thumbnails
15:25
kudvenkat
Рет қаралды 44 М.
asp net multiple file upload with progress bar
18:36
kudvenkat
Рет қаралды 79 М.
Jaidarman TOP / Жоғары лига-2023 / Жекпе-жек 1-ТУР / 1-топ
1:30:54