在指定对象范围内移动
<pre><code class="language-js">// 只能在指定层上移动
function moveOnLayer(oLayer, event){
if(!event.target) return
const o = event.target
oLayer.setCoords()
let {left, top, width, height} = oLayer.getBoundingRect()
if(o.currentHeight > height || o.currentWidth > width) return
o.setCoords()
let {left: l, top: t, width: w, height: h} = o.getBoundingRect()
// 上
if(t < top){
o.top = Math.max(o.top, o.top - t, top)
}
// 左
if(l < left){
o.left = Math.max(o.left, o.left - l, left)
}
// 下
if(t + h > top + height){
o.top = Math.min(o.top, height - h + o.top - t) + top
}
// 右
if(l + w > left + width){
o.left = Math.min(o.left, width - w + o.left - l) + left
}
}</code></pre>