d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Jquery Help
Add Reply New Topic New Poll
Member
Posts: 29,305
Joined: Nov 12 2005
Gold: 710.00
Jun 9 2014 05:30pm
I'm new to jQuery and I'm just trying to mess around with it at the moment and learn the basics. Now, if I would like to use jQuery for a hover effect instead of CSS, how would I go about doing so (using functions other than .hover)?

Code
$(document).ready(function() {
$('li.menu a').mouseenter(function() {
$('li.menu a').css("background", "#006db0");
$('li.menu a').mouseleave(function() {
$('li.menu a').css("background", "#bbbbbb");
});
});
});


With the above code I have 4 links in a menu (li.menu a is the selector ofc). When I use the jQuery above, all 4 of them turn red when I hover over 1. I would like to know how to make it so only works on the current one I'm hovered over.

Unrelated to this problem, how long do you think I'd have to study jQuery before I can make a simple gallery by myself that lets me click the picture, open up a box on the page, and scroll through the rest? That's ultimately my goal. I would like to make a cool gallery for my portfolio as opposed to downloading a script.

EDIT: I know it's a lot easier to just do it in CSS but learning this way will help me get the syntax down at least lol. Surely I don't have to assign a class to each link in the menu and use that little bit of code for each one. That's the only way I know how to make it select just 1 instead of all 4.

Code
$(document).ready(function() {

$('li.menu a.gallery').mouseenter(function() {
$('li.menu a.gallery').css("background", "#006db0");
$('li.menu a.gallery').mouseleave(function() {
$('li.menu a.gallery').css("background", "#bbbbbb");
});
});

$('li.menu a.services').mouseenter(function() {
$('li.menu a.services').css("background", "#006db0");
$('li.menu a.services').mouseleave(function() {
$('li.menu a.services').css("background", "#bbbbbb");
});
});

$('li.menu a.resources').mouseenter(function() {
$('li.menu a.resources').css("background", "#006db0");
$('li.menu a.resources').mouseleave(function() {
$('li.menu a.resources').css("background", "#bbbbbb");
});
});

$('li.menu a.contact').mouseenter(function() {
$('li.menu a.contact').css("background", "#006db0");
$('li.menu a.contact').mouseleave(function() {
$('li.menu a.contact').css("background", "#bbbbbb");
});
});

});


That seems like way too much code for something simple.

This post was edited by Santora on Jun 9 2014 05:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 9 2014 06:05pm
Quote (Santora @ Jun 9 2014 07:30pm)
I'm new to jQuery and I'm just trying to mess around with it at the moment and learn the basics. Now, if I would like to use jQuery for a hover effect instead of CSS, how would I go about doing so (using functions other than .hover)?

Code
$(document).ready(function() {
    $('li.menu a').mouseenter(function() {
        $('li.menu a').css("background", "#006db0");
    $('li.menu a').mouseleave(function() {
        $('li.menu a').css("background", "#bbbbbb");
});
});
});


With the above code I have 4 links in a menu (li.menu a is the selector ofc). When I use the jQuery above, all 4 of them turn red when I hover over 1. I would like to know how to make it so only works on the current one I'm hovered over.

Unrelated to this problem, how long do you think I'd have to study jQuery before I can make a simple gallery by myself that lets me click the picture, open up a box on the page, and scroll through the rest? That's ultimately my goal. I would like to make a cool gallery for my portfolio as opposed to downloading a script.

EDIT: I know it's a lot easier to just do it in CSS but learning this way will help me get the syntax down at least lol. Surely I don't have to assign a class to each link in the menu and use that little bit of code for each one. That's the only way I know how to make it select just 1 instead of all 4.

Code
$(document).ready(function() {

    $('li.menu a.gallery').mouseenter(function() {
        $('li.menu a.gallery').css("background", "#006db0");
    $('li.menu a.gallery').mouseleave(function() {
        $('li.menu a.gallery').css("background", "#bbbbbb");
});
});

    $('li.menu a.services').mouseenter(function() {
        $('li.menu a.services').css("background", "#006db0");
    $('li.menu a.services').mouseleave(function() {
        $('li.menu a.services').css("background", "#bbbbbb");
});
});

    $('li.menu a.resources').mouseenter(function() {
        $('li.menu a.resources').css("background", "#006db0");
    $('li.menu a.resources').mouseleave(function() {
        $('li.menu a.resources').css("background", "#bbbbbb");
});
});

    $('li.menu a.contact').mouseenter(function() {
        $('li.menu a.contact').css("background", "#006db0");
    $('li.menu a.contact').mouseleave(function() {
        $('li.menu a.contact').css("background", "#bbbbbb");
});
});

});


That seems like way too much code for something simple.


i'm by no means a javascript/css expert, but i'm guessing you can use IDs. give your li id=1, then your "a" can have id='a1'. then on mouseenter, just append 'a' with the id of current mouseenter control, find that control, and change its css.

then your second li has id=2 and its corresponding a can have id='a2', etc

This post was edited by carteblanche on Jun 9 2014 06:06pm
Member
Posts: 29,305
Joined: Nov 12 2005
Gold: 710.00
Jun 9 2014 07:29pm
Quote (carteblanche @ Jun 9 2014 08:05pm)
i'm by no means a javascript/css expert, but i'm guessing you can use IDs. give your li id=1, then your "a" can have id='a1'. then on mouseenter, just append 'a' with the id of current mouseenter control, find that control, and change its css.

then your second li has id=2 and its corresponding a can have id='a2', etc


I appreciate the help. I believe that's exactly what I did with the second set of code except I used classes to give the selectors an identity. I'm just hoping there is an easier way to do it with jQuery because all of that jQuery code is the equivalent of:

Code
li.menu a {
background: #bbbbbb;
}

li.menu a:hover {
background: #006db0;
}


I think there is a .hover function in jQuery, I'll check that out.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 9 2014 08:16pm
surely your event handlers have a parameter which tells you the caller? then attach the same event handler to all your li in your markup, and use the param to identify what control needs the new background.

This post was edited by carteblanche on Jun 9 2014 08:16pm
Member
Posts: 29,305
Joined: Nov 12 2005
Gold: 710.00
Jun 10 2014 12:22am
Apparently "this" is crucial lol. Ended up figuring it out. I just created a new class (hover) and used the hover function in jQuery. I should have just went with that function to begin with instead of trying to complicate things lol.

Code
$(document).ready(function(){
$('li.menu a').hover(
function(){
$(this).addClass('hover');
},
function(){
$(this).removeClass('hover');
}
);
});


This post was edited by Santora on Jun 10 2014 12:26am
Member
Posts: 2,579
Joined: Jun 1 2012
Gold: 1,524.00
Jun 10 2014 03:21pm
Quote (Santora @ Jun 10 2014 02:22am)
Apparently "this" is crucial lol. Ended up figuring it out. I just created a new class (hover) and used the hover function in jQuery. I should have just went with that function to begin with instead of trying to complicate things lol.

Code
$(document).ready(function(){
$('li.menu a').hover(
  function(){
  $(this).addClass('hover');
  },
  function(){
  $(this).removeClass('hover');
  }
);
});


Yup, "this" is a reference to the DOM element of invocation in a callback.

$() is the jQuery constructor, so $(this) lets you use jQuery functions on that DOM element

This post was edited by Schwag on Jun 10 2014 03:22pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll