當前綁定將我的代碼保存在一個對象中,我想將點擊事件的內容轉換爲方法,但不是過度確定這是如何完成的。當前的代碼看起來是這樣的:將代碼片段轉換爲javascript中的方法
當前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()?
$('.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');
}
});
}
,但我想的代碼看起來是這樣,但我知道我錯過了幾個關鍵的東西,加上這顯然並不意味着點擊事件:
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');
}
}
任何人都可以提供一些建議,如何使這項工作?
IVE移動的slider_li內容到方法toggle_slides現在怎麼稱呼它? – styler
你打算如何調用init? – PeeHaa
init是對象bep的一部分,所以我在調用bep.init();啓動對象 – styler