jQuery animating along a sine wave

前端 未结 6 1742
-上瘾入骨i
-上瘾入骨i 2021-01-20 01:02

I\'ve spent a couple days at this and I give up.

I\'m trying to get an object to animate along a sine wave infinitely. It should not end after the first period.

6条回答
  •  野的像风
    2021-01-20 01:20

    When I comment out this line:

    setTimeout(function(){float(!dir)}, 0);
    

    the element stops motion precisely on the line marked It skips here.

    It appears that when you reset the motion to // avoid exceeding stack it resets the position of the element to to y=0, while preserving the x value of the element as well as its path of motion.

    This hypothesis is further validated in that when ever a skip occurs (anywhere on the y axis) the element always resumes its motion from y=0. Sometimes its y value is > y = 0 while sometimes it is < y = 0 -- thus the random looking "skipping around."

    Edit

    Going back to the source sine demo, it seems you can get near infinite scrolling, by manipulating the x= ... line. After some looking at the original source, it appears that the demo script was only written to accommodate that one specific example and fixed width problems.

    Here is a working example.

    By manipulating the numbers on line 1 and 2 you can specify the number of pixels for the path to traverse, and the slow the path down on line 3 to make it the same speed as the original demo. So, not mathematically infinite, but it took a good 45 seconds to complete on my computer. By manipulating these specific lines you can make it as "infinite" as you need.

    window.SineWave = SineWave = function() {
        this.css = function(p) {
            s = Math.sin((p-1)*500);  // 1
            x = (5000 - p*5000) * 10; // 2
            y = s * 100 + 150;
            return {top: y + "px", left: x + "px"};
        } 
    }
    
      $("#nyan").stop().animate(
            {path: new SineWave}, 
            50000, // 3
            "linear"
      );
    

提交回复
热议问题