【问题标题】:Jquery mouseenter mouseleave not working correctlyJquery mouseenter mouseleave 无法正常工作
【发布时间】:2015-04-21 20:14:37
【问题描述】:

当鼠标悬停时,我正在尝试从缩略图中移动 wp 插件的标题,并通过此脚本将它们放在顶部的另一个 div 中:

$(document).ready(function() {

  $(".caption").mouseenter(function() {

    $(".captiondiv").html("");

    var str = $(".caption-title:visible").text();

    $(".captiondiv").prepend(str);

  }).mouseleave(function() {

    $(".captiondiv").html("");

  });
});

效果很好,当我将鼠标缓慢悬停在每个缩略图上时,标题在左上角的视频中正确显示:

https://www.youtube.com/watch?v=2DKDq0RNJM4&feature=youtu.be

如果我快速悬停,脚本会显示错误的字幕,它会留下以前的字幕并添加新的:(

https://www.youtube.com/watch?v=GAK0LhkwUbI&feature=youtu.be

我真的迷路了!请帮忙!

最好的问候!

【问题讨论】:

  • 请也分享您的html代码
  • 是wp上安装的插件,

标签: javascript jquery html css


【解决方案1】:

$("ul li img").hover(
    function(){
        var alt = $(this).attr("alt");
        $("strong").html(alt);
    },
    function(){
        $("strong").html("");
    }
);
ul{
    list-style-type:none
}
ul li{
    float:left  
}
ul li img{
    /* grayscale effect */
    -webkit-filter: grayscale(1); 
       -moz-filter: grayscale(1); 
         -o-filter: grayscale(1); 
        -ms-filter: grayscale(1); 
            filter: grayscale(1);
}
ul li img:hover{
    /* clear grayscale effect */
    -webkit-filter: none; 
       -moz-filter: none; 
         -o-filter: none; 
        -ms-filter: none; 
            filter: none;
    /* set 3s animate */
    -webkit-transition: all .3s ease-in-out;
       -moz-transition: all .3s ease-in-out;
         -o-transition: all .3s ease-in-out;
        -ms-transition: all .3s ease-in-out;
            transition: all .3s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<span>Title:</span>
<strong></strong>

<ul>
    <li><img src='https://cdn3.iconfinder.com/data/icons/blackblue/128/iPhoto.png' alt='1'></li>
    <li><img src='https://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/apps/sodipodi.png' alt='2'></li>
    <li><img src='https://cdn0.iconfinder.com/data/icons/IS_CMS/128/image.png' alt='3'></li>
    <li><img src='https://cdn3.iconfinder.com/data/icons/flatforlinux/128/7-Image%20capture.png' alt='4'></li>
    <li><img src='https://cdn1.iconfinder.com/data/icons/fs-icons-ubuntu-by-franksouza-/128/image-png.png' alt='5'></li>
    <li><img src='https://cdn3.iconfinder.com/data/icons/humano2/128x128/apps/synfig_icon.png' alt='6'></li>
    <li><img src='https://cdn1.iconfinder.com/data/icons/nuvola2/128x128/filesystems/folder_image.png' alt='7'></li>
    <li><img src='https://cdn2.iconfinder.com/data/icons/oxygen/128x128/mimetypes/image-png.png' alt='8'></li>
</ul>

EXAMPLE

【讨论】:

  • 谢谢!但是我想修改的插件使用
    来分组缩略图而不是
      ,这可能是问题吗?
  • 这不是问题。这仅仅是一个预览。 mouseentermouseleave 有问题。看看我是如何自己解决的。我用了hover,给了他两个功能。第一个函数类似于mouseenter,第二个函数类似于mouseleave
  • 我也有同样的悬停问题:(
【解决方案2】:

如果要替换整个标题,请不要使用prepend。 你可以使用$(".captiondiv").html(str);

编辑 问题是元素(".caption-title:visible") 有多个值。使用$(this)元素解决了得到想要的元素。

var str = $(this).find(".caption-title:visible").text();
$(".captiondiv").html(str);

【讨论】:

  • 我用 html() 试过,但不工作,同样的问题 :(
  • 但是,为什么我快速悬停时会出现问题?
  • 可能选择器$(".jig-caption-title:visible")返回的不仅仅是一个元素,因为有一个从彩色到灰度的过渡。没有代码我无法确定。 console.log($(".jig-caption-title:visible")); 会帮你找出错误。
  • 是的,这就是问题所在,但是我如何才能在没有 :visible 的情况下只显示活动缩略图的标题?因为当我删除可见的脚本显示我所有的标题,插件使用这个:
  • 尝试var str = $(this).find(".jig-caption-title:visible").text(); $(".caption-perso").html(str); 仅引用鼠标输入元素。如果jig-caption-title.jig-caption 的子级,它就可以工作
【解决方案3】:

不要使用 'prepend' 方法,而是使用 .html() 方法来设置元素的内容。

【讨论】:

  • Stop() 函数怎么样?
  • stop() 函数在元素上调用,该元素上当前正在运行的动画(如果有)立即停止。这里我们要设置元素的数据。如果可能的话,请您在小提琴中分享代码。
【解决方案4】:

这应该可行:

$(document).ready(function() {

  $(".jig-caption").mouseenter(function() {

    var str = $(".jig-caption-title:visible").text();

    $(".caption-perso").html(str);

  }).mouseleave(function() {

    $(".caption-perso").html("");

  });

});

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签