1

我是 javascrip 的新手,我想在页面中制作一个带有雨效果的 div,我做了一些东西,但我不知道如何让它移动,它在我的 div 中随机绘制蓝点,我希望它们下去,这是我的代码:

<html>
<head>

<style>
.punct
{
background-color:blue;
position:absolute;
width:2px;
height:6px;
}
</style>

<script type="text/javascript">


var cleft;
var ctop;

var x=document.getElementById ('content');
function strop (cleft,ctop,d)
{
if (x==null) x="<div class='punct' style='top:"+ctop+"px;left:"+cleft+"px'></div>";
else x=x+"<div class='punct' id='"+d+"' style='top:"+ctop+"px;left:"+cleft+"px'>    </div>";
document.getElementById ('content').innerHTML=x;
}

function randomFromInterval(from,to)
{
return Math.floor(Math.random()*(to-from+1)+from);
}

var y=30;
function start ()
{
if (y!=0){
var a;
var b;
cleft=a;
ctop=b;
a=randomFromInterval (20,1000);
b=randomFromInterval (10,50);
strop(a,b,y);
setTimeout (function () {start ()},500);
y--;
}
}


</script>

</head>
<body>
<div id='content' style='border:2px solid black; height:500px; width:1000px;'></div>
<button onclick='start()'>Start </button>
</body>
</html>
4

5 回答 5

3

另一个仅限 javascript 的解决方案。这使得水滴像在原始帖子中一样缓慢出现,并在它们到达底部时去除水滴。http://jsfiddle.net/35h2Q/4/

function strop(cleft, ctop, d) {
    var drop = document.createElement('div');
    drop.className = 'punct';
    drop.style.left = cleft + 'px';
    drop.style.top = ctop + 'px';
    drop.id = d;
    document.getElementById('content').appendChild(drop);
}

function randomFromInterval(from, to) {
    return Math.floor(Math.random() * (to - from + 1) + from);
}
var n, interval;

function newDrop() {
    var x = randomFromInterval(20, 480),
        y = randomFromInterval(10, 50);
    strop(x, y, n);
    n--;
    if (n > 0) {
        setTimeout(newDrop, 500);
        // 500ms is the interval between drops appearing
    }
}

function start() {
    n = 30;
    newDrop();
    interval = setInterval(function() {
        var drops = document.getElementsByClassName('punct'),
            newY;
        if (drops.length == 0) {
            clearInterval(interval);
            return;
        }
        for (var i = 0; i < drops.length; i++) {
            newY = drops[i].offsetTop + 2;   
                 // drops move by 2px in each frame
            if (newY > drops[i].parentNode.offsetHeight) {
                drops[i].parentNode.removeChild(drops[i]);
            }
            else {
                drops[i].style.top = newY + 'px';
            }
        }
    }, 30);   // 30ms is the interval between drops moving (frame rate)
}​
于 2012-12-05T10:57:19.303 回答
1

仅 Javascript 解决方案 \o/

        <script type="text/javascript">
var cleft;
var ctop;

var x=document.getElementById ('content');
function strop (cleft,ctop,d)
{
    if (x==null) x="<div class='punct' id='"+d+"' style='top:"+ctop+"px;left:"+cleft+"px'></div>";
    else x=x+"<div class='punct' id='"+d+"' style='top:"+ctop+"px;left:"+cleft+"px'></div>";

    document.getElementById ('content').innerHTML=x;
}

function randomFromInterval(from,to)
{
    return Math.floor(Math.random()*(to-from+1)+from);
}

var y=130;
var speed = 2;

function start ()
{
    if (y!=0){
        var a;
        var b;
        cleft=a;
        ctop=b;
        a=randomFromInterval (20,1000);
        b=randomFromInterval (10,500);
        strop(a,b,y);
        y--;
    }

    // Move existing droplets
    for (var i=1; i<=130; i++)
    {
        var el = document.getElementById(i.toString());
        if (el !== null)
        {
            var tp = parseInt(el.style.top) + speed + i*.0125;
            if (tp > 500) 
                tp -= 500;
            el.style.top = tp + "px";
        }
    }

    setTimeout (function () {start ()},10);
}

</script>
于 2012-12-05T10:25:32.087 回答
0

可以使用jQuery animate,对顶部位置进行动画,div使效果稍微好一点。

示例代码:

<script>   
        var cleft;
        var ctop;

        var x=document.getElementById ('content');
        function strop (cleft, ctop, d)
        {
            if (x==null) x="<div class='punct' style='top:"+ctop+"px;left:"+cleft+"px'></div>";
            else x=x+"<div class='punct' id='"+d+"' style='top:"+ctop+"px;left:"+cleft+"px'>    </div>";
            document.getElementById ('content').innerHTML=x;        
        }

        function randomFromInterval(from, to)
        {
            return Math.floor(Math.random()*(to-from+1)+from);
        }

        var y=30;
        function start ()
        {
            if (y != 0){
                var a;
                var b;
                cleft=a;
                ctop=b;
                a=randomFromInterval (20,1000);
                b=randomFromInterval (10,50);
                strop(a, b, y);
                $("#"+y).animate({"top":"480px"},1000)
                setTimeout (function () {start ()},1100);
                y--;
            }
        }
</script>
于 2012-12-05T10:16:12.423 回答
0

试试这个,http://jsfiddle.net/CBv5K/

<html>
<head>
<style> 
#demo
{
background-color:blue;
width:2px;
height:6px;
position:relative;
animation:rain .5s;
-moz-animation:rain .5s; /* Firefox */
-webkit-animation:rain .5s; /* Safari and Chrome */
-o-animation:rain .5s; /* Opera */
}

@keyframes rain
{
0%   {top:0px;}
10%   {top:50px;}
20%   {top:100px;}
30%   {top:150px;}
40%   {top:200px;}
50%   {top:250px;}
60%   {top:300px;}
70%   {top:350px;}
80%   {top:400px;}
90%   {top:4500px;}
100%   {top:500px;}
}

@-moz-keyframes rain /* Firefox */
{
0%   {top:0px;}
10%   {top:50px;}
20%   {top:100px;}
30%   {top:150px;}
40%   {top:200px;}
50%   {top:250px;}
60%   {top:300px;}
70%   {top:350px;}
80%   {top:400px;}
90%   {top:4500px;}
100%   {top:500px;}
}

@-webkit-keyframes rain /* Safari and Chrome */
{
0%   {top:0px;}
10%   {top:50px;}
20%   {top:100px;}
30%   {top:150px;}
40%   {top:200px;}
50%   {top:250px;}
60%   {top:300px;}
70%   {top:350px;}
80%   {top:400px;}
90%   {top:4500px;}
100%   {top:500px;}
}

@-o-keyframes rain /* Opera */
{
0%   {top:0px;}
10%   {top:50px;}
20%   {top:100px;}
30%   {top:150px;}
40%   {top:200px;}
50%   {top:250px;}
60%   {top:300px;}
70%   {top:350px;}
80%   {top:400px;}
90%   {top:4500px;}
100%   {top:500px;}
}
</style>
</head>
<body>
<div id='demo'></div>
</body>
</html>

我这样做只是为了一滴雨。我使用 CSS 制作动画。记住,你给的分数越多,雨看起来就越平滑。

于 2012-12-05T10:39:16.640 回答
0

我在这里找到了两个例子,希望对你有所帮助:
Special Rain 和 Cloud On Page
Hard Raining on the Page
源代码也可以。您还可以通过在网站内搜索来找到更多相关内容。


正如用户评论的那样,我将来源放在这里。它们是两个做这些事情的 js 文件。

<script type="text/javascript" src="http://htmlfreecodes.com/codes/rain.js">
</script>

并且

<script src="http://javascriptbestcodes.com/codes/cloudandrain.js"></script>
于 2016-08-25T18:53:12.027 回答