在一个页面中,在不同的位置插入相同的特效,导致只有一个可以运行,另一个无效
<div class="img-1"> <div id="scrollpic"><a id="prev" href="javascript:;"></a> <ul> <li><img src="images/52C58PICA3W_1024.jpg" alt="" /></li> <li><img src="images/u=527348655,832489918&fm=21&gp=0.jpg" alt="" /></li> <li><img src="images/91.jpg" alt="" /></li> <li><img src="images/3ea37e962e7da162fd45e9a44ce_p1_mk1_wm35.jpg" alt="" /></li> <li><img src="images/200862181337850.jpg" alt="" /></li> </ul> <a id="next" href="javascript:;"></a>
</div> </div>
<script type="text/javascript" src=“js/xunhuan.js”>// <![CDATA[ window.onload = function(){ //操作dom var oPicList = document.getElementById("scrollpic"); var oUl = oPicList.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li"); var aPrev = document.getElementById("prev"); var aNext = document.getElementById("next"); oUl.innerHTML+=oUl.innerHTML; oUl.style.width = aLi[0].offsetWidth * aLi.length + "px"; oUl.style.left =-oUl.offsetWidth/2; var speed = 1; //控制滚动 function movePic(){ if(oUl.offsetLeft<=-oUl.offsetWidth/2){ oUl.style.left = "0"; } if(oUl.offsetLeft>0){ oUl.style.left = -oUl.offsetWidth/2; } oUl.style.left = oUl.offsetLeft + speed +"px"; } var timer = setInterval(movePic,30); //鼠标操作 暂停/滚动 oUl.onmouseover = function(){ clearInterval(timer); } oUl.onmouseout = function(){ timer = setInterval(movePic,30); } //控制图片左右滚动 aPrev.onmouseover=function(){ speed=1; } aNext.onmouseover=function(){ speed=-1; } } // ]]></script>
<div id="cp-text"> <h1><strong>活动二:</strong></h1> <br />6月1日-3日,更有多款温馨儿童套餐半价优惠,我们不仅要带给您的孩子快乐,更要带给孩子健康</div>
<div class="img-2"> <div id="scrollpic01">
<a id="prev01" href="javascript:;"></a> <ul> <li><img src="images/60cc1411dc75e7e73429b9ef88a2f59e.jpg" alt="" /></li> <li><img src="images/0066wBgEgy6USvoGDfr29&690.jpg" alt="" /></li> <li><img src="images/fd039245d688d43fe665cfb57e1ed21b0ff43bac.jpg" alt="" /></li> <li><img src="images/u=2353490882,1623480867&fm=21&gp=0.jpg" alt="" /></li> <li><img src="images/创意便当.jpg" alt="" /></li> </ul> <a id="next01" href="javascript:;"></a>
</div> </div>
<script type="text/javascript" src="js/xunhuan01.js">// <![CDATA[ window.onload = function(){ //操作dom var oPicList = document.getElementById("scrollpic01"); var oUl = oPicList.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li"); var aPrev = document.getElementById("prev01"); var aNext = document.getElementById("next01"); oUl.innerHTML+=oUl.innerHTML; oUl.style.width = aLi[0].offsetWidth * aLi.length + "px"; oUl.style.left =-oUl.offsetWidth/2; var speed = 1; //控制滚动 function movePic(){ if(oUl.offsetLeft<=-oUl.offsetWidth/2){ oUl.style.left = "0"; } if(oUl.offsetLeft>0){ oUl.style.left = -oUl.offsetWidth/2; } oUl.style.left = oUl.offsetLeft + speed +"px"; } var timer = setInterval(movePic,30); //鼠标操作 暂停/滚动 oUl.onmouseover = function(){ clearInterval(timer); } oUl.onmouseout = function(){ timer = setInterval(movePic,30); } //控制图片左右滚动 aPrev.onmouseover=function(){ speed=1; } aNext.onmouseover=function(){ speed=-1; } } // ]]></script>
可以使用下面方法。ab分别替代上面两个window.onload。如下:
window.onload= function(){a();b();}
function a(){第一个函数}
function b(){第二个函数}
window.onload只能绑定一次
如果想要达到这种效果,该怎么编写呢?
@By_The_Way: 把这两个代码块封装成方法,在window.onload中执行方法就可以了
直接赋值的方法会把第一次的onload事件被第二次替换掉,所以只有一个有效了。