jQuery 或数学使用宽度百分比减去 div 的像素 - jSfiddle

发布于 2024-12-25 13:47:43 字数 1252 浏览 3 评论 0原文

http://jsfiddle.net/motocomdigital/7fyvn/1/

您好,这是一个问题我真的很难找到答案。

它不一定是 jQuery,它可能是一些简单的 css。

我在这里创建了jSfiddle,以便您可以自己进行实验。


我的情况是我有一个可变宽度容器,在小提琴中称为 div#container

在这个容器内,我有两列。列宽由百分比确定。

目前,我使用附加的 .left & ,左侧宽度为 65%,右侧宽度为 35%。 .right 类。

即使我在 .column 类上使用 width: 50% ,我也会遇到同样的问题。

我的问题是,我的两列之间需要 10 像素的边距。目前我的列宽度总计为 100%,以填充整个白框。

如果我向左列添加 10px padding-right。然后布局就会中断,添加 margin-right 也会发生同样的情况。

我的问题是,如何使用 jQuery 或 javascript 从每个列宽度中减去 5px?同时仍然保留我的宽度百分比?

这在某种程度上是可能的还是我在做梦。 javascript数学可以吗?我可以设想,但我想不出从哪里开始,或者什么方法最安全、最轻便。

它将在手机浏览器上使用,因此当方向从横向翻转为纵向时,javascript 减法需要保持真实,同时允许调整百分比宽度。

希望这是有道理的,请随时向我询问更多信息。


更新解决方案

请参阅此处仅使用 css 和额外的 div 解决的小提琴... http:// /jsfiddle.net/7fyvn/4/

您可以调整窗口大小,装订线始终保持不变,距左栏 -20px。请参阅类 .new-div.text-padding

http://jsfiddle.net/motocomdigital/7fyvn/1/

Hello, this is a question I'm really struggling to find an answer for.

It may not be necessarily jQuery, it my be a simple bit of css.

I have created jSfiddle here so you can experiment for yourself.

My circumstance is that I have a variable width container, called div#container in fiddle.

And inside this container, I have 2 columns. The column widths are determined by percentage.

Currently I have the left width at 65% and right at 35%, using additional .left & .right classes.

I would have the same problem even if I was using width: 50% on my .column class.

My problem, is that I need a 10px margin between my 2 columns. Currently my column widths add up to a total of 100%, to fill the entire white box.

If I add 10px padding-right to my left column. Then the layout breaks, and the same happens with adding margin-right.

My question is, how can I subtract 5px from each of the column widths using jQuery or javascript? Whilst still keeping the percentage for my widths?

Is this possible in some way or am I dreaming. Is it possible with javascript math? I can envisage it, but I can't think where to start, or what method is the safest and lightest.

It will be used on phone browsers, so when the orientation flips from landscape to portrait, the javascript subtraction needs to stay true whilst allowing the percentage widths to adjust.

Hope this makes sense, don't hesitate to ask me for more info.


UPDATE SOLUTION

See solved fiddle here just using css and an extra div... http://jsfiddle.net/7fyvn/4/

You can adjust the window size and gutter always stays the same, -20px off the left column. See classes .new-div and .text-padding

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2025-01-01 13:47:43

一种可能的方法是添加:

$(document).ready(function () { /* standard jQuery document ready */
    $("div.white-box").each(function(){ /* not optimal, but allowing for each white box to be a different width on the page */
        var width = $(this).width(), /* get the width of the white-box */
            left = $(this).find("div.left"), /* find the left column */
            right = $(this).find("div.right"); /* find the right column */
        left.width((width - 10) * 0.65).css('padding-right','10px'); /* set the left column to 65% of total minus 10 pixels and then pad those pixels back on to provide spacing */
        right.width((width - 10) * 0.35); /* set the right column to 35% of what is left after taking 10 pixels away from the total */
    });
});

One possible way would be to add:

$(document).ready(function () { /* standard jQuery document ready */
    $("div.white-box").each(function(){ /* not optimal, but allowing for each white box to be a different width on the page */
        var width = $(this).width(), /* get the width of the white-box */
            left = $(this).find("div.left"), /* find the left column */
            right = $(this).find("div.right"); /* find the right column */
        left.width((width - 10) * 0.65).css('padding-right','10px'); /* set the left column to 65% of total minus 10 pixels and then pad those pixels back on to provide spacing */
        right.width((width - 10) * 0.35); /* set the right column to 35% of what is left after taking 10 pixels away from the total */
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文