使用width()方法并以百分比形式返回值
Using the width() method and returning the value as a percentage
所以我想使用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返回值只在循环中返回一次
- XMLHttpRequest未返回值-状态202
- 根据是否解析了 Promise 从函数返回值
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何从客户端的数组中获取用户输入和返回值
- Javascript中带有返回值的嵌套函数
- 为变量分配多个nodejs导出返回值时出现问题
- 通过回调从onreadystatechange返回值
- 从Ajax函数返回值
- JavaScript生成器中收益率返回值的解析
- HTML如何根据javascript函数的返回值限制文本输入
- 返回值+new Date()与Date.now()是否不同
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 如何在Android中将Javascript返回值转换为String
- chrome.storage.sync.get未返回值-Angular服务
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何从嵌套的API函数返回值
- angularjs如何从返回值中找到正确的属性
- 使用width()方法并以百分比形式返回值