0

我试图让图像在一段时间内平滑地改变不透明度。这是我的代码。

<script type="text/javascript">
pulsem(elementid){
    var element = document.getElementById(elementid)
    jquery(element).pulse({opacity: [0,1]}, 
{    duration: 100, // duration of EACH individual animation    
     times: 3, // Will go three times through the pulse array [0,1]   
     easing: 'linear', // easing function for each individual animation    
     complete: function() {        alert("I'm done pulsing!");    }
})
</script>


<a href="city.htm"><img src="waterloo.png" onmouseover="javascript:pulsem("waterloo")" border="0" class="env" id="waterloo"/></a>

另外,有没有办法让这种情况自动发生而无需鼠标悬停?谢谢。

4

4 回答 4

4

我假设您的代码用于 jQuery 脉冲插件:http: //james.padolsey.com/javascript/simple-pulse-plugin-for-jquery/

如果您上面的代码不起作用,则将“jquery”修复为“jQuery”。

要在页面加载时启动它,只需执行以下操作:

jQuery(function() {
jQuery('#yourImageId').pulse({
    opacity: [0,1]
}, {
     duration: 100, // duration of EACH individual animation
     times: 3, // Will go three times through the pulse array [0,1]
     easing: 'linear', // easing function for each individual animation
     complete: function() {
         alert("I'm done pulsing!");
    }
});

为您的图像添加一个 id,您就是金子。});

于 2010-06-09T18:39:27.443 回答
2

要自动触发动画:

pulsate( $('#waterloo') );

修改后的代码以不断脉动(不确定这是否是您所追求的) - 脉动效果降级为它自己的函数,因此您可以直接调用它或在您的事件处理程序中调用它

<script type="text/javascript">
  $(function() { // on document ready
     $('#waterloo').hover( //hover takes an over function and out function
       function() {
         var $img = $(this);
         $img.data('over', true); //mark the element that we're over it
         pulsate(this); //pulsate it
       },
       function() {
          $(this).data('over', false); //marked as not over
       });
  });

 function pulsate(element) {
    jquery(element).pulse({opacity: [0,1]}, // do all the cool stuff
        {    duration: 100, // duration of EACH individual animation    
             times: 3, // Will go three times through the pulse array [0,1]   
             easing: 'linear', // easing function for each individual animation    
             complete: function() {  
                 if(  $(this).data('over') ){ // check if it's still over (out would have made this false)
                     pulsate(this); // still over, so pulsate again
                 }
         }});
  } 

<a href="city.htm"><img src="waterloo.png" border="0" class="env" id="waterloo"/></a>

注意 - 要触发事件,您可以使用.trigger()或辅助函数,例如

$('#waterloo').mouseover() // will fire a 'mouseover' event

或者

$('#waterloo').trigger('mouseover');
于 2010-06-09T18:42:13.777 回答
1

这可能是您正在寻找的。

http://www.infinitywebcreations.com/2011/01/how-to-create-a-throbbingpulsing-image-effect-with-jquery/

于 2012-04-05T13:52:58.737 回答
1

当鼠标悬停在图像上并在鼠标移出时恢复完全不透明时,我个人会做这样的事情来脉冲......

$(document).ready(function () {

    function Pulse(Target, State) {
        //Every 750ms, fade between half and full opacity
        $(Target).fadeTo(750, State?1:.5, function() {Pulse(Target, !State)});
    }

    $("#ImageId").hover(function () {
        $(this).stop()
        Pulse(this);
    }, function () {
        $(this).stop(false, true).fadeTo(200, 1); //200ms to return to full opacity on mouse out
    });
});
于 2013-08-13T01:32:04.470 回答