Ich habe 3 Funktionen, die alle mit Click-Ereignissen eingestellt sind, die die gleichen Funktionen innerhalb ihrer jeweiligen Divs auslösen. Ich möchte das optimieren, also wiederhole ich mich nicht immer wieder. Was wäre der beste Weg dies zu schreiben?Die beste Methode zum Optimieren und Zusammenführen dieser Jquery-Funktionen
//Progress Bar animation when tab clicked
$('#tab1').click(function() {
$('#stack .progress div').each(function() {
var display = $(this),
currentValue = parseInt(display.text()),
nextValue = $(this).attr('data-values'),
diff = nextValue - currentValue,
step = (0 < diff ? 1 : -1);
if (nextValue == '0') {
$(display).css('padding', '0');
} else {
$(display).css('color', '#fff').animate({
'width': nextValue + '%'
}, 'slow');
}
for (var i = 0; i < Math.abs(diff); ++i) {
setTimeout(function() {
currentValue += step
display.html(currentValue + '%');
}, 20 * i);
}
})
});
$('#tab2').click(function() {
$('#design .progress div').each(function() {
var display = $(this),
currentValue = parseInt(display.text()),
nextValue = $(this).attr('data-values'),
diff = nextValue - currentValue,
step = (0 < diff ? 1 : -1);
if (nextValue == '0') {
$(display).css('padding', '0');
} else {
$(display).css('color', '#fff').animate({
'width': nextValue + '%'
}, 'slow');
}
for (var i = 0; i < Math.abs(diff); ++i) {
setTimeout(function() {
currentValue += step
display.html(currentValue + '%');
}, 20 * i);
}
})
});
$('#tab3').click(function() {
$('#other .progress div').each(function() {
var display = $(this),
currentValue = parseInt(display.text()),
nextValue = $(this).attr('data-values'),
diff = nextValue - currentValue,
step = (0 < diff ? 1 : -1);
if (nextValue == '0') {
$(display).css('padding', '0');
} else {
$(display).css('color', '#fff').animate({
'width': nextValue + '%'
}, 'slow');
}
for (var i = 0; i < Math.abs(diff); ++i) {
setTimeout(function() {
currentValue += step
display.html(currentValue + '%');
}, 20 * i);
}
})
});
Dies erzeugt den folgenden Fehler "Uncaught TypeError: Kann Eigenschaft 'Aufruf' von undefined nicht lesen" –
mein Fehler! gerade jetzt korrigiert. – ykaragol
Obwohl ich wusste, dass es mehrere Möglichkeiten gab, das zu tun, was ich wollte, schien es ziemlich trocken und schneidend zu sein und ich musste keine zusätzlichen Typen-Tags hinzufügen. Vielen Dank! –