以编程方式使用Javascript创建Wave

时间:2014-05-01 21:41:01

标签: javascript jquery html css css3

以下是我目前的http://jsfiddle.net/6GEfr/

这有效但我希望它像波浪一样。而不是一个' v'形状,它应该看起来像一个实际的波。你是如何逐步这样做的?

var height = 0;

setInterval(function () {
    $('#container').prepend('<div style="height:' + Math.abs(height++) + '%"></div>');
    height = (height == 100) ? -100 : height;
}, 10);

我的css:

html, body, #outerContainer, #container {
    height:100%;
}
#outerContainer {
    display : table;
}
#container {
    display : table-cell;
    vertical-align:bottom;
    white-space:nowrap;
}
#container > div {
    width:5px;
    background:black;
    display:inline-block;
}

1 个答案:

答案 0 :(得分:11)

只需使用Math.sin()为wave建模。

Updated Example

var i = 5, height = 0;
setInterval(function () {
    $('#container').prepend('<div style="height:' + height + '%"></div>');
    i += 0.05;
    height = 50 * Math.sin(i) + 50;
}, 10);

如果要使波形更平滑,请减小增量值和元素的宽度。这是一个example