多个onload冲突

时间:2012-04-27 01:25:39

标签: javascript jquery onload

你好......我使用jQuery缩略图滚动条遇到了问题。它与Fancybox以及我根据onload事件使用的另一个脚本冲突。我找到了这个函数,可以通过simon wilson激活多个onload事件:


function func1() {
  alert("This is the first.");
}
function func2() {
  alert("This is the second.");
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(func1);
addLoadEvent(func2);
addLoadEvent(function() {

I WANT TO INSERT THE FOLLOWING JQUERY FUNCTION HERE

})

我现在的问题是让第3个功能正常工作。这是一个语法问题。

这是我要插入的jQuery:

jQuery.noConflict();
(function($){
window.onload=function(){ 
    $("#tS3").thumbnailScroller({ 
        scrollerType:"hoverPrecise", 
        scrollerOrientation:"vertical", 
        scrollSpeed:2, 
        scrollEasing:"easeOutCirc", 
        scrollEasingAmount:800, 
        acceleration:4, 
        scrollSpeed:800, 
        noScrollCenterSpace:10, 
        autoScrolling:0, 
        autoScrollingSpeed:2000, 
        autoScrollingEasing:"easeInOutQuad", 
        autoScrollingDelay:500 
    });
}
})(jQuery);

任何建议都非常感谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

function func1() {
  alert("This is the first.");
}
function func2() {
  alert("This is the second.");
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(func1);
addLoadEvent(func2);
addLoadEvent(function() {
    $("#tS3").thumbnailScroller({ 
        scrollerType:"hoverPrecise", 
            scrollerOrientation:"vertical", 
            scrollSpeed:2, 
            scrollEasing:"easeOutCirc", 
            scrollEasingAmount:800, 
            acceleration:4, 
            scrollSpeed:800, 
            noScrollCenterSpace:10, 
            autoScrolling:0, 
            autoScrollingSpeed:2000, 
            autoScrollingEasing:"easeInOutQuad", 
            autoScrollingDelay:500 
        });
})

您的问题是,您尝试添加的功能是使用自己的功能专门覆盖window.onload,而不是按照意图添加自己。

答案 1 :(得分:1)

你为什么需要这样做?为什么需要对这样的onload处理程序进行排队?既然你已经使用了jQuery,为什么还要使用本机处理程序呢?

function func1() { ... }
function func2() { ... }
function thumbScroller() {
$("#tS3").thumbnailScroller({ 
        scrollerType:"hoverPrecise", 
        scrollerOrientation:"vertical", 
        scrollSpeed:2, 
        scrollEasing:"easeOutCirc", 
        scrollEasingAmount:800, 
        acceleration:4, 
        scrollSpeed:800, 
        noScrollCenterSpace:10, 
        autoScrolling:0, 
        autoScrollingSpeed:2000, 
        autoScrollingEasing:"easeInOutQuad", 
        autoScrollingDelay:500 
    });
}

现在集体称呼他们

jQuery(document).ready(function() {
    func1();
    func2();
    thumbScroller();
});