javascript - 如何使用 CSS/jQuery 将一个元素相对于另一个元素定位

标签 javascript jquery html css

我尝试将以下代码中心的 title 定位到 circle

<style>
*, *:after, *:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Open Sans";
}


/* Form Progress */
.progress {
  width: 1500px;
  margin: 20px auto;
  text-align: center;
}
.progress .circle,
.progress .bar {
  display: inline-block;
  background: #fff;
  width: 40px; height: 40px;
  border-radius: 40px;
  border: 1px solid #d5d5da;
}
.progress .bar {
  position: relative;
  width: 100px;
  height: 6px;
  top: -33px;
  margin-left: -5px;
  margin-right: -5px;
  border-left: none;
  border-right: none;
  border-radius: 0;
}
.progress .circle .label {
  display: inline-block;
  width: 32px;
  height: 32px;
  line-height: 32px;
  border-radius: 32px;
  margin-top: 3px;
  color: #b5b5ba;
  font-size: 17px;
}
.progress .circle .title {
  color: #b5b5ba;
  font-size: 13px;
  line-height: 30px;
  margin-left: -5px;
}

/* Done / Active */
.progress .bar.done,
.progress .circle.done {
  background: #eee;
}
.progress .bar.active {
  background: linear-gradient(to right, #EEE 40%, #FFF 60%);
}
.progress .circle.done .label {
  color: #FFF;
  background: #8bc435;
  box-shadow: inset 0 0 2px rgba(0,0,0,.2);
}
.progress .circle.done .title {
  color: #444;
}
.progress .circle.active .label {
  color: #FFF;
  background: #0c95be;
  box-shadow: inset 0 0 2px rgba(0,0,0,.2);
}
.progress .circle.active .title {
  color: #0c95be;
}
</style>

这是我的主体元素,带有 titlecircle

<div class="progress">
  <div class="circle active">
    <span class="label">1</span>
    <span class="title">Ticket&nbsp;Requested</span>
  </div>
  <span class="bar"></span>
  <div class="circle">
    <span class="label">2</span>
    <span class="title">Ticket&nbsp;Raised</span>
  </div>
  <span class="bar"></span>
  <div class="circle">
    <span class="label">3</span>
    <span class="title">Completed</span>
  </div>
</div>

我正在做这个 jQuery 来改变我的 title 相对于 circleleft 但这不是工作。

$(document).ready(function(){
    for (i=1;i<4;i++){
        var pos = $('.progress .circle:nth-of-type(' + i + ')').position();
        var widthLabel = $('.progress .circle:nth-of-type(' + i + ') .title').outerWidth();
        $('.progress .circle:nth-of-type(' + i + ') .title').css({
            position: "aboslute",   
            left: (pos.left - (widthLabel/2)) + "px"
        });
    }
});

最佳答案

您提供的代码中存在 position - absolute 的拼写错误,即使拼写更正,也无法正常工作,因为它需要相对于 circle。

试试下面的代码

$(document).ready(function(){
            for (i=1;i<4;i++){
            var widthLabel = $('.progress .circle:nth-of-type(' + i + ') .title').outerWidth();
            var widthCircle = $('.progress .circle:nth-of-type(' + i + ')').outerWidth();
            $('.progress .circle:nth-of-type(' + i + ') .title').css({
                position: "relative",   
                left: -((widthLabel/2) - (widthCircle/2)) + "px"
            });
        }
    });

然后用圆宽度减去标签宽度。这样就可以了

关于javascript - 如何使用 CSS/jQuery 将一个元素相对于另一个元素定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283841/

相关文章:

javascript - 传递参数数组

javascript - 从行中的复选框获取表值

javascript - 由于 MIME 类型错误,Chrome 拒绝执行 AJAX 脚本

html - 使用显示属性时如何为内部 div 元素设置固定宽度

javascript - 询问用户是否确定,然后禁用按钮并提交表单

javascript - 两种形式,一种提交按钮

javascript - 缩小 NodeJS 中使用的代码是否有意义?

javascript - 使用 d3.js 附加多个气泡云图

javascript - 列出包装 Shiny 传单的输入处理程序

动态加载到 iframe 的 JavaScript 在父范围内运行