html - 计算从自动到像素的宽度,然后在 LESS CSS 中按像素添加

标签 html css less

我想使用自动宽度并向其添加 x 像素。
如何实现 Less 文件?

span 
{
    width:calc(auto + 100px);
}

我想使用自动,因为我不知道文本的长度。但我知道固定大小的文本会在某个时候附加到它上面。我不希望发生这种情况时跨度变大。因此,将 100px 添加到 auto 将完美处理。

最佳答案

您应该使用 JQuery 来完成此操作。

window.onload = function(){
  $('span').width($('span').width()+100px);
};

CSS 代码仍然存在

span{
  width: auto;
}

编辑:如果跨度内容在执行 onload 函数后发生变化,那么只要内容发生变化,您就应该执行该行。

Here你可以找到如何做到这一点。感谢@Emile

1- The jQuery change event is used only on user input fields because if anything else is manipulated (e.g., a div), that manipulation is coming from code. So, find where the manipulation occurs, and then add whatever you need to there.

2- But if that's not possible for any reason (you're using a complicated plugin or can't find any "callback" possibilities) then the jQuery approach I'd suggest is:

a. For simple DOM manipulation, use jQuery chaining and traversing, $("#content").html('something').end().find(whatever)....

b. If you'd like to do something else, employ jQuery's bind with custom event and triggerHandler

$("#content").html('something').triggerHandler('customAction');

$('#content').unbind().bind('customAction', function(event, data) { //Custom-action });

Here's a link to jQuery trigger handler: http://api.jquery.com/triggerHandler/

关于html - 计算从自动到像素的宽度,然后在 LESS CSS 中按像素添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920302/

相关文章:

javascript - 少编译和保存 Bootstrap

jquery - 悬停时显示子菜单,但取消悬停时不隐藏它

php - 如何使用php从HTML表格中提取数据

javascript - CSS 永久显示框

IE 8 中的 jQuery css 切换错误

less - 使用 LessCSS 的用户定义函数?

html - 标题图像( slider )和菜单栏之间的白色间隙

javascript - 如何禁用特定类下的提交按钮

css - ".msg"字体颜色在 Mac 和 Windows 上不同

css - 用于选择除伪元素之外的所有元素的 LESS 嵌套规则