一个页面中onload和onscroll冲突,只能显示一种效果

ice241018 2016-05-09 11:16:29
js代码1:
window.onload=window.onresize=window.onscroll=function () {
var oBox = document.getElementById("divQQbox");
var oLine = document.getElementById("divOnline");
var oMenu = document.getElementById("divMenu");
var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
setTimeout(function() {
clearInterval(oBox.timer);
var iTop = parseInt((document.documentElement.clientHeight - oBox.offsetHeight) / 2) + iScrollTop;
oBox.timer = setInterval(function() {
var iSpeed = (iTop - oBox.offsetTop) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
oBox.offsetTop == iTop ? clearInterval(oBox.timer) : (oBox.style.top = oBox.offsetTop + iSpeed + "px");
}, 10)
}, 100)

oBox.onmouseover = function() {
this.style.width = 153 + "px";
this.style.height = 348 + "px";
oLine.style.display = "block";
oLine.style.background = "#f5fbff";
oMenu.style.display = "none";
};
oBox.onmouseout = function() {
this.style.width = '';
this.style.height = '';
oLine.style.display = "none";
oMenu.style.display = "block";
};
};

js代码2:
window.onload = function ()
{
var oDiv = document.getElementById('playBox');
var oPre = getByClass(oDiv,'pre')[0];
var oNext = getByClass(oDiv,'next')[0];
var oUlBig = getByClass(oDiv,'oUlplay')[0];
var aBigLi = oUlBig.getElementsByTagName('li');
var oDivSmall = getByClass(oDiv,'smalltitle')[0]
var aLiSmall = oDivSmall.getElementsByTagName('li');

function tab()
{
for(var i=0; i<aLiSmall.length; i++)
{
aLiSmall[i].className = '';
}
aLiSmall[now].className = 'thistitle'
startMove(oUlBig,'left',-(now*aBigLi[0].offsetWidth))
}
var now = 0;
for(var i=0; i<aLiSmall.length; i++)
{
aLiSmall[i].index = i;
aLiSmall[i].onclick = function()
{
now = this.index;
tab();
}
}
oPre.onclick = function()
{
now--
if(now ==-1)
{
now = aBigLi.length;
}
tab();
}
oNext.onclick = function()
{
now++
if(now ==aBigLi.length)
{
now = 0;
}
tab();
}
var timer = setInterval(oNext.onclick,3000) //滚动间隔时间设置
oDiv.onmouseover = function()
{
clearInterval(timer)
}
oDiv.onmouseout = function()
{
timer = setInterval(oNext.onclick,3000) //滚动间隔时间设置
}
}

这两段代码单独执行没有问题,但是放在一个页面中,有一个js效果不起作用,请问如何解决?谢谢!
...全文
157 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookee 2016-05-10
  • 打赏
  • 举报
回复

function doit() {
    var oBox = document.getElementById("divQQbox");
    var oLine = document.getElementById("divOnline");
    var oMenu = document.getElementById("divMenu");
    var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    setTimeout(function() {
        clearInterval(oBox.timer);
        var iTop = parseInt((document.documentElement.clientHeight - oBox.offsetHeight) / 2) + iScrollTop;
        oBox.timer = setInterval(function() {
            var iSpeed = (iTop - oBox.offsetTop) / 8;
            iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
            oBox.offsetTop == iTop ? clearInterval(oBox.timer) : (oBox.style.top = oBox.offsetTop + iSpeed + "px");
        }, 10)
    }, 100)

    oBox.onmouseover = function() {
        this.style.width = 153 + "px";
        this.style.height = 348 + "px";
        oLine.style.display = "block";
        oLine.style.background = "#f5fbff";
        oMenu.style.display = "none";
    };
    oBox.onmouseout = function() {
        this.style.width = '';
        this.style.height = '';
        oLine.style.display = "none";
        oMenu.style.display = "block";
    };
};
window.onload = function ()
  {
          window.onresize=window.onscroll = doit;
          doit();
	  var oDiv = document.getElementById('playBox');
	  var oPre = getByClass(oDiv,'pre')[0];
	  var oNext = getByClass(oDiv,'next')[0];
	  var oUlBig = getByClass(oDiv,'oUlplay')[0];
	  var aBigLi = oUlBig.getElementsByTagName('li');
	  var oDivSmall = getByClass(oDiv,'smalltitle')[0]
	  var aLiSmall = oDivSmall.getElementsByTagName('li');
	  
	  function tab()
	  {
	     for(var i=0; i<aLiSmall.length; i++)
	     {
		    aLiSmall[i].className = '';
	     }
	     aLiSmall[now].className = 'thistitle'
	     startMove(oUlBig,'left',-(now*aBigLi[0].offsetWidth))
	  }
	  var now = 0;
	  for(var i=0; i<aLiSmall.length; i++)
	  {
		  aLiSmall[i].index = i;
		  aLiSmall[i].onclick = function()
		  {
			  now = this.index;
			  tab();
		  }
	 }
	  oPre.onclick = function()
	  {
		  now--
		  if(now ==-1)
		  {
			  now = aBigLi.length;
		  }
		   tab();
	  }
	   oNext.onclick = function()
	  {
		   now++
		  if(now ==aBigLi.length)
		  {
			  now = 0;
		  }
		  tab();
	  }
	  var timer = setInterval(oNext.onclick,3000) //滚动间隔时间设置
	  oDiv.onmouseover = function()
	  {
		  clearInterval(timer)
	  }
	   oDiv.onmouseout = function()
	  {
		  timer = setInterval(oNext.onclick,3000) //滚动间隔时间设置
	  }
  }
ice241018 2016-05-10
  • 打赏
  • 举报
回复
谢谢hookee,问题解决!
天际的海浪 2016-05-09
  • 打赏
  • 举报
回复
第二个代码 window.onload = function () { ............ } 改成 function myload() { ........... } if (window.attachEvent) window.attachEvent("onload", myload); else window.addEventListener("load", myload, false);

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧