简体   繁体   English

旋转div,左边缘和右边缘倾斜/切回平行

[英]Rotating div with left and right edges slanted/cut back to parallel

I use this function to rotate div: 我使用此功能旋转div:

function connect(x1,y1,x2,y2,color,thickness) {
    var length = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));// distance
    var cx = ((x1 + x2) / 2) - (length / 2);// center
    var cy = ((y1 + y2) / 2) - (thickness / 2);// center
    var angle = Math.atan2((y1-y2),(x1-x2))*(180/Math.PI);// angle
    var htmlLine = '<div style="height:'+thickness+'px; background-color:'+color+';line-height:1px;position:absolute;left:'+cx+'px;top:'+cy+'px;width:'+length+'px;transform:rotate('+angle+'deg);"></div>';
    document.body.innerHTML += htmlLine;
}

How can I cut-off edges from that div, so that left and right side stays parallel to the screen? 如何从该div切掉边缘,使左侧和右侧与屏幕平行?

Take a look at the picture to understand what I'm going for 看一下图片,了解我要做什么

在此处输入图片说明

Use transform: skew(ax, ay) with ax == 0 or transform: skewY(ay) : 使用ax == 0 transform: skew(ax, ay) transform: skewY(ay)

 div { width: 200px; height: 20px; background: green; transform: skew(0, 20deg); transform-origin: top left; } 
 <div></div> 

In your case, if you intend to keep your connect function the same, just replace rotate in your transform call with skewY : 在你的情况,如果你想把你的connect功能相同,只需更换rotate在与变换电话skewY

 function connect(x1,y1,x2,y2,color,thickness) { var length = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));// distance var cx = ((x1 + x2) / 2) - (length / 2);// center var cy = ((y1 + y2) / 2) - (thickness / 2);// center var angle = Math.atan2((y1-y2),(x1-x2))*(180/Math.PI);// angle var htmlLine = '<div style="height:'+thickness+'px; background-color:'+color+';line-height:1px;position:absolute;left:'+cx+'px;top:'+cy+'px;width:'+length+'px;transform:skewY('+angle+'deg);"></div>'; document.body.innerHTML += htmlLine; } connect(0, 0, 300, 40, 'green', 10); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM