简体   繁体   中英

Why can't I get the proper y position in JavaScript?

When I run the function spin() for the first time, it works. I get the y coordinate of the image called num , which is 470. In every run that follows, it stays 470, even though you can see the image move!

<p id="demo">Click the button to display the y position.</p>
<div style="position:absolute; top:450px; width:300px; height:140px; overflow:hidden; background-color:black;">
    <div style="position:relative; top:20px; left:30px; width:80px; height:100px; overflow:hidden; background-color:white;">
        <img id="numbers" src="numbers.png" style="position: absolute; left: 8px;">
    </div>
    <button id="spinButton" style="position: relative; left: 200px; width: 80px;" onclick="spin();">SPIN</button>
</div>
var num = document.getElementById("numbers");
var spn_btn = document.getElementById("spinButton");

function spin() {
    //generate a random offset value divisible by 100
    var newNumber = (randomNumber() * 100) + 300;

    //Debug
    document.getElementById("demo").innerHTML = num.y;

    TweenMax.to(num, 2, {
        y: -newNumber
    });
}

function randomNumber() {
    var rand = Math.floor(Math.random() * 10);
    return rand;
}

(Uses TweetMax )

It looks like the problem is with this line:

TweenMax.to(num, 2, {y:-newNumber});

I'm not familiar with this library so I don't know if it's doing what it's supposed to. Also I don't see the image popping up on my screen anywhere. Can you expand on what the desired functionality is?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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