如何在图像到达页面顶部时使用警报?

JavaScript - How can I use an alert when an image has reached the top of the page?

本文关键字:顶部 图像      更新时间:2023-09-26

我是编程新手,我正试图与火箭进行一场比赛。火箭必须到达页面顶部,当它到达页面顶部时,我想要一个警报出来(?)。做这件事最好的方法是什么?提前感谢!

我编辑了我的帖子,我在我的功能下使用了"if"。但是这行不通。

    function leftArrowPressed() {
        var element = document.getElementById("image1");
        element.style.left = parseInt(element.style.left) - 5 + 'px';
        }
        function rightArrowPressed() {
        var element = document.getElementById("image1");
        element.style.left = parseInt(element.style.left) + 5 + 'px';
        }
        function upArrowPressed() {
        var element = document.getElementById("image1");
        element.style.top = parseInt(element.style.top) - 5 + 'px';
        }
        function downArrowPressed() {
        var element = document.getElementById("image1");
        element.style.top = parseInt(element.style.top) + 5 + 'px';
        }


        function moveSelection(evt) {
            if(parseInt(element.style.top)==0)
            {
                alert("some text there");
            }
            switch (evt.keyCode) {
                case 37:
                leftArrowPressed();
                break;
                case 39:
                rightArrowPressed();
                break;
                case 38:
                upArrowPressed();
                break;
                case 40:
                downArrowPressed();
                break;
                }
            };
    function docReady()
    {
      window.addEventListener('keydown', moveSelection);
    }
if(parseInt(document.getElementById("image1").style.top)<=0)
{
alert("some text there");
}

放在moveSelection(), switch之前