javascript - 使用 javascript 移动 div 会导致位置 :relative to position:static 发生意外更改

标签 javascript html css

我正在尝试通过按下按钮向左/向下移动一个 div。它会导致从 position:relative 到 position:static 的意外变化。解释为什么会发生这种情况会很好。

<html>
<body style='border: solid blue 1px; height: 90px'>
<div style='width:900px; height: 80px; position:relative; border: solid green 1px'>
<div id='test' style='position:relative; left:500px; top:0px; border: solid red 2px; width:300px'>
<button id='L' onclick='theButton(this)'>move left</button>
<button id='R' onclick='theButton(this)'>reset</button>
<button id='T' onclick='theButton(this)'>move down</button>
</div>
</div>
<script>

function theButton(x) {
    console.log("x: " + x);
    var aDiv = document.getElementById('test');

    var topPosition = window.getComputedStyle(aDiv).top;  
    console.log(topPosition);
    var topPositionDigitString = topPosition.slice(0,topPosition.length-2); // this strips "px" from the string
    var topPositionNumber = parseInt(topPositionDigitString); // this converts to the numeric we need   

    //var leftPosition = window.getComputedStyle(aDiv).left;
    var leftPosition = aDiv.style.left;
    console.log( leftPosition);
    var leftPositionDigitString = leftPosition.slice(0,leftPosition.length-2);
    var leftPositionNumber = parseInt(leftPositionDigitString);
    console.log("leftPositionNumber: " + leftPositionNumber);

    if(x.id=='L') {
        leftPositionNumber += 50; 
        console.log("leftPositionNumber: " + leftPositionNumber);
        var setleftPosition = 'left:' + leftPositionNumber + 'px';
        aDiv.style = setleftPosition;
        console.log("getComputedStyle(aDiv).left: " + window.getComputedStyle(aDiv).left);
        console.log("aDiv.style.left: " + aDiv.style.left);
    }
    if(x.id=='R') {
        aDiv.style = 'left:0px';
    }
    if(x.id=='T') {
        topPositionNumber += 50;
        var setTopPosition = 'top:' + topPositionNumber + 'px';
        aDiv.style = setTopPosition;
    }
    console.log("x: " + x);
    console.log("topPosition:" + topPosition);
    console.log("getComputedStyle(aDiv).left: " + window.getComputedStyle(aDiv).left);
    console.log("getComputedStyle(aDiv).position: " + window.getComputedStyle(aDiv).position);
}
</script>
</body>
</html>

我已经把代码放在 JSBin https://jsbin.com/vineqeedit?html,js,console,output 以防任何想要使用它的人更方便。

感谢您的帮助,
杰拉德

最佳答案

固定:

function theButton(x) {
  console.log("x: " + x);
  var aDiv = document.getElementById('test');

  var topPosition = window.getComputedStyle(aDiv).top;
  console.log(topPosition);
  var topPositionDigitString = topPosition.slice(0, topPosition.length - 2); // this strips "px" from the string
  var topPositionNumber = parseInt(topPositionDigitString); // this converts to the numeric we need	

  //var leftPosition = window.getComputedStyle(aDiv).left;
  var leftPosition = aDiv.style.left;
  console.log(leftPosition);
  var leftPositionDigitString = leftPosition.slice(0, leftPosition.length - 2);
  var leftPositionNumber = parseInt(leftPositionDigitString);
  console.log("leftPositionNumber: " + leftPositionNumber);

  if (x.id == 'L') {
    leftPositionNumber -= 50;
    console.log("leftPositionNumber: " + leftPositionNumber);
    var setleftPosition = leftPositionNumber + 'px';
    aDiv.style.left = setleftPosition;
    console.log("getComputedStyle(aDiv).left: " + window.getComputedStyle(aDiv).left);
    console.log("aDiv.style.left: " + aDiv.style.left);
  }
  if (x.id == 'R') {
    aDiv.style.left = '500px';
    aDiv.style.top = '0px';
  }
  if (x.id == 'T') {
    topPositionNumber += 50;
    var setTopPosition = topPositionNumber + 'px';
    aDiv.style.top = setTopPosition;
  }
  console.log("x: " + x);
  console.log("topPosition:" + topPosition);
  console.log("getComputedStyle(aDiv).left: " + window.getComputedStyle(aDiv).left);
  console.log("getComputedStyle(aDiv).position: " + window.getComputedStyle(aDiv).position);
}
<body style='border: solid blue 1px; height: 90px'>
  <div style='width:900px; height: 80px; position:relative; border: solid green 1px'>
    <div id='test' style='position:relative; left:500px; top:0px; border: solid red 2px; width:300px'>
      <button id='L' onclick='theButton(this)'>move left</button>
      <button id='R' onclick='theButton(this)'>reset</button>
      <button id='T' onclick='theButton(this)'>move down</button>
    </div>
  </div>
  <script></script>
</body>

无论如何,您的麻烦是因为 position: relative; 是在元素的内联样式中设置的。在你的代码中,你有几个地方可以设置整个 element.style = "string",它用一种在线样式替换了整个内联 css。所以我将其更改为仅使用 element.style.left = newLeft 更改左侧或顶部,然后修复了它。

关于javascript - 使用 javascript 移动 div 会导致位置 :relative to position:static 发生意外更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34198119/

相关文章:

html - 开权后的recentre网站设计 'frame'

javascript - 超时没有发生 - 简单的 Javascript

javascript - Meteor 回调返回未定义

javascript - 将 Storybook/vue 与 SCSS 一起使用

javascript - 点击并按住激活全日历日点击事件所需的一天

html - 如何在 Tumblr 的 Field Notes 主题中更改我的提交按钮文本?

javascript - 无法在spring maven元素中从html访问css和js

javascript - 无法使用 JavaScript 隐藏 HTML div

javascript - 自执行匿名函数返回

javascript - 使用 jquery 向上或向下滑动整个 HTML 表格,不起作用