悬停图像会自动悬停出去吗?

时间:2013-07-11 07:05:47

标签: jquery hover rollover

按钮是顶部的六个方形图像。

http://wthdesign.net/test/rollover/services.html

我使用的代码:

$( document ).ready(function() {

$('.originalImg,rollOverImg').hover(

  function () {
    $(this).next().stop().animate({left: '0'}, 500, function(){});
    console.log("in");
  },
  function () {
    $(this).next().stop().animate({left: '90px'}, 500, function(){});
    console.log("out");
  }

);

我的HTML:

<div class="slicesBoxesTop"><img class="originalImg" src="images/home.png" /><img class="rollOverImg" src="http://makingdesign.com.au/images/mickslice1.gif" width="90" height="90" /></div> 
<div class="slicesBoxTop2"><img  class="originalImg" src="images/home.png" /><img class="rollOverImg" src="http://makingdesign.com.au/images/paulslice2.gif" width="90" height="90" /></div>
<div class="sliceTop3"><img  class="originalImg" src="images/home.png" /><img class="rollOverImg" src="http://makingdesign.com.au/images/mickslice2.gif" width="90" height="90" /></div>
<div class="sliceTop4"><img  class="originalImg" src="images/home.png" /><img class="rollOverImg" src="http://makingdesign.com.au/images/paulslice3.gif" width="90" height="90" /></div>
<div class="sliceTopbox5"><img  class="originalImg" src="images/home.png" /><img class="rollOverImg" src="http://makingdesign.com.au/images/mickslice3.gif" width="92" height="92" /></div>

出于某种原因,即使我的光标仍悬停在图像上,它也会自动滚动。

2 个答案:

答案 0 :(得分:1)

首先,你的选择器是错误的:

$('.originalImg,rollOverImg').hover

应该是:

$('.originalImg,.rollOverImg').hover

其次,当您调用动画时,'rollOverImg'与'originalImg'重叠,导致您的悬停事件的“鼠标移出”部分被触发。你想在你原来的&amp;翻转图片:

$(".originalImg").mouseenter(function() {
  $(this).next().stop().animate({left: '0'}, 500, function(){});
});
$(".rollOverImg").mouseleave(function() {
  $(this).prev().stop().animate({left: '90px'}, 500, function(){});
});

答案 1 :(得分:1)

问题:你有两个选择器在悬停... $('.originalImg,.rollOverImg')所以当你将鼠标移动到rollOverImg时...会调用origninalImg mouseleave函数..

溶液,

将所有div命名为同一个类,并使用hover进行整个<div>。未经测试但这应该有效。

试试这个

<强> HTML

<div class="slicesBoxesTop divClass"><img class="originalImg"..
<div class="slicesBoxTop2 divClass"><img class="originalImg" ...
 ..so on

<强> jquery的

$( document ).ready(function() {

$('.divClass').hover(
   function () {
       $(this).find('.rollOverImg').stop().animate({left: '0'}, 500, function(){});
     console.log("in");
   },
   function () {
     $(this).find('.rollOverImg').stop().animate({left: '90px'}, 500, function(){});
     console.log("out");
   }

 );