Currently tying to keep my code within an object, I would like to convert the contents of a click event into a method but not overly sure how this is done. The current code looks like this:
Current JS
init: function(){
var _this = this,
slides_li = _this.els.slides.children(),
large = 260,
small = 60;
// Hover
slides_li.on('mouseover', function(){
$(this).stop(true, true).animate({ opacity: 1 }, 300);
}).on('mouseout', function(){
$(this).stop(true, true).animate({ opacity: .8 }, 300)
});
// Click handlers
slides_li.on('click', function(){
// Would like to move this code into its own method toggle_slides() ??
$('.active', _this.els.slides).not(this).animate({
width: small
}, 300).removeClass('active');
// animate the clicked one
if ( !$(this).hasClass('active') ){
$(this).animate({
width: large
}, 300).addClass('active');
}
});
}
but I would like the code to look like this but I know I'm missing a few key things plus this obviously doesn't mean the clicked event:
JS
init: function(){
var _this = this,
slides_li = _this.els.slides.children(),
large = 260,
small = 60;
// Hover
slides_li.on('mouseover', function(){
$(this).stop(true, true).animate({ opacity: 1 }, 300);
}).on('mouseout', function(){
$(this).stop(true, true).animate({ opacity: .8 }, 300)
});
// Click handlers
slides_li.on('click', function(){
toggle_slides(); //pass in this?
});
},
toggle_slides: function(){ // add argument for this?
$('.active', _this.els.slides).not(this).animate({
width: small
}, 300).removeClass('active');
// animate the clicked one
if ( !$(this).hasClass('active') ){
$(this).animate({
width: large
}, 300).addClass('active');
}
}
Can anyone offer some advice on how to make this work?