简体   繁体   English

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

[英]How to position one element relative another using CSS/jQuery

I'm tring to position the title in the following code center to the circle . 我想将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>

Here's my body elements with title and circle 这是我带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>

And I'm doing this jQuery to alter the left of my title relative to circle but it's not wotking. 我正在执行此jQuery,以将我的titleleft相对于circle更改,但它不起作用。

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

The code u have given has the spelling mistake for position - absolute, Even if the spelling is corrected, it won't work as it needs to be relative to circle. 您提供的代码在位置上存在拼写错误-绝对错误,即使纠正了拼写,也无法正常工作,因为它需要相对于圆。

Try the following code 试试下面的代码

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

The label width is then subtracted with the circle width. 然后,将标签宽度减去圆宽度。 This will do it 这会做到的

Use this simple way 用这个简单的方法

 $('.title').each(function(){ var width = $(this).width() / 2; $(this).css({marginLeft: -width}); }); 
 *, *: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; } 
 <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> 

Fiddle 小提琴

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

I've moved your code to a fiddle but I failed to get it to work as you want it, instead I formated a slightly Thin approach without using javascript. 我已经将您的代码移到了一个小提琴,但未能按您的要求运行,而是我格式化了一个略显Thin的方法而未使用javascript。

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

Feel free to edit to your spects 随时编辑您的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM