html+jquery生成单元格的对角线

本文介绍了一种利用jQuery和CSS实现动态绘制HTML表格中单元格对角线的方法。通过对单元格宽度和高度的计算,确定对角线的角度和长度,并通过CSS的伪元素:before及transform属性来完成绘制。
<script src="http://code.jquery.com/jquery-latest.js"></script>
<head>
	<style>
		table td {width: 200px; height: 150px; border: 1px solid #000;}
	</style>
</head>
<body onload="change()">
	<table>
		<tr>
			<td class="inline"></td>
		</tr>
	</table>
</body>
<script>
function change() {
	var top = 0;
	var left = 0;
	var length = 0;
	var angle = 0;
	var width = $('.inline').width();
	var height = $('.inline').height();

	//线条position设置为absolute时,坐标需要重新定位,获取单元格的偏移量作为线条的偏移量
	top = $('.inline').offset().top;
	left = $('.inline').offset().left;

	angle = Math.round(Math.atan(width/height)*180/Math.PI);//获取对角线角度,Math.atan()获取的是弧度,乘于180/pi得到角度
	length = Math.sqrt(width * width + height * height);//获取对角线长度,勾股定理

	$('style').append('.inline::before {content: ""; position: absolute; width: 1px;left: ' + left + 'px;top: ' + top + 'px;background-color: #000; transform-origin: top;height: ' + length + 'px; transform: rotate(-' + angle + 'deg);}');
	}
</script>

效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值