一个Javascript解决方案:
演示:http: //jsfiddle.net/nz8ut/2/
function resizableStart(e){
this.originalW = this.clientWidth;
this.originalH = this.clientHeight;
this.onmousemove = resizableCheck;
this.onmouseup = this.onmouseout = resizableEnd;
}
function resizableCheck(e){
if(this.clientWidth !== this.originalW || this.clientHeight !== this.originalH) {
this.originalX = e.clientX;
this.originalY = e.clientY;
this.onmousemove = resizableMove;
}
}
function resizableMove(e){
var newW = this.originalW + e.clientX - this.originalX,
newH = this.originalH + e.clientY - this.originalY;
if(newW < this.originalW){
this.style.width = newW + 'px';
}
if(newH < this.originalH){
this.style.height = newH + 'px';
}
}
function resizableEnd(){
this.onmousemove = this.onmouseout = this.onmouseup = null;
}
var els = document.getElementsByClassName('resizable');
for(var i=0, len=els.length; i<len; ++i){
els[i].onmouseover = resizableStart;
}
上面的解决方案使用mouseover
andmouseout
来触发resizableStart
and resizableEnd
。问题是如果要调整大小的元素有子元素,当鼠标放在一个子元素上时,该元素会收到一个mouseout
事件,然后,它会收到一个mouserover
从子元素冒泡的事件。
为了避免这些事件,我使用mouseenter
和mouseleave
事件实现了另一个解决方案:
演示:http: //jsfiddle.net/nz8ut/3/