jquery - 使用 jquery、html 和 css 在菜单项上创建颜色淡化效果时出现问题

标签 jquery html css

为了咯咯笑,我正在创建一个免费的图像,但可以互动的网站。在菜单上,我希望每个元素在 mouseover 时淡化为灰色,然后在 mouseout 时淡化回白色。目前,所有菜单项都会同时淡出。对于许多检查我的代码的人来说,您一定会很快明白原因。我对它太陌生了。

网站链接:Zero Img Site

jQuery 的东西:

<head>
    <title>Imageless Website in HTML5, jQuery and CSS</title>
    <link rel="stylesheet" href="css/main.css" type="text/css" />
    <!-- <script type="text/javascript" src="js/CreateHTML5Elements.js"></script> -->
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js"></script>

</head>

<script>
$(document).ready(function(){
        $(".block").mouseover(function(){
        $(".block").animate( { backgroundColor: '#777777' }, 100)
    });
        $(".block").mouseout(function(){
        $(".block").animate( { backgroundColor: 'white' }, 200)
    });
});
</script>

HTML 内容:

<a href="http://www.example.com">
    <div class="block">
        home
    </div>
</a>

<a href="http://www.example.com">
    <div class="block">
        about
    </div>

</a>

<a href="http://www.example.com">
    <div class="block">
        contact
    </div>
</a>

</nav>

最佳答案

您需要更改鼠标悬停函数中的 .block 选择器,以便它们仅作用于您鼠标悬停的元素。您可以使用关键字 this 来执行此操作。

$(".block").mouseover(function(){
    $(this).animate( { backgroundColor: '#777777' }, 100)
});
$(".block").mouseout(function(){
    $(this).animate( { backgroundColor: 'white' }, 200)
});

关于jquery - 使用 jquery、html 和 css 在菜单项上创建颜色淡化效果时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069309/

相关文章:

javascript - 可重用的 Vue.js Ajax 方法。将 Vue 数据数组名称作为参数传递。如何?

javascript - 使用 Javascript DOM 根据条件选择元素

javascript - 页眉 - 页脚包含在另一个 Html 文件中

html - 我的网站在缩放 100%+ 时一团糟

html - rails will_paginate 仅显示当前页面链接

CSS继承问题

jquery - 显示 : inline-block doesn't work on ajax response

html - 删除 Bootstrap 静态导航栏底部不需要的填充

HTML/CSS 在此示例中,是否有更好/更清晰的水平和垂直居中方式?

javascript - 谷歌街景平滑导航