jquery - 使用 jQuery 更改 Div 颜色的问题

标签 jquery html css

好的,这就是我得到的:

<!DOCTYPE html>
<html>
<head>
    <title>The Down-Champlain Regatta</title>
    <link rel="stylesheet" href="homepage.css"/>
    <link href='http://fonts.googleapis.com/css?family=Poiret+One' rel='stylesheet' type='text/css'>
    <script src="jquery-1.11.2.js"></script>
    <script src="jquery-ui.js"></script>
</head>
<body>
    <div id="topcontainer">
        <img src="Images/Kim.jpg" id="tom">
        <img src="Images/Kim.jpg" id="zach">

        <div id="head">
            <p id="a">Hello South Burlington. We're the</p>
            <h5>Down Champlain Regatta.</h5>
            <p id="s">And we've got a bold new plan for sailing education on Lake Champlain.</p>
        </div>

        <div id="down">
            <p>check it out</p>
        </div>
    </div>

    <div id="container">
        <h5>Here's What We're Doing</h5>
        <p class="beginning">The most important thing in sailing is experience. Read all the books you want - you'll still need time on the water.</p>
        <p>The Down Champlain Regatta is a non-profit organization designed to give students, if nothing else, tons of time on the water. Its three weeks of all-weather keelboat sailing on Lake Champlain, something not offered in many other places, will teach students more than anything else can.</p>
        <p>This course is not for new sailors. It's for kids who know how to sail, but want to take it to the next level. It's for kids who want to move up into the world of keelboat racing.</p>
        <p>And at the end of the course, they do just that. The course finishes with an all-day, student-led race down Lake Champlain. This is a unique opportunity for the students to apply their newly learned skills, build confidence, and get exposure in the racing community. </p>

    </div>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#down").mouseover(function(){
                $("#down").animate({backgroundColor: "#1363bf"}, 2000)
            });

            $("#down").mouseout(function(){
                $("#down").animate({backgroundColor: "#e03535"}, 250)
            });

            $("#down").click(function(){
                $("html, body").animate({
                    scrollTop: $("#container").position().bottom+"px"
                }, 800)
            });
        });
    </script>
</body>
</html>

基本上,有一个 div,我希望当您将鼠标悬停在它上面时它会改变颜色。

看,问题是 .mouseover 正在检测 div 和其中的段落(“检查它”)。因此,当您将鼠标悬停在 div 和段落上时,它会经历两次颜色变化。有没有办法解决?也许使用 .mouseover 以外的东西?

这是 dwreck08 的更新代码:

    $("#down").mouseover(function(){
        $("#down").not("p").animate({backgroundColor: "#1363bf"}, 2000)
    });

    $("#down").mouseout(function(){
        $("#down").not("p").animate({backgroundColor: "#e03535"}, 250)
    });

最佳答案

正如@Albin 的评论所述,我还建议使用 css3 转换来实现这一点。如果愿意,您仍然可以使用 jquery 来切换它:JS Fiddle

 $("#down").mouseover(function () {
     $("#down").css('background', 'blue');
 });

$("#down").mouseout(function () {
     $("#down").css('background', 'red');
 });


#down {
    transition: 1s;
}
#down:hover {
    transition: 2s;
}

关于jquery - 使用 jQuery 更改 Div 颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013677/

相关文章:

javascript - jQuery:WAITING窗口加载时忽略第 3 方内容

javascript - stackoverflow 列表中有趣的元素背景颜色 - 强制更改

java - 在 GAE Java 中创建自定义用户数据库

javascript - 过渡到页面上的 anchor

javascript - jqGrid 在 RTL 方向的错误滚动位置

javascript - Kendo UI 模板加载隐藏元素

javascript - Jquery 不透明度切换

html - 防止卡片在卡片组中扩展

javascript - 使用 jQuery、javascript 满足以下要求的图像轮播

php - 如何在 friend 邀请的Facebook发送对话框消息框中添加默认消息