没有移动元素从相对位置变为固定位置

时间:2015-04-07 11:16:57

标签: javascript css twitter-bootstrap

我希望“提升”之前相对定位的<div>,以便我可以完全控制其位置。使用引导网格定位div。

为此,我想将样式从“relative”更改为“fixed”,但没有(最初)移动<div>

我试图找到获取相对元素的绝对left属性的方法,允许我在更改到固定位置时简单地设置它,但似乎没有?做一个正确的方法(感觉有点hacky)。

2 个答案:

答案 0 :(得分:2)

使用 Element.getBoundingClientRect() 获取相对于视口的元素坐标。

在此代码段中,单击列表项以将其冻结到位:

&#13;
&#13;
document.addEventListener('click', function(e) {
  var el= e.target;
  if(el.tagName==='SPAN') {
    var br= el.getBoundingClientRect();
    el.style.position= 'fixed';
    el.style.left= br.left+'px';
    el.style.top = br.top +'px';
  }
});
&#13;
body {font: 40px verdana;}
span {position: relative;  color: white; cursor: pointer}
#A {background: #036;}
#B {background: #630;}
#C {background: #063;}
#D {background: #603;}
#E {background: #360;}
&#13;
<ul>
  <li><span id="A">Alpha  </span>
  <li><span id="B">Bravo  </span>
  <li><span id="C">Charlie</span>
  <li><span id="D">Delta  </span>
  <li><span id="E">Echo   </span>
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

以下是您在评论中提到的代码示例:

我首先得到了offset元素的顶部和左侧位置。

然后使用.css() Jquery API设置它。

$(function() {
  var $tobechanged = $("#tobechanged").offset();
  $("#tobechanged").css({
      top : $tobechanged.top+"px",
      left: $tobechanged.left+"px",
      position: "fixed"
  });
});
body {
  height: 30000px;
}

#relative {
  background: red;
  height: 50px;
  width: 100%;
}
#tobechanged {
  height: 100px;
  background: green;
  width: 100%;
  position: relative;
}
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

<body>
<div id="relative">
  
</div>
<div id="tobechanged">
  
</div>

使用纯JS:

window.onload = function() {

  var $tobechanged = document.querySelector("#tobechanged"),
  $tobechangedOffset = $tobechanged.getBoundingClientRect();
  $tobechanged.style.top = $tobechangedOffset.top+"px";
  $tobechanged.style.left = $tobechangedOffset.left+"px";
  $tobechanged.style.position = "fixed";
};
body {
  height: 30000px;
}

#relative {
  background: red;
  height: 50px;
  width: 100%;
}
#tobechanged {
  height: 100px;
  background: green;
  width: 100%;
  position: relative;
}
<body>
<div id="relative">
  
</div>
<div id="tobechanged">
  
</div>