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

时间:2016-02-09 04:23:46

标签: 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来改变left相对于title circle的{​​{1}},但它不是wotking。

$(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"
        });
    }
});

4 个答案:

答案 0 :(得分:1)

你给出的代码有一个拼写错误的位置 - 绝对,即使拼写被纠正,它也不会起作用,因为它需要相对于圆。

尝试以下代码

$(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"
            });
        }
    });

然后用圆宽减去标签宽度。这样做

答案 1 :(得分:1)

使用这种简单的方法

&#13;
&#13;
$('.title').each(function(){
	var width = $(this).width() / 2;
	$(this).css({marginLeft: -width});
});
&#13;
*, *:after, *:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Open Sans";
}


/* Form Progress */
.progress {
  /*width: 1500px;*/
  width: 100%; /*demo use*/
  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;
  margin-left: -5px;
  margin-right: -5px;
  border-left: none;
  border-right: none;
  border-radius: 0;
}
.progress .circle{
  position: relative; /* new changes*/
}
.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;
  /* new changes*/
  left: 50%; 
  position: absolute; 
  top: 33px;
}

/* 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;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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>
&#13;
&#13;
&#13;

Fiddle

答案 2 :(得分:0)

你的拼写位置:jQuery中的“绝对”错误。

答案 3 :(得分:0)

我已将您的代码移动到一个小提琴,但我没能按照您的意愿使其工作,而是我在不使用javascript的情况下形成了一种略微精简的方法。

https://fiddle.jshell.net/znrr38tv/

随意编辑您的作品