How to get image to pulse with opacity with JQuery

2019-06-11 00:07发布

I am trying to get an image to change opacity smoothly over a duration of time. Here's the code I have for it.

<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>

Also, is there a way for this to happen automatically without the need of a mouseover? Thanks.

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-11 00:48

To fire the animation of your own accord:

pulsate( $('#waterloo') );

revised code to continually pulsate (wasn't sure if this was what you're after) - the pulsate effect is relegated to it's own function so you can call it directly or in your event handler

<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>

Note - to trigger events, you can use .trigger() or the helper functions, like

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

or

$('#waterloo').trigger('mouseover');
查看更多
一夜七次
3楼-- · 2019-06-11 00:51

I personally do something like this to pulse when the mouse hovers over the image and return to full opacity on mouse out...

$(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
    });
});
查看更多
手持菜刀,她持情操
5楼-- · 2019-06-11 01:05

I'm assuming your code is for the jQuery pulse plugin: http://james.padolsey.com/javascript/simple-pulse-plugin-for-jquery/

If your above code is not working, then fix "jquery" to be "jQuery".

For starting it on page load, just do:

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!");
    }
});

Add an id to your image and you're golden. });

查看更多
登录 后发表回答