Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用CSS/jQuery相对定位一个元素_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何使用CSS/jQuery相对定位一个元素

Javascript 如何使用CSS/jQuery相对定位一个元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正试图将标题定位在圆圈的以下代码中心 <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 .cir

我正试图将
标题
定位在
圆圈
的以下代码中心

<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>
我做这个jQuery是为了改变我的
标题
相对于
圆圈的
左侧
,但它不起作用

$(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"
        });
    }
});
$(文档).ready(函数(){

对于(i=1;i您在jQuery中拼写了position:“absolute”错误。

您在jQuery中拼写了position:“absolute”错误。

您给出的代码有position-absolute的拼写错误,即使拼写得到纠正,它也无法工作,因为它需要相对于圆

请尝试以下代码

$(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"
            });
        }
    });
$(文档).ready(函数(){

对于(i=1;i,u给出的代码有position-absolute的拼写错误,即使拼写得到纠正,它也不会工作,因为它需要相对于圆

请尝试以下代码

$(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"
            });
        }
    });
$(文档).ready(函数(){

对于(i=1;i我已经将您的代码移动到了一个小地方,但是我没有让它按照您想要的方式工作,相反,我在不使用javascript的情况下形成了一个稍微精简的方法


请随意编辑您的SPECT

我已经将您的代码移动到了一个小提琴上,但是我没有让它按照您想要的方式工作,而是在不使用javascript的情况下形成了一个稍微精简的方法

请随意编辑您的SPECT

使用此简单方式

$('.title')。每个(函数(){
var width=$(this).width()/2;
$(this.css({marginLeft:-width});
});
*,*:之后,*:之前{
保证金:0;
填充:0;
框大小:边框框;
字体系列:“开放式SAN”;
}
/*形成过程*/
.进展{
/*宽度:1500px*/
宽度:100%;/*演示使用*/
保证金:20px自动;
文本对齐:居中;
}
.进步,循环,
.进步吧{
显示:内联块;
背景:#fff;
宽度:40px;高度:40px;
边界半径:40px;
边框:1px实心#d5da;
}
.进步吧{
位置:相对位置;
宽度:100px;
高度:6px;
左边距:-5px;
右边距:-5px;
左边界:无;
边界权:无;
边界半径:0;
}
.进步,循环{
位置:相对;/*新更改*/
}
.进度.圆圈.标签{
显示:内联块;
宽度:32px;
高度:32px;
线高:32px;
边界半径:32px;
利润上限:3倍;
颜色:#b5b5ba;
字号:17px;
}
.进度.循环.标题{
颜色:#b5b5ba;
字体大小:13px;
线高:30px;
左边距:-5px;
/*新变化*/
左:50%;
位置:绝对位置;
顶部:33px;
}
/*完成/激活*/
.进度.完成,
.进展.循环.完成{
背景:#eee;
}
.progress.bar.active{
背景:线性梯度(向右,EEE 40%,FFF 60%);
}
.progress.circle.done.label{
颜色:#FFF;
背景#8bc435;
盒影:插入0.2pxRGBA(0,0,0,2);
}
.进度.循环.完成.标题{
颜色:#444;
}
.progress.circle.active.label{
颜色:#FFF;
背景:#0c95be;
盒影:插入0.2pxRGBA(0,0,0,2);
}
.progress.circle.active.title{
颜色:#0c95be;
}

1.
索取车票
2.
提高票数
3.
完整的
使用这种简单的方法

$('.title')。每个(函数(){
var width=$(this).width()/2;
$(this.css({marginLeft:-width});
});
*,*:之后,*:之前{
保证金:0;
填充:0;
框大小:边框框;
字体系列:“开放式SAN”;
}
/*形成过程*/
.进展{
/*宽度:1500px*/
宽度:100%;/*演示使用*/
保证金:20px自动;
文本对齐:居中;
}
.进步,循环,
.进步吧{
显示:内联块;
背景:#fff;
宽度:40px;高度:40px;
边界半径:40px;
边框:1px实心#d5da;
}
.进步吧{
位置:相对位置;
宽度:100px;
高度:6px;
左边距:-5px;
右边距:-5px;
左边界:无;
边界权:无;
边界半径:0;
}
.进步,循环{
位置:相对;/*新更改*/
}
.进度.圆圈.标签{
显示:内联块;
宽度:32px;
高度:32px;
线高:32px;
边界半径:32px;
利润上限:3倍;
颜色:#b5b5ba;
字号:17px;
}
.进度.循环.标题{
颜色:#b5b5ba;
字体大小:13px;
线高:30px;
左边距:-5px;
/*新变化*/
左:50%;
位置:绝对位置;
顶部:33px;
}
/*完成/激活*/
.进度.完成,
.进展.循环.完成{
背景:#eee;
}
.progress.bar.active{
背景:线性梯度(向右,EEE 40%,FFF 60%);
}
.progress.circle.done.label{
颜色:#FFF;
背景#8bc435;
盒影:插入0.2pxRGBA(0,0,0,2);
}
.进度.循环.完成.标题{
颜色:#444;
}
.progress.circle.active.label{
颜色:#FFF;
背景:#0c95be;
盒影:插入0.2pxRGBA(0,0,0,2);
}
.progress.circle.active.title{
颜色:#0c95be;
}

1.
索取车票
2.
提高票数
3.
完整的

我知道这并不是完全回答问题,但这是您努力获得所需结果的原因。我知道这并不是完全回答问题,但这是您努力获得所需结果的原因。