JavaScript/jQuery 鼠标悬停问题
我希望当鼠标悬停在图像上时,应该触发一次事件,并且只有在鼠标离开该图像并再次返回后,并且至少经过 2 秒后,才应该再次触发该事件。 如果我将鼠标悬停在图像
<img src="images/reficon.png" onmouseover="refreshcash()" onmouseout="normalimg()" id="cashrefresh"/>
function refreshcash() {
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function normalimg() {
$("#cashrefresh").attr("src","images/reficon.png");
}
代码更新 上,我当前的函数将被连续调用(refreshcash
) 这段代码似乎有一个错误,但算法有点逻辑
<script type="text/javascript">
var canhover = 1;
var timeok = 1;
function redotimeok() {
timeok = 1;
}
//
function onmenter()
{
if (canhover == 1 && timeok == 1)
{
enter();
canhover = 0;
}
}
//
function onmleave()
{
leave();
canhover = 1;
setTimeout(redotimeok(), 2000);
leave();
}
//
$('#cashrefresh').hover(onmenter(),onmleave());
function enter(){
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function leave(){
$("#cashrefresh").attr("src","images/reficon.png");
}
</script>
I want that when mouse is over an image, an event should be triggered ONCE, and it should be triggered again only after mouse is out of that image and back again, and also at least 2 seconds passed.
My current function is called continuously (refreshcash
) if I leave the mouse over my image
<img src="images/reficon.png" onmouseover="refreshcash()" onmouseout="normalimg()" id="cashrefresh"/>
function refreshcash() {
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function normalimg() {
$("#cashrefresh").attr("src","images/reficon.png");
}
code update
This code seems to have a bug,but the algorithm is kinda logical
<script type="text/javascript">
var canhover = 1;
var timeok = 1;
function redotimeok() {
timeok = 1;
}
//
function onmenter()
{
if (canhover == 1 && timeok == 1)
{
enter();
canhover = 0;
}
}
//
function onmleave()
{
leave();
canhover = 1;
setTimeout(redotimeok(), 2000);
leave();
}
//
$('#cashrefresh').hover(onmenter(),onmleave());
function enter(){
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function leave(){
$("#cashrefresh").attr("src","images/reficon.png");
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
悬停
:您的图像应如下所示:
更新:
修改您的代码,如下所示:
Try the
hover
:And your image should look like:
Update:
Modify your code like this:
您是否以某种方式缓存图像?如果您用
src
属性替换它们,而没有在其他地方指定width
/height
(最好是 CSS)或让它们随时可用,则悬停框(img
元素)将折叠成一个较小(或没有)的框,直到图像加载得足够远,浏览器知道图像的正确尺寸以调整框的大小(这可能会影响其他元素)正在根据图像进行调整)。确切的效果取决于浏览器,但您可能会失去悬停状态,从而导致调用 mouseout 函数。我假设两个图像的大小相同,所以如果您还没有这样做,您可以尝试将尺寸添加到
#cashrefresh
的 CSS 中,看看是否可以解决问题。对于延迟,我建议使用 jQuery 计时器插件 (或类似的插件),它可以简化处理与自己做相比,计时器的数量。您可能想要为计时器命名,并在添加下一个计时器之前尝试停止较旧的计时器。
Do you have the images cached in some way? If you replace them by their
src
attribute without specifyingwidth
/height
elsewhere (best would be CSS) or having them readily available then the hovered box (img
element) will collapse into a smaller (or no) box until the image has been loaded far enough for the browser to know the correct dimensions of the image to resize the box (which may affect other elements being adjusted to the image). The exact effect depends on the browser but you may lose the hover state causing the call of your mouseout function.I assume that both images are the same size, so if you didn't already, you could try adding the dimensions to your CSS for
#cashrefresh
and see if that fixes the problem.For the delay I would recommend using the jQuery timers plugin (or a similar one) which eases handling of timers compared to doing it on your own. You would probably want to give your timers names and try to stop older ones before you add the next one.