jquery .mouseover() and .mouseout() with fade



当用户将鼠标悬停在父div上时,我想用jquery淡入一个div。

我有以下代码:

.HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Explore D&amp;D Creative</title>
        <link rel="stylesheet" href="media/css/explore.css" />
        <script type="text/javascript" src="media/js/jquery.min.js"></script>
        <script type="text/javascript" src="media/js/jquery.custom.js"></script>
    </head>

    <body id="home">
        <!-- BEGIN CONTENT -->
        <div id="content">
            <!-- BEGIN CONTENT TOP SLIDESHOW -->
            <div id="top-slide">
                <div class="wrapper">
                </div>  
                <div id="select">...</div>          
            </div>
            <!-- END CONTENT TOP SLIDESHOW -->

            <!-- BEGIN CONTENT TWITTER -->
            <div id="twitter-main">
                <div class="wrapper">
                    <i class="icon-twitter"></i>
                    <span class="divider"></span>
                    <h2 class="feed">THIS IS SOME TWITTER TEXT</h2>
                    <p class="info">@DandDCreative - SOME TIME AGO</p>
                </div>            
            </div>
            <!-- END CONTENT TWIITER -->

        </div>
        <!-- END CONTENT -->
    </body>
</html>​

.CSS:

/*============================================
    CONTENT
============================================*/
#content {
    min-height:100%;
    margin-top:55px;
    padding-top:10px;
}
/*============================================
    HOME.PHP
============================================*/
#home #content #top-slide {
    background-color:#3D3B37;
    height:300px;
    position:relative;
}
#home #content #top-slide #select {
    height:48px;
    width:100%;
    position:absolute;
    bottom:0;
    background:url(../img/home/bg-slie-select.png) repeat;
    display:none;
}
#home #content #twitter-main {
    background-color:#cccccc;
    height:200px;
    margin:10px 0;
    padding-top:30px;
    text-align:center;
}
#home #content #twitter-main i.icon-twitter {
    background:url(../img/common/social/twitter.png) no-repeat center;
    width:37px;
    height:37px;
    margin:0px auto 20px auto;
    display:block;
}
#home #content #twitter-main span.divider {
    border-top:1px solid #535353;
    height:0;
    width:100px;
    display:block;
    margin:0 auto;
}
#home #content #twitter-main h2.feed {
    margin:40px 0;
}
#home #content #twitter-main p.info {
    font-size:10px;
    color:#666666;
}

和JS:

$(document).ready(function() {

    //HOME.PHP
    $('#top-slide').mouseover(function() {
        ('#select').fadeIn(600);
    });
    $('#top-slide').mouseout(function() {
        ('#select').fadeOut(600);
    });           
});​

此代码在鼠标输入和鼠标输出时显示以下错误:

Uncaught TypeError: Object #select has no method 'fadeIn'

Uncaught TypeError: Object #select has no method 'fadeOut'

我认为这可能与鼠标悬停/鼠标退出方法有关,但也尝试使用单击方法,但它的作用相同。

我可能做了一些愚蠢的事情,但我找不到问题。

这是每个人的JSFIDDLE:http://jsfiddle.net/Ze28y/1/

您错过了处理程序的$

$('#top-slide').bind("mouseenter", function()
{
  $('#select').stop(true).fadeIn(600); //$('#select'), not ('#select')
});
$('#top-slide').bind("mouseleave", function()
{
  $('#select').stop(true).fadeOut(600); //$('#select'), not ('#select')
});

此外,还应在淡入淡出之前先添加一个stop,以防止多个淡入淡出进入队列。而且,由于#select#top-slide的子代,因此您应该使用事件mouseentermouseleave而不是mouseovermouseout。(与此相关)

使用$

 $('#top-slide').mouseover(function() {
    $('#select').fadeIn(600);
});
$('#top-slide').mouseout(function() {
    $('#select').fadeOut(600);
});

你忘记了声明jquery对象

$=">

试试这个,你错过了$

  $('#top-slide').mouseover(function() {
        $('#select').fadeIn(600);
    });
    $('#top-slide').mouseout(function() {
        $('#select').fadeOut(600);
    });

缺少$ 你最好使用悬停。

$('#top-slide').hover(
    function() { $('#select').fadeIn(600); },
    function() { $('#select').fadeOut(600); }
);

最新更新