为什么跟着视频学,设置好右边的JS效果后 没有那种效果呢?
window.onload=function(){
var rightDiv=document.getElementById("right");
var mainDiv=document.getElementById("main");
var ifKeyDown=false;
rightDiv.onmousedown=function(){
ifKeyDown=true;
}
window.onmouseup=function(){
ifKeyDown=false;
}
window.onmousemove=function(e){
if(ifKeyDown==true)
{
var x= e.clientX;//鼠标x左边
var widthBefore=mainDiv.offsetWidth-2;
var addWidth=x - getPosition(mainDiv).left - widthBefore;//鼠标移动后增加的宽度
mainDiv.style.width=addWidth + widthBefore +"px";
}
}
}
//获取元素相对于屏幕左边的距离;
function getPosition(node)
{
var left=node.offsetLeft;
var parent=node.offsetParent;
var top=node.offsetTop;
while(parent != null)
{
left +=parent.offsetLeft;
top +=parent.offsetTop;
parent=parent.offsetParent;
}
return (left,top);
}