简体   繁体   English

jquery在mouseenter上显示一个元素

[英]jquery show an element on mouseenter

As the title says i want the element to fade in when the mouse hovers over it. 正如标题所说,当鼠标悬停在它上面时,我希望元素淡入。

<div class="carousel-item">

    <div class="carousel-item-top">
        <img class="img-responsive" alt="a" src="carousel/crsl-img-1.png">
        <div class="carousel-item-top-hover"></div>
    </div>

    <div class="inline-block carousel-item-bottom">
        <h5 class="pull-left">Awesome Design</h5>
        <span class="pull-right share">
            74
            <i class="fa fa-heart"></i>
            <span class="v-sep"></span>
            <i class="fa fa-share"></i>
        </span>
    </div>

</div>

The css for the item i hope to become visible 该项目的CSS我希望变得可见

.carousel-item-top-hover{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #219fd1;
    opacity: 0.85;
    display: none;
}

And one of the javascript variants i was hoping to be able to handle it 我希望能够处理它的一个javascript变种

$(".carousel-item-top").mouseenter(function () {
    $(this)(".carousel-item-top-hover").fadeIn('slow')
});

Note: What you said doesn't reflect your code that you tried (you said you wanted the element to be shown when the mouse is over IT, but the code makes it show when something else is hovered over.) I'm assuming you meant what your code says. 注意:你所说的并不反映你尝试过的代码(你说你希望在鼠标结束时显示该元素,但代码会在其他东西悬停时显示。)我假设你意味着你的代码所说的。

The proper handler is $(el).mouseover() . 适当的处理程序是$(el).mouseover()

$(".carousel-item-top").mouseover(function () {
    $(".carousel-item-top-hover").fadeIn('slow')
});

you almost had it, but you had a superfluous (this) . 你几乎拥有它,但你有一个多余的(this)

You can't hover over something that isn't displayed. 您不能将鼠标悬停在未显示的内容上。 The mouse can't hover over something that isn't there. 鼠标不能悬停在不存在的东西上。 You might want to set visibility to hidden instead of display none. 您可能希望将可见性设置为隐藏而不是显示无。 Then there is still space where you can hover over it. 然后还有空间,你可以将鼠标悬停在它上面。

visibility: hidden;

If that doesn't work, set the opacity to 0 and then animate the opacity to .85 when you mouse enter. 如果这不起作用,请将不透明度设置为0,然后在鼠标输入时将不透明度设置为.85。

Instead of using fadeIn , use fadeTo like this: 而不是使用fadeIn ,使用fadeTo像这样:

$(".carousel-item-top-hover").fadeTo('slow', 1);

Instead of slow, you can also provide an integer for the speed in milliseconds. 您也可以提供速度的整数(以毫秒为单位),而不是慢速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM