Is this horribly inefficient or does it look ok??? How do I test resources used by it?
$.easing.def = "easeOutBack";
$(document).ready(function() {
var numResults = $("#scroll > div").size();
var scrollSize = numResults * 264;
var stopSize = ((numResults - 6) * 264) * -1;
$("#scroll").width(scrollSize);
$("#page-left").hide();
$("#page-right").click(function() {
var marginleft = parseInt(jQuery("#scroll").css("margin-left"));
if(marginleft > stopSize) {
$("#page-left").show();
$(this).hide();
$("#scroll").animate({"margin-left": "-=783px"}, 800, function() {
var marginleft = parseInt(jQuery("#scroll").css("margin-left"));
if(marginleft > stopSize) {
$("#page-right").show();
}
});
}
});
$("#page-left").click(function() {
var marginright = parseInt(jQuery("#scroll").css("margin-left"));
if(marginright < -10) {
$("#page-right").show();
$(this).hide();
开发者_Python百科 $("#scroll").animate({"margin-left": "+=783px"}, 800, function() {
var marginright = parseInt(jQuery("#scroll").css("margin-left"));
if(marginright < -10) {
$("#page-left").show();
}
});
}
});
});
Chrome gives you the ability to take heap snapshots. DeveloperTools->Profiles->HeapSnapshots
You can take snapshot at various time intervals to compare memory usage. Another option is paid one http://www.softwareverify.com/javascript/memory/feature.html
I don't see any reason why that would consume much in terms of resources. You're just animating things left and right, right? I guess some better coding practices that I'd point out would be to store things you use repeatedly like $("#scroll")
in a variable so you don't search the DOM every time for the same thing, and also choosing one of jQuery
or $
unless you need to do otherwise.
The real question I'd have is what exactly 783
represents. If it's because your screen is 800 pixels wide, then keep in mind that not everyone will see you page that way.
As for the profiling part, Rizwan's answer gets +1.
精彩评论