调整大小时左右移动div



我有一个框,在调整大小时向右移动,直到它以半屏幕宽度消失。

    function move() {
        var x = screen.width - window.innerWidth;
        document.getElementById("logo").style.marginLeft = (x * 2) + "px";
    }
    window.onresize = move;

我希望当盒子碰到窗口边缘时,它能以与向右移动相同的速度逐渐向左移动。

    function move() {
        var x = screen.width - window.innerWidth;
          do {
        document.getElementById("logo").style.marginLeft = (x * 2) + "px";
        } while (window.innerWidth > "850px");
          do {
        document.getElementById("logo").style.marginLeft -= (x * 2) + "px";
        } while (window.innerWidth > "850px");
        }
    window.onresize = move;

上面的代码就好像第二条语句不存在

假设#徽标的宽度为100px;你可以这样做,当#标志碰到屏幕边缘时,它会保留在那里。

function move() {
    var pos = 0;
        x = screen.width - window.innerWidth,
        winWidth = (window.innerWidth/2)-50;
        if (x < winWidth) {
            pos = x*2;
        }
        else {
            blockPos = (winWidth - x);
            pos = parseInt(document.getElementById("logo").style.marginLeft) +blockPos;
        }
        if (pos < 0) pos = 0;
        document.getElementById("logo").style.marginLeft = pos+"px";
    }
window.onresize = move;

最新更新