有 3 个元素(图像、标题和块)需要active根据hover.
是不是这样:
var svg_planung = $('#planung path.hover'),
svg_realisation = $('#realisation path.hover'),
svg_betrieb = $('#betrieb path.hover'),
planung_box = $('#planungBox'),
realisation_box = $('#realisationBox'),
betrieb_box = $('#betriebBox'),
planung_title = $('#planungTitle'),
realisation_title = $('#realisationTitle'),
betrieb_title = $('#betriebTitle');
// по умолчанияю активные
$(planung_box).addClass('active');
$(svg_planung).addClass('active');
$(planung_title).addClass('active');
$(svg_planung).hover(function() {
$('.dev-box').removeClass('active');
$('path.hover').removeClass('active');
$('.svg-box .title').removeClass('active');
$(planung_box).addClass(' active');
$(svg_planung).addClass(' active');
$(planung_title).addClass(' active');
});
$(svg_realisation).hover(function() {
$('.dev-box').removeClass('active');
$('path.hover').removeClass('active');
$('.svg-box .title').removeClass('active');
$(realisation_box).addClass(' active');
$(svg_realisation).addClass(' active');
$(realisation_title).addClass(' active');
});
$(svg_betrieb).hover(function() {
$('.dev-box').removeClass('active');
$('path.hover').removeClass('active');
$('.svg-box .title').removeClass('active');
$(betrieb_box).addClass(' active');
$(svg_betrieb).addClass(' active');
$(betrieb_title).addClass(' active');
});
该代码按预期工作。是否有可能以任何方式减少它(优化)?
我会传递模板和样式,但这是优化
也许我误解了什么,但是:
是否可以将所有内容切割成一个单独的函数并在悬停时使用参数调用它?