嗨,我正在做一个CodePen来制作一个没有Jquery的可拖的和大小大小的盒子。我想要完成的是,当您在div的主体上悬停/单击时,您可以将它拖来拖去,并具有“移动”光标。
当您在边框上悬停/单击时,您可以根据单击的侧边调整框的大小,并使用“”或“行调整大小”游标。
我真正想知道的是,是否甚至可以使用JS和CSS选择边框,如果是的话,如何选择。
发布于 2017-04-25 18:43:23
这是一个如何确定您正在悬停的边框的示例(祝您的其他计算好运):
var div = document.querySelector("#div");
var delta = 10; // the thickness of the hovered border area
div.onmousemove = function(e) {
var rect = div.getBoundingClientRect();
var x = e.clientX - rect.left, // the relative mouse postion to the element
y = e.clientY - rect.top, // ...
w = rect.right - rect.left, // width of the element
h = rect.bottom - rect.top; // height of the element
var c = ""; // which cursor to use
if(y < delta) c += "n"; // north
else if( y > h - delta) c += "s"; // south
if(x < delta) c += "w"; // west
else if(x > w - delta) c += "e"; // east
if(c) // if we are hovering at the border area (c is not empty)
div.style.cursor = c + "-resize"; // set the according cursor
else // otherwise
div.style.cursor = "pointer"; // set to pointer
}
#div {
background-color: red;
width: 100px;
height: 100px;
}
<div id="div"></div>
<br>Psst! Hover at the border area! Corners too.
注释:上面的方法与元素是否有边框无关,它是否有子节点(例如img
.)。
发布于 2017-04-25 18:41:30
创建一个div并使它在您的孔div内是绝对的,添加您的边框样式,然后给它一个悬停效果和您想要的事件。设置相对于孔div的位置和对边框div的绝对设置,您必须根据边框宽度设置左、右、上、下到负值,您可能需要将背景颜色设置为透明。
https://stackoverflow.com/questions/43623964
复制相似问题