jquery - 如何使用 css 和 jquery 实现 100% -100px

标签 jquery html css asp.net

我在 css 和 jquery 中使用相同的代码:

width: 100% -100px;

所以,我使用这段代码:

$(document).ready(function(){
    $('.left').css('width', '100%').css('width', '-=100px');
});

但是当浏览器加载网站后调整大小时它不能正常工作。我想工作类似于此代码:

.left
{
    width: 80%; //100% -100px;
}
.right
{
    width: 20%; //100px;
}

最佳答案

更新: Demo using jQuery

选项 1:

使用 jQuery:

function resize_div(selector){
    var curr_width = selector.width(); // get the current width
    new_width = curr_width - 100;
    selector.css('width', new_width);
}

$(document).ready(function(){
    $('.left').css('width', '100%'); // set the width to 100% initially
    resize_div($('.left')); // then resize width on document ready
});
$(window).resize(function(){
    $('.left').css('width', '100%'); // set the width to 100% initially
    resize_div($('.left')); // resize width everytime the window is resized
});

HERE'S A DEMO

选项 2:

您可以使用 css calc(),例如:

/* Firefox */
width: -moz-calc(100% - 100px);
/* WebKit */
width: -webkit-calc(100% - 100px);
/* Opera */
width: -o-calc(100% - 100px);
/* Standard */
width: calc(100% - 100px);

选项 3:

使用 padding and box-sizing

.some-div
{
   padding-right: 50px;
   padding-left: 50px;
   width: 100%;
   -moz-box-sizing: border-box;
   box-sizing: border-box;
}

关于jquery - 如何使用 css 和 jquery 实现 100% -100px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228962/

相关文章:

javascript - 如何在不滚动的情况下触发滚动事件

jquery - 展开折叠网格并调整为页面宽度

javascript - 创建一个自定义下拉选择,就像用户注销时在推特上使用的那样

javascript - 按 HTML 实体分割字符串?

html - opera mini 输入字段中文本的居中对齐

python - 修正经典风格的 Matplotlib 数学字体大小

javascript - 为什么 jQuery 这里需要 `getWindow(doc)` ?

javascript - 应用鼠标悬停样式时,通过箭头键导航删除应用于列表项的选定类

javascript - 如何根据数值通过数据属性进行搜索并获取最接近的值

html - Bootstrap - 第二列顶部填充?