「前端基础」实现一个拖曳,要怎样才能兼容ie7,8,9,麻烦各位帮我看看

window.onload = function() { var bar = document.getElementById('bar'); var box = document.getElementById("box"); bar.onmousedown = function(event) { drag(event,box); }

if(window.addEventListener) { bar.addEventListener('mousedown',function() { drag(event,box); },false); } else if(window.attachEvent) { bar.attachEvent('mousedown',function(){drag(event,box);}); }

} function drag(event,obj)//拖曳 { var event = event || window.event; console.log(obj.offsetTop); var disX = event.clientX - obj.offsetLeft; var disY= event.clientY - obj.offsetTop; console.log(disY);

document.onmousemove = function(event)
{
  event = event || window.event;
  mouseMove(event,disX,disY);
}
document.onmouseup = function()
{
  document.onmousemove = null;
  document.onmouseup = null;
}

} function mouseMove(event,disX,disY)//鼠标移动时 { var box = document.getElementById('box'); var event = event || window.event; var left = event.clientX - disX; var top = event.clientY - disY; var winWidth = document.documentElement.clientWidth || document.body.clientWidth; var winHeight = document.documentElement.clientHeight || document.body.clientHeight; maxW = winWidth - box.offsetWidth; maxH = winHeight - box.offsetHeight; if (left > maxW) { left = maxW; } else if (left < 0) { left = 0; } else if(top >maxH) { top = maxH; } else if(top < 0) { top = 0; } box.style.left = left +'px'; box.style.top = top + 'px'; }

回答
程序狗 ,web前端程序员

拖拽是H5新标签,IE低版本是不支持的,可以引入<!– HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries –>试试

极乐网 ,极乐,让技术变得更加简单!
我来回答
无用回答
问题标签
问题修改记录
暂无修改记录
广告位 点击查看投放指南

我的收藏