【问题标题】:jquery images flickeringjquery图像闪烁
【发布时间】:2012-08-22 12:33:48
【问题描述】:

我有一个页面,其中包含几个图像缩略图,它们的不透明度设置为 0.6,直到鼠标悬停在它上面:然后,我将不透明度设置为 1.0,并显示一个带有视频标题的小型浮动 div光标。像这样……

CSS:

#theFloater { background-color: #444; color: #FFF; position: absolute; display: none; font-family: Arial, Helvetica, sans-serif; font-size: .85em; z-index:25;}
#reelList img { height: 20px; display: inline-block; margin: 0; z-index: 1}

jQuery:

$('#reelList li').hover(function(){
    $(this).find('img').animate({opacity: 1.0}, 200, function(){});
    $('#theFloater').html(theTitles[$(this).index()]);
    $('#theFloater').show();
}, function(){
    $(this).find('img').animate({opacity: 0.6}, 200, function(){});     
    $('#theFloater').hide();                            
});

var mouseX;
var mouseY;

$("a img").mousemove(function(e){
    mouseX = e.pageX;
    mouseY = e.pageY;
});

frameRate =  30;
timeInterval = Math.round( 1000 / frameRate );

atime = setInterval(function(){ 
    w = $('#theFloater').width() / 2;
    $('#theFloater').css('left', mouseX - w).css('top', mouseY - 35);
}, timeInterval);

这很好用,除非当光标退出缩略图时,有时,图像会连续几次上下动画不透明度,通常是两次或三次。如果我不显示浮动 div,它会完美运行。出于某种原因,浮动 div 与古怪有关。

有人知道为什么浮动 div 会导致缩略图多次动画吗?

【问题讨论】:

  • 请包含 html。和/或做一个 jsfiddle
  • 我以前从未见过 jsfiddle...我必须检查一下,但与此同时,这是链接:ggfilmz.com/foo/x/index.html

标签: jquery image jquery-animate flicker


【解决方案1】:

我能够让它多次动画的唯一方法是打开鼠标,然后关闭,然后非常快速地重新打开几次然后停止 - 动画继续播放我进行切换的次数(准确的效果)。

我相信您正在寻找的是 jQuery 的 stop() 函数,该函数会停止当前动画(因此它们不会不断增加数量 - 每个新的动画调用都会在运行之前停止之前的动画)。

简而言之,用这个 jQuery 交换 $('#reelList li').hover 函数

$('#reelList li').hover(function(){
    $(this).find('img').stop().animate({opacity: 1.0}, 200, function(){});
    $('#theFloater').html(theTitles[$(this).index()]);
    $('#theFloater').show();
}, function(){
    $(this).find('img').stop().animate({opacity: 0.6}, 200, function(){});     
    $('#theFloater').hide();                            
});

有关stop() 的更多信息,请访问jquery.com api reference section

【讨论】:

  • +1 你也可以链接$('#theFloater').html(theTitles[$(this).index()]).show();。事实上,var $this = $(this); 甚至 $img = $this.find('img'); 贯穿始终也会提高速度和最小化 :)
  • 非常感谢,但是 stop() 没有更正它。 (你可以在网站上看到它更新)让它显示问题的最可预测的方法是非常快速地将光标移入和移出中间缩略图,甚至不会多次。
  • 我正在查看您的 JS (ggfilmz.com/foo/x/rplayer.js),看起来 stop() 根本不在您的代码中?也许你重新更新了它?
  • 好吧,看来我遇到了一些互联网连接问题。在我确定 .stop() 代码已上传后,看起来它毕竟可以工作。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-01-07
  • 2018-12-29
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 2012-05-13
  • 2010-10-29
相关资源
最近更新 更多