Let’s say I have the following code:
$('.slide_back').hover(function() {
var $this = $(this),
height = $this.height(),
$slideHide = $this.find('.slide_hide');
$slideHide.stop()
.animate({marginTop: height / 2 + 'px'}, 400);
}, function() {
var $this = $(this),
height = $this.height(),
$slideHide = $this.find('.slide_hide');
$slideHide.stop()
.animate({marginTop: height + 'px'}, 400);
});
You see a great deal of repetition here. Is there a more universal solution I could apply, rather than wrapping duplicate code into another helper function?
UPDATE: I am looking for some sort of ‘restore state’ or ‘undo’ function.
Create a custom function which returns a new function with the desired settings. It’s the neatest way to achieve that result.
Another method is using a variable to remember the current state (see method 2):
Method 1: Helper function
Method 2: Using a variable