jQuery 悬停在一个 div 上

标签 jquery html css

我是 jQuery 的新手,我真的无法理解这个问题。

我正在尝试选择 <a href="contact"><div id="res">Reserveren</div></a> .

这是我的 jQuery 代码:

 $(document).ready(function() {

 $('#res').hover(
    function () {
    $(this).animate({backgroundColor: "#fff" });
    },
    function () {
    $(this).animate({backgroundColor: "#000" });
    }

  });

但是,没有任何效果。它根本没有改变任何东西……我做错了什么?

提前致谢, 巴特

最佳答案

jQuery 不会,不能,在不使用 a suitable plug-in 的情况下为 color 设置动画。 ,或 jQuery UI library .

但是,您可以使用类名来利用 CSS 转换:

$('#res').hover(
    function () {
        $(this).addClass('animating');
    },
    function () {
        $(this).removeClass('animating');
    });

使用以下 CSS:

#res { /* default styles */
    background-color: #000;
    -moz-transition: background-color 1s linear;
    -ms-transition: background-color 1s linear;
    -o-transition: background-color 1s linear;
    -webkit-transition: background-color 1s linear;
    transition: background-color 1s linear;
}

#res.animating, /* hovered-class/animating styles */
.animating {
    background-color: #fff;
    -moz-transition: background-color 1s linear;
    -ms-transition: background-color 1s linear;
    -o-transition: background-color 1s linear;
    -webkit-transition: background-color 1s linear;
    transition: background-color 1s linear;
}

JS Fiddle demo .

关于jQuery 悬停在一个 div 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532752/

相关文章:

jquery - Kendo Grid销毁后如何提示删除成功?

javascript - ajax 方法的范围有何不同

html - Yii2 - 模板主题问题

html - 在水平菜单中,子菜单 Css 无法正常工作

html - 按钮与 div - 使用哪个?

html - 有什么方法可以捆绑 HTML/CSS/IMAGES 吗?

jquery - 向插件添加回调函数(colorPicker)

javascript - 如何在多个标签中添加与类名一起使用的 ID?

php - 如何使用 PHPWord 将 html 标签字符串转换为 word 文档?

javascript - jQuery 没有在幻灯片容器 div 中加载元素