javascript - div 中的图像未在 Javascript 设置的坐标处绘制

标签 javascript css html

我们将 div 的宽度和高度设置为图像的宽度和高度,并为 div 设置背景图像。在给定的坐标处绘制背景图像。 div 中的图像应该适合背景图像的框。我们计算了背景图像中的框起点(通过使用 http://nicodjimenez.github.io/boxLabel/annotate.html ),我们正在相应地设置 div 图像的坐标。但是图像没有在给定的坐标处正确绘制。

<!DOCTYPE html>
<html>
    <head>
        <title>Moving Screen Saver</title>
        <style>
            html, body {
                position: relative;
                height: 100%;
                width: 100%;
                margin: 0px;
            }

            #board {
                position: absolute;
                background-repeat: no-repeat;
                z-index: 99999;
                background-color: transparent;
            }
        </style>
    </head>
    <body>
        <div id="board">
            <img id="poster" src="//:0">
        </div>

        <script>
            var board = document.getElementById('board');
            var poster = document.getElementById('poster');

            let imgPath = 'url(https://s14.postimg.cc/lyv8mdy9d/billboard3.png)';
            let posterPath = 'url(https://s14.postimg.cc/4yccdq0nl/Iron_Man.jpg';

            var img = new Image();
            img.onload = function() {
                board.style.width = this.width + "px";
                board.style.height = this.height + "px";
                board.style.left = (window.innerWidth - this.width) + "px";
                board.style.top = (window.innerHeight - this.height - 20) + "px";
                board.style.backgroundImage = imgPath;
                poster.style.left = (parseInt(board.style.left) + 13) + 'px';
                poster.style.top = (parseInt(board.style.top) + 38) + 'px';
                poster.style.width = '158px';
                poster.style.height = '73px';
                poster.src = 'https://s14.postimg.cc/4yccdq0nl/Iron_Man.jpg';
            }

            img.src = 'https://s14.postimg.cc/lyv8mdy9d/billboard3.png';
            function moveLeft()
        {   
            board.style.left = board.style.left || 0;

            if(parseInt(board.style.left) <= -(parseInt(board.style.width)))
            {
                board.style.left = (window.innerWidth - parseInt(board.style.width)) + "px";
            }

            board.style.left = parseInt(board.style.left) - 2 + 'px';
            poster.style.left = (parseInt(board.style.left) + 13) + 'px';
            poster.style.top = (parseInt(board.style.top) + 38) + 'px';
            requestAnimationFrame(moveLeft);
        }

        moveLeft();

        </script>
    </body>
</html>

下面是 jsfiddle。

https://jsfiddle.net/vyv5p04v/5/

任何人都可以让我知道代码中有任何问题。

下面是截图。

enter image description here

最佳答案

尝试在负载处理程序中设置 poster.style.position = "absolute" 或 "relative"。默认定位是静态的。

关于javascript - div 中的图像未在 Javascript 设置的坐标处绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49912062/

相关文章:

javascript - 调整窗口大小/最大化窗口时背景/内容下半部分消失

javascript - 数组 forEach 传递 "push"作为参数

javascript - 一旦 Rx.Observable 中发生错误,ValueChanges 就会停止工作

javascript - 两个相邻的 50% 宽度/等高字段集的正确 HTML 对齐/大小低于 100% 宽度?

javascript - 如何在x轴上只显示工作日?

JQuery 选择器取决于一个 div,当我滑动 img 时它会添加一个类

javascript - 使用 jquery 动态菜单背景颜色更改

css - Angular CSS 动画不起作用

html - 半 Angular 内联 block 元素导致奇怪的位置问题

jquery - 绝对 div 停止重叠 - 让它们相对移动