我编写了ajQuery代码来确定高度和宽度,并单击功能。代码如下:
$(document).ready(function(){
console.log("Jquery is working!");
adjustBar();
$(window).on('resize', function() {
adjustBar();
})
$('#height').on('input change', function() {
var height = $(this).val();
if (height >= 30) {
var leftOffset = (Math.tan(45 * (Math.PI / 180)) * (height / 2) + 3) * -1;
$('.steps').css('height', height).css('line-height', height + "px").css('left', leftOffset + "px");
adjustBar();
}
});
$('.steps').on('click', function() {
$('.steps').removeClass('active');
$(this).addClass('active');
})
});
function adjustBar() {
var items = $('.steps').length;
var elHeight = $('.steps').height() / 2; //Division by 2 because each pseudo which is skewed is only 50% of its parent.
var skewOffset = Math.tan(45 * (Math.PI / 180)) * elHeight;
var reduction = skewOffset + ((items - 1) * 4);
var leftOffset = $('.steps').css('left').replace('px', '');
var factor = leftOffset * (-1) - 2;
$('.steps').css({
'width': '-webkit-calc((100% + 4px - ' + reduction + 'px)/' + items + ')'
}); // 4px for borders on either side
$('.steps:first-child, .steps:last-child').css({
'width': '-webkit-calc((100% + 4px - ' + reduction + 'px)/' + items + ' + ' + factor + 'px)'
}); // 26px because to make up for the left offset. Size of last-child is also increased to avoid the skewed area on right being shown
$('.steps span').css('padding-left', (skewOffset + 15) + "px");
$('.steps:first-child span, .steps:last-child span').css({
'width': '-webkit-calc(100% - ' + factor + 'px)'
});
} 当我运行该代码时,我会得到以下错误:
错误TS2362:算术操作的左边必须是'any‘、'number’、'bigint‘或枚举类型。event-viewer/event-viewer.component.ts(34,35):error TS2345:参数类型为'string string not string[]‘,不能分配给'string string number not(此处: HTMLElement,index: number,value: string) => string string number index’的参数。键入'string[]‘不能指定键入'string欧元编号’(这是: HTMLElement,index: number,value: string),=> \x,string,not,void)‘。键入“string[]”不能指定键入“string”。event-viewer/event-viewer.component.ts(49,20):error TS2362:算术操作的左侧必须是'any‘、'number’、'bigint‘或枚举类型。
height、width和leftoffset似乎被认为是字符串,但它们应该是由网页中元素的位置来计算的数值。我怎样才能纠正这些错误?
发布于 2019-04-12 09:23:12
应该将字符串值转换为数字。您可以使用:var height = Number($(this).val());
https://stackoverflow.com/questions/55646224
复制相似问题