Trying to change an image's position and size

2019-08-28 23:55发布

问题:

I'm working with a tennis ball emoji for a submit button, and I'd like to animate it off the screen when the user clicks on it.

emoji http://emojipedia.org/wp-content/uploads/2013/07/160x160xtennis-racquet-and-ball.png.pagespeed.ic.aacvJSaqx9.jpg

I'd like for it maybe to move up the screen and shrink a little bit, and continue off the top of the screen. I suppose for realism it should bounce a little bit before it goes off the screen, but that would probably be a needless complication.

Here's what I've got so far jsfiddle:

(function() {
    $(document).on('click','#save',clicked)
    function clicked(myEvent) {
        myEvent.preventDefault()

        var local = {}
        local.width = ["50px", "swing" ]
        local.height = [ "50px", "swing" ]
    local.top = "-64px"
        $(this).animate(local,"slow")
    }
})()

The problem is that it's not moving up the screen as it's shrinking. I'd like for it to look like you're serving the tennis ball for an ace.

回答1:

You have to position the ball absolutely before running the JavaScript, using CSS:

#save {
    position: absolute;
    top: 100px;
}


回答2:

May be something like this: http://jsfiddle.net/LaRWY/7/

@-webkit-keyframes bounce {
  from, to  {
    top: 0;
    -webkit-animation-timing-function: ease-out;
  }
  50% {
    top: 220px;
    -webkit-animation-timing-function: ease-in;
  }
}
@keyframes bounce {
  from, to  {
    top: 0;
    animation-timing-function: ease-out;
  }
  50% {
    top: 220px;
    animation-timing-function: ease-in;
  }
}

#save{
  -webkit-animation-name: bounce;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-duration: 4.2s;

  animation-name: bounce;
  animation-iteration-count: infinite;
  animation-duration: 4.2s;
  position: absolute;
  top: 100px;
}