简体   繁体   English

具有相同CSS左值的Bootstrap按钮不在同一行中绘制

[英]Bootstrap buttons with the same css left value is not drawn in the same line

I have an image as indicated below 我有如下图所示

在此处输入图片说明

I have to add buttons to the end of each of the six image lines upon page load. 在页面加载时,我必须在六个图像行的末尾添加按钮。 I added my first button and managed to manipulate the top and left css styles using jquery to get the button into it's proper place. 我添加了我的第一个按钮,并设法使用jquery来控制topleft CSS样式,以将按钮放置在正确的位置。 Up to this point everything is working as expected and the button stays in place when resizing the screen etc. When however adding a second button and using the same left style as with the first one I'm running into an issue where the first button is drawn more to the left, even though it has the same left style as the second button. 到现在为止,一切都按预期工作,并且在调整屏幕大小时按钮保持在原位。但是,当添加第二个按钮并使用与第一个相同的left样式时,我遇到了一个问题,即第一个按钮是即使它与第二个按钮具有相同的left样式,也向左绘制更多。 For example 例如

在此处输入图片说明

I logged the left values of both buttons as I resized to the console, as seen from the below image the values are the same 在调整到控制台的大小时,我记录了两个按钮的左值,如下面的图像所示,值是相同的

在此处输入图片说明

Here is my HTML 这是我的HTML

<div id="vehicleContainer" class="ibox-content text-center">
    <button id="rdtLeftFrontTyreButton" class="btn btn-danger btn-circle btn-lg btn-outline" type="button"><i class="fa fa-times"></i></button>
    <button id="rdtLeftRearInnerTyreButton" class="btn btn-danger btn-circle btn-lg btn-outline" type="button"><i class="fa fa-times"></i></button>
    <img id="vehicleImage" src="~/content/RDT.png"/>
</div>

and here is my javascript function used to place the buttons in the correct positions 这是我的JavaScript函数,用于将按钮放置在正确的位置

function drawLayout() {
        var vehicleContainer = $("#vehicleContainer");

        var vehicleContainerHeight = vehicleContainer.height();
        var vehicleContainerWidth = vehicleContainer.width();

        var vehicleImage = $("#vehicleImage");

        // Set image height relative to the screen height
        var vehicleImageHeight = (vehicleContainerHeight * 98) / 100;
        vehicleImage.height(vehicleImageHeight);

        // Set image width relative to the screen width
        var vehicleImageWidth = (vehicleContainerWidth * 60) / 100;
        vehicleImage.width(vehicleImageWidth);

        var rdtLeftFrontTyreButton = $("#rdtLeftFrontTyreButton");
        var rdtLeftFrontTyreButtonTop = (vehicleContainerHeight * 39) / 100;
        var rdtLeftFrontTyreButtonLeft = (vehicleContainerWidth * -0.5) / 100;

        rdtLeftFrontTyreButton.css({
            position: "relative",
            top: -(rdtLeftFrontTyreButtonTop) + "px",
            left: -(rdtLeftFrontTyreButtonLeft) + "px"
        });

        console.log(rdtLeftFrontTyreButtonLeft);

        var rdtLeftRearInnerTyreButton = $("#rdtLeftRearInnerTyreButton");
        var rdtLeftRearInnerTyreButtonTop = (vehicleContainerHeight * 1.5) / 100;
        var rdtLeftRearInnerTyreButtonLeft = (vehicleContainerWidth * -0.5) / 100;

        rdtLeftRearInnerTyreButton.css({
            position: "relative",
            top: -(rdtLeftRearInnerTyreButtonTop) + "px",
            left: -(rdtLeftRearInnerTyreButtonLeft) + "px"
        });

        console.log(rdtLeftRearInnerTyreButtonLeft);
    }

Is this working? 这管用吗? I see that you are using position:relative , so they already start with a different position. 我看到您正在使用position:relative ,所以它们已经从另一个位置开始。 Because there are standing next to each other. 因为那里是彼此相邻的。

With display:block you can be sure that they have the same position left. 使用display:block可以确保它们具有相同的位置。

rdtLeftFrontTyreButton.css({
  position: "relative",
  top: -(rdtLeftFrontTyreButtonTop) + "px",
  left: -(rdtLeftFrontTyreButtonLeft) + "px",
  display: "block"
});

rdtLeftRearInnerTyreButton.css({
  position: "relative",
  top: (rdtLeftRearInnerTyreButtonTop) + "px",
  left: -(rdtLeftRearInnerTyreButtonLeft) + "px",
  display: "block"
});

Beside that, why are you doing relative and not absolute ? 除此之外,您为什么要relative而不是absolute

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

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