所以我想使用 width()
方法返回我网站上元素的宽度,默认情况下它将以像素为单位显示。
我正在阅读W3Schools 您可以将值更改为许多不同的单位,例如 px、em、pt 等。
我想以百分比形式返回宽度,如何实现?
这是我的代码:
Jquery
$('.range-control').each(function(){
var container = $(this),
input = container.find('input'),
output = container.find('output'),
rangeWidth = input.width(),
thumbWidth = container.attr('data-thumbwidth'),
startValue = input.val(),
startOffset = ((rangeWidth - thumbWidth) / 100) * startValue;
output
.css({
'left' : startOffset
});
$(input).on('input', function(){
var value = this.value,
offset = ((rangeWidth - thumbWidth) / 100) * value;
output
.val(value)
.css({
'left' : offset
});
});
});
任何帮助将不胜感激!!
最佳答案
不能只用计算吗?
var parentWidth = $(this).parent().width();
var thisWidth = $(this).width();
var percentWidth = (thisWidth/parentWidth)*100+"%";
关于javascript - 使用 width() 方法并以百分比形式返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686482/