简体   繁体   English

使用javascript在页面上随机移动div

[英]Moving div randomly around the page with javascript

I am making a game with Javascript and Jquery where the player can buy fish and they swim around in their tank. 我正在用Javascript和Jquery制作游戏,玩家可以购买鱼,然后在鱼缸里游泳。 I want <div id="goldfish"><img src="fish/goldfish_l.gif"/></div> to move around inside of <div id="container"> I also want to change the img src to fish/goldfish_r when the fish moves right. 我想<div id="goldfish"><img src="fish/goldfish_l.gif"/></div><div id="container">内部移动,我也想将img src更改为fish / goldfish_r当鱼向右移动时。 So far I have tried: 到目前为止,我已经尝试过:

function moveDiv() {
    var $span = $("#goldfish");

    $span.fadeOut(1000, function() {
        var maxLeft = $(window).width() - $span.width();
        var maxTop = $(window).height() - $span.height();
        var leftPos = Math.floor(Math.random() * (maxLeft + 1))
        var topPos = Math.floor(Math.random() * (maxTop + 1))

        $span.css({ left: leftPos, top: topPos }).fadeIn(1000);
    });
};
moveDiv();setInterval(moveDiv, 5000);

but that just makes it disappear then reappear somewhere else. 但这只会使其消失,然后重新出现在其他地方。

You were close. 你近了 You don't need to fade out and the fade in and you can simply use the callback of the function to loop. 您无需淡出和淡入,您只需使用函数的回调即可循环。

Fiddle . 小提琴

function AnimateIt() {
    var theDiv = $("#the-div"),
        theContainer = $("#container"),
        maxLeft = theContainer.width() - theDiv.width(),
        maxTop = theContainer.height() - theDiv.height(),
        leftPos = Math.floor(Math.random() * maxLeft),
        topPos = Math.floor(Math.random() * maxTop);

    if (theDiv.position().left < leftPos) {
        theDiv.removeClass("left").addClass("right");
    } else {
        theDiv.removeClass("right").addClass("left");
    }

    theDiv.animate({
        "left": leftPos,
        "top": topPos
    }, 1200, AnimateIt);
}
AnimateIt();

As you can see, the background changes when going either left or right. 如您所见,向左或向右移动时背景会发生变化。 I've done this with a class, so you could easily change the background-image, but if you really want to change the source you can do this: 我已经在一个类中完成了此操作,因此您可以轻松更改背景图像,但是如果您确实想更改源代码,则可以执行以下操作:

Fiddle . 小提琴

function AnimateIt() {
    var theDiv = $("#the-div"),
        theContainer = $("#container"),
        maxLeft = theContainer.width() - theDiv.width(),
        maxTop = theContainer.height() - theDiv.height(),
        leftPos = Math.floor(Math.random() * maxLeft),
        topPos = Math.floor(Math.random() * maxTop),
        imgRight = "http://f00.inventorspot.com/images/goldfish.jpg",
        imgLeft = "http://2.bp.blogspot.com/-F8s9XEIBSsc/T41x37x9w1I/AAAAAAAAB9A/cDfFJLCERII/s1600/Goldfish-1600x1200.jpg";

    if (theDiv.position().left < leftPos) {
        theDiv.attr("src", imgRight);
    } else {
        theDiv.attr("src", imgLeft);
    }

    theDiv.animate({
        "left": leftPos,
        "top": topPos
    }, 2000, AnimateIt);
}
AnimateIt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM