经过大量的反复试验,并且不了解它的内涵,我已经做到了:
$("#experience-left-details").on('click', '.see-map, .see-gallery', function (event) {
event.preventDefault();
$("#experience-left-details").fadeToggle(
function(){
$(".media-container, .swiper-container").toggleClass('hide');
mySwiper.resizeFix(true);
mySwiper.reInit(true);
}
);
$("#experience-left-details").fadeToggle();
});
是否可以通过保留功能(显然)来重制此代码,而无需重复此行:
$("#experience-left-details").fadeToggle();
答案 0 :(得分:1)
我觉得你连续两次调用相同的方法,这就是我要如何清理它:
// store the jquery object for later use,
// instead of recreating over and over
var $eld = $("#experience-left-details");
$eld.on('click', '.see-map, .see-gallery', function (event) {
event.preventDefault();
// use fadeOut its more clear as to what you are doing
$eld.fadeOut(
function(){
$(".media-container, .swiper-container").toggleClass('hide');
mySwiper.resizeFix(true);
mySwiper.reInit(true);
}
// chain on fadeIn for after fadeOut is done
).fadeIn();
});