Using Step function in animate to transform-rotate

2019-07-13 11:27发布

All,

I am trying create a 'to & fro' animation using jquery animate & css transform property.

I referred to this SO post on using step function in animate, however, Im not having much success. Please review & advice.

Jsfiddle.net demo

HTML:

<ul id="a">
<li class="cAccessories" id="AccButtons"><span>blah blah..</span></li>
<li class="cAccessories" id="AccRibbons"><span>blah blah..</span></li>
<li class="cAccessories" id="AccLaces"><span>blah blah..</span></li>
<li class="cAccessories" id="AccEmbroider"><span>blah blah..</span></li>
</ul>   

CSS:

#a {
    position: absolute;
    top: 25px;
    left: 25px;
    width: 150px;
    height: 150px;
    padding: 0;
    margin: 0;
    list-style: none;
    background-color: blue;
}

JAVASCRIPT:

$("#a").animate ({"margin-left": "+=10px"}, {step: function (now, fx) {
$("#a").css ("-moz-transform", "rotate('5deg')");
$("#a").css ("-webkit-transform", "rotate ('5deg')");
$("#a").css ("-ms-transform", "rotate ('5deg')");
$("#a").css ("-o-transform", "rotate ('5deg')");
}, duration: "slow"}, "linear", function ()
{
$("#a").animate ({left: "+=0px"}, {step: function (now, fx) {
$("#a").css ("-webkit-transform", "rotate ('-5deg')");
$("#a").css ("-moz-transform", "rotate ('-5deg')");
 $("#a").css ("-ms-transform", "rotate ('-5deg')");
$("#a").css ("-o-transform", "rotate ('-5deg')");
}, duration: "slow"}, "linear" ); });

Following is the original code, which I doesn't like the transform animation (even though it animates the 'top' value perfectly).

iTemplateNo = $(this).attr("id").substr (-1, 1);
eCurHanger = document.getElementById("hanger0" + iTemplateNo);
$(eCurHanger).attr("src", "Img/V2/Hanger.png");
$(eCurHanger).css ({"width": "45px", "height": "35px"});
$(eCurHanger).animate ({top: "+=10px"}, {step: function (angle, fx) {
$(eCurHanger).css ({"-webkit-transform": "rotate ("+angle+"deg)",
      "-moz-transform": "rotate ("+angle+"deg)",
      "-ms-transform": "rotate ("+angle+"deg)",
      "-o-transform": "rotate ("+angle+"deg)"});
   }, duration: 1000}, "linear", function ()
   {
    $(eCurHanger).animate ({left: "+=10px"}, {step: function (angle, fx) {
    $(this).css ({"-webkit-transform": "rotate("+angle+"deg)",
    "-moz-transform": "rotate("+angle+"deg)",
    "-ms-transform": "rotate("+angle+"deg)",
    "-o-transform": "rotate("+angle+"deg)"})
   }, duration: 1000}, "linear" ) });

2条回答
干净又极端
2楼-- · 2019-07-13 12:16

I do not know what eCurHanger is, but look here http://jsfiddle.net/8tP9D/

var angle = 0;
$("#a").animate ({"margin-left": "+=200px"}, {step: function (now, fx) {
    angle += 1;
    $(this).css ({"-moz-transform":"rotate("+angle+"deg)",
                  "-webkit-transform":"rotate("+angle+"deg)",
                  "-ms-transform":"rotate("+angle+"deg)",
                  "-o-transform":"rotate("+angle+"deg)"});
}, duration: 5000}, "linear");

instead of global angle you could use now variable. http://jsfiddle.net/39pe6/

$("#a").animate ({"margin-left": "+=200px"}, {step: function (angle, fx) {
    $(this).css ({"-moz-transform":"rotate("+angle+"deg)",
                  "-webkit-transform":"rotate("+angle+"deg)",
                  "-ms-transform":"rotate("+angle+"deg)",
                  "-o-transform":"rotate("+angle+"deg)"});
}, duration: 5000}, "linear");
查看更多
Juvenile、少年°
3楼-- · 2019-07-13 12:17

Researched a bit. There were certain syntax errors. Here is the output you require I think Click Here!.

The code working with me is here:

$("#a").animate({ textIndent: 5 }, {
        step: function(now,fx)
        {
            $(this).css({
                '-webkit-transform':'rotate('+now+'deg)',
                'transform':'rotate('+now+'deg)',
                '-ms-transform':'rotate('+now+'deg)'});
        },duration:1000,
complete:function() {
  $("#a").animate({ textIndent: -5 }, {
        step: function(now,fx)
        {
            $(this).css({
                '-webkit-transform':'rotate('+now+'deg)',
                'transform':'rotate('+now+'deg)',
                '-ms-transform':'rotate('+now+'deg)'});
        },duration:1000
});
}});
查看更多
登录 后发表回答