Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining jquery for effiency

Tags:

jquery

Attempting to condense some of the coding on a website I'm now looking after and was wondering if there is a more efficient way of writing the following coding as I realise there is doubling up of information.

$("span.menu-link-title.active-components").mouseover(function(){$('#active-components-image').css('display', 'block');});
$("span.menu-link-title.active-components").mouseleave(function(){$('#active-components-image').css('display', 'none');});
$("span.menu-link-title.adaptors").mouseover(function(){$('#adaptors-image').css('display', 'block');});
$("span.menu-link-title.adaptors").mouseleave(function(){$('#adaptors-image').css('display', 'none');});
like image 748
Vince P Avatar asked Jun 27 '26 07:06

Vince P


2 Answers

You can use hover and toggle methods:

$("span.menu-link-title.adaptors").hover(function(){
   $('#adaptors-image').toggle();
});

hover is a shorthand for mouseenter(not mouseover) and mouseleave events and it also accepts 2 functions. You can also cache the objects.

var $adapt = $('#adaptors-image');
$("span.menu-link-title.adaptors").hover(function(){
    $adap.show(); // mouseenter
}, function(){
    $adap.hide(); // mouseleave
});
like image 185
undefined Avatar answered Jun 28 '26 22:06

undefined


This might be a little more lean:

$("span.menu-link-title.active-components").hover(function() {
    $('#active-components-image').toggle();
});
$("span.menu-link-title.adaptors").hover(function() {
    $('#adaptors-image').toggle();
});​
like image 27
j08691 Avatar answered Jun 28 '26 21:06

j08691



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!