0

我必须为 single 显示两个图像mouseover。因此,当我mouseover到图像时,首先显示图像,然后以 5000 的时间延迟显示图像,以显示相同的悬停。现在mouseout显示原始图像。

我对 JavaScript 和 jQuery 不太熟悉。
有人可以给我一些关于如何做到这一点的想法。

我所做的是,

 $('.image1').mouseover(function() {

    setInterval($(this).removeClass(.image1).addClass('image-over1'),5000);
    $(this).removeClass(.image1).addClass('image-over2');

    });
   $('.image1').mouseout(function() {
  $(this).removeClass('image-over1');  
    $(this).removeClass('image-over2').addClass(item);
    });


   $('.image1').click(function(){
    document.location='index.php?page='index.php'; 
    })
4

4 回答 4

0

首先,我认为您的方法存在问题;如果您在鼠标悬停时从元素中删除“image1”类,则该元素将不会被鼠标悬停的 $(".image1") 选择器匹配。您是否有理由需要删除它?如果你这样做了(即,如果你需要禁用 CSS 中的类上定义的某些内容),是否还有其他可以匹配的选择器?

至于时间延迟,如果你使用的 jQuery 版本大于 1.4,你可以使用 .delay() 函数:

$('.image1').mouseover(function() {
 $(this).addClass('image-over1').delay(5000).addClass('image-over2');
});
于 2010-12-02T06:27:16.147 回答
0

.hover()函数允许您同时指定鼠标悬停/鼠标悬停,并且您需要为setInterval

$('.image1').hover(function(evt) {

  // mouse over function.

  // DOM Element that got the mouseover.
  var target = evt.target; 

  if (target.timer) {
    clearTimeout(target.timer);
    target.timer = null;
  }

  target.timer = setInterval(function() {

       // $(this) will not work here, since 'this' has changed.
       // depending on your css you shouldn't need to remove the '.image1'
       // class, just make sure .image-over1 and .image-over2 are
       // stronger selectors, or occur after .image1
       $('.image1').addClass('image-over2');    

       // at this point your element will be (just guessing <img>, could be
       // anything really:
       // <img class="image1 image-over1 image-over2" .../>

       // it's absolutely fine for the image to have all those classes as
       // long as your css is correct.       

   }, 5000);

    $('.image1').addClass('image-over1');

}, function(evt) {

   // mouse out function.

  // DOM Element that got the mouseout.
  var target = evt.target; 

  if (target.timer) {
    clearTimeout(target.timer);
    target.timer = null;
  }

   $('.image1').removeClass('image-over1');
   $('.image1').removeClass('image-over2');

 });


$('.image1').click(function(){ document.location='index.php?page='index.php'; })
于 2010-12-02T06:27:46.250 回答
0

即使图像是动态生成的,也可以在 PHP 中以编程方式生成动画 gif - 请参阅http://php.net/manual/en/function.imagegif.php

于 2010-12-02T11:49:16.860 回答
0

也许您要创建这些图像的动画 GIF?然后使用类似于此处的代码:http: //www.netmechanic.com/news/vol3/design_no10.htm

于 2010-12-02T11:43:31.587 回答