javascript - How to reverse hover state with jQuery? - Stack Overflow

My assignment requires that I use jquery only. Doesn't seem practical but I'm limited. I was

My assignment requires that I use jquery only. Doesn't seem practical but I'm limited. I was able to figure out how to do a hover state but when the styles get applied, it stays. So check this out..

$(".cta-button").css({
      background: "#476887",
      "text-align": "center",
      width: "173px",
      height: "40px",
      margin: "62px auto 33px",
      cursor: "pointer"
    });
    $(".cta-button").hover(function() {
      $(this).css("background-color", "#509de5");
    });

Of course when I'm no longer hovering, I want it to revert back to it's original background color. How do I go about doing this? Thanks!!

My assignment requires that I use jquery only. Doesn't seem practical but I'm limited. I was able to figure out how to do a hover state but when the styles get applied, it stays. So check this out..

$(".cta-button").css({
      background: "#476887",
      "text-align": "center",
      width: "173px",
      height: "40px",
      margin: "62px auto 33px",
      cursor: "pointer"
    });
    $(".cta-button").hover(function() {
      $(this).css("background-color", "#509de5");
    });

Of course when I'm no longer hovering, I want it to revert back to it's original background color. How do I go about doing this? Thanks!!

Share asked Sep 2, 2017 at 0:59 DresDres 1,5093 gold badges23 silver badges36 bronze badges 5
  • 2 api.jquery./hover – Scott Simpson Commented Sep 2, 2017 at 1:03
  • 2 Did you read the documentation at all? hover() accepts two functions, one for the mouse entering, and one for the mouse leaving. – adeneo Commented Sep 2, 2017 at 1:17
  • Read documentation of libraries.... – epascarello Commented Sep 2, 2017 at 1:21
  • Use $(this).addClass(...) rather than .css() in the hover() function, then you can easily removeClass() on either the second arg of hover or mouseout(). – fdomn-m Commented Sep 2, 2017 at 1:34
  • Possible duplicate of: stackoverflow./questions/9480968/… – Aref Ben Lazrek Commented Sep 2, 2017 at 1:34
Add a ment  | 

2 Answers 2

Reset to default 7

You can easily achieve that by using the hover method of jQuery

As stated in the docs, this method can accept one to two arguments, the first one is called the handlerIn which can be translated as the hover state, mouse enter, etc and the handlerOut which corressponds to the 'mouse leave'

So to achieve what you want you can try something like this

$('DOM_NODE').hover(mouseEnter, mouseLeave);
function mouseEnter() {
     // do something when the mouse enters the dom node 
};
function mouseLeave() {
     // do something when the mouse leaves the dom node
};

You can add mouseover event
a fiddle

$(".cta-button").css({
      background: "#476887",
      "text-align": "center",
      width: "173px",
      height: "40px",
      margin: "62px auto 33px",
      cursor: "pointer"
    });
    $(".cta-button").hover(function() {
      $(this).css("background-color", "#509de5");
    }).mouseover(function() {
      $(this).css("background-color", "#476887");
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744930926a4601732.html

相关推荐

  • javascript - How to reverse hover state with jQuery? - Stack Overflow

    My assignment requires that I use jquery only. Doesn't seem practical but I'm limited. I was

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信