2017-09-02 101 views
0

我的任务要求我只使用jquery。看起来不实际,但我是有限的。我能够弄清楚如何做悬停状态,但是当样式被应用时,它仍然存在。所以看看这个..如何用jQuery反转悬停状态?

$(".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"); 
    }); 

当然,当我不再徘徊,我想它恢复到它的原始背景颜色。我如何去做这件事?谢谢!!

+2

https://api.jquery.com/hover/ –

+2

你阅读文档都什么呢? 'hover()'接受**两个**函数,一个用于鼠标进入,另一个用于鼠标离开。 – adeneo

+0

阅读库的文档.... – epascarello

回答

2

就可以轻松实现,通过使用hover method of jQuery

如文档指出,这种方法可以接受一到两个参数,第一个是称为handlerIn,它可以翻译为悬停状态,鼠标输入等,并且其对“鼠标离开”的响应为handlerOut

因此为了实现Ë你想你可以尝试这样的事情

$('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 
}; 
+0

mouseEnter不mouserEnter –

+0

这是所有的文件,我应该先读它。我会花更多时间阅读文档而不是谷歌搜索。谢谢! – Dres

0

您可以添加鼠标悬停事件
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"); 
    });