拖拽一个元素的框架

function down(e){
        //获取元素的开始位置和鼠标点击的开始位置
		left = $(elem).position().left;
        startX = e.clientX;
        function move(e){
        	var newLeft = 0;
            endX = e.clientX;
            newLeft = left + endX - startX;
            if (newLeft > max) {
                newLeft = max;
            } else if (newLeft < 0) {
                newLeft = 0;
            }
            $(elem).css({
                left: newLeft
            });
         }
           
       	function up(){
           $(document).off('mousemove');
           $(document).off('mouseup');
           //释放捕获
           elem.releaseCapture && elem.releaseCapture();
        }
       	$(document).on('mousemove',move);
       	$(document).on('mouseup',up);
        //设置捕获
        elem.setCapture && elem.setCapture();
        //阻止浏览器默认事件
        e.preventDefault && e.preventDefault();
        return false;
}
$(elem).on('mousedown',down);

 

你可能感兴趣的:(JS)