If you are trying to do this on a mobile device, it may take longer to start the animation since the device is waiting (even 200 ms +) to make sure that you are not trying to double-tap. If so, you can go to libraries like zepto.js , which has a tap event or fastclick.js , which emulates click events.
in terms of selector performance, $(this).parent() should be fine. In this case, however, you could eliminate a few milliseconds without writing or reading the variable first, for example:
$(".expandable .level1").bind('click', function () { $(this).next().slideToggle(200, function () { $(this).parent().toggleClass("opened"); }); });
In addition, "As with jQuery 1.7, the .on () method is the preferred method for attaching event handlers to a document." ( jQuery api ). Or, in other words, bind() deprecated!
And finally, there is a general rule according to which filtering by tag name is faster than by class name or id, so $("li.expandable .level1") should be faster than $(".expandable .level1") .
Perhaps you can also be more specific regarding specific circumstances, such as a browser / device or any other difference from your violin?
source share