Skip to content

Instantly share code, notes, and snippets.

@lucianomlima
Created January 28, 2015 14:41
Show Gist options
  • Save lucianomlima/9706fdef0c0f640b7c61 to your computer and use it in GitHub Desktop.
Save lucianomlima/9706fdef0c0f640b7c61 to your computer and use it in GitHub Desktop.
Exemplo de otimização de código jQuery
/**
* Código antes da otimização
* Fornecido por Ricardo Miranda
*/
/*
$(document).ready(function () {
try {
$('#orelha').delay(5000).animate({
right: '150px'
}).delay(15000).animate({right:'50px'});
$("#orelha a.fecha").hide();
$("#orelha a.abre").click(function (g) {
g.preventDefault();
$("#orelha").animate({right: "+=221"}, "slow", function () {
$("#orelha a.abre").hide();
$("#orelha a.fecha").show();
});
});
$("#orelha a.fecha").click(function (g) {
g.preventDefault();
$("#orelha").animate({right: "-=221"}, "slow", function () {
$("#orelha a.abre").show();
$("#orelha a.fecha").hide();
});
});
} catch (e) {
console.log('error ', e);
}
});
*/
$(function () {
var element = $('#orelha'),
openLink = $('a.abre', element),
closeLink = $('a.fecha', element),
linkAnimation = function(hideElement, showElement){
hideElement.hide();
showElement.show();
};
try {
element
.delay(5000)
.animate({right: '150px'})
.delay(15000)
.animate({right: '50px'});
closeLink.hide();
openLink.click(function (event) {
event.preventDefault();
element.animate({right: "+=221"}, "slow", linkAnimation(openLink, closeLink));
});
closeLink.click(function (event) {
event.preventDefault();
element.animate({right: "-=221"}, "slow", linkAnimation(closeLink, openLink));
});
} catch (e) {
console.log('Error:', e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment