【问题标题】:jQuery document ready conflicting with window onload?jQuery 文档准备好与窗口加载冲突?
【发布时间】:2011-12-30 15:28:14
【问题描述】:

我正在尝试创建一个视频库。我正在使用 jQuery 制作一个滑出面板 这很简单。我也在使用 jQuery 来滚动缩略图。他们都工作得很漂亮。问题是我需要滚动缩略图才能在滑出面板内工作,但它不会。我认为这与准备文档和另一个是窗口加载这两个功能有关。我不确定,因为我是 jQuery 新手。有人能帮忙吗。

这是滑出面板功能。

 $(document).ready(function(){
$(".trigger").click(function(){
    $(".panel").toggle("fast");
    $(this).toggleClass("active");
    return false;
});
});

她是滚动功能。

 <script>
 jQuery.noConflict() 
(function($){
window.onload=function(){ 
$("#tS2").thumbnailScroller({ 
    scrollerType:"clickButtons", 
    scrollerOrientation:"horizontal", 
    scrollSpeed:2, 
    scrollEasing:"easeOutCirc", 
    scrollEasingAmount:600, 
    acceleration:4, 
    scrollSpeed:800, 
    noScrollCenterSpace:10, 
    autoScrolling:0, 
    autoScrollingSpeed:2000, 
    autoScrollingEasing:"easeInOutQuad", 
    autoScrollingDelay:500 
});
}
})(jQuery);
</script>

任何人都可以在这里看到任何会相互冲突的东西吗?

这是整个 html。

 <html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Vertical Sliding Info Panel With jQuery</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<link href="jquery.thumbnailScroller.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="jquery-ui-1.8.13.custom.min.js"></script>

<script>
$(document).ready(function(){
$(".trigger").click(function(){
    $(".panel").toggle("fast");
    $(this).toggleClass("active");
    return false;
});
});
</script>
</head>

<body>

<div class="panel">
<div>
<div id="tS2" class="jThumbnailScroller">
<div class="jTscrollerContainer">
    <div class="jTscroller">
        <a href="#"><img src="thumbs/img1.jpg" /></a>
        <a href="#"><img src="thumbs/img2.jpg" /></a>
        <a href="#"><img src="thumbs/img3.jpg" /></a>
        <a href="#"><img src="thumbs/img4.jpg" /></a>
        <a href="#"><img src="thumbs/img5.jpg" /></a>
        <a href="#"><img src="thumbs/img6.jpg" /></a>
        <a href="#"><img src="thumbs/img7.jpg" /></a>
        <a href="#"><img src="thumbs/img8.jpg" /></a>
        <a href="#"><img src="thumbs/img9.jpg" /></a>
        <a href="#"><img src="thumbs/img10.jpg" /></a>
        <a href="#"><img src="thumbs/img11.jpg" /></a>
        <a href="#"><img src="thumbs/img12.jpg" /></a>
        <a href="#"><img src="thumbs/img13.jpg" /></a>
        <a href="#"><img src="thumbs/img14.jpg" /></a>
        <a href="#"><img src="thumbs/img15.jpg" /></a>
        <a href="#"><img src="thumbs/img16.jpg" /></a>
        <a href="#"><img src="thumbs/img17.jpg" /></a>
        <a href="#"><img src="thumbs/img18.jpg" /></a>
        <a href="#"><img src="thumbs/img19.jpg" /></a>
        <a href="#"><img src="thumbs/img20.jpg" /></a>
    </div>
</div>
<a href="#" class="jTscrollerPrevButton"></a>
<a href="#" class="jTscrollerNextButton"></a>
</div>
<!-- thumbnail scroller markup end -->

<script>
/* jQuery.noConflict() for using the plugin along with other libraries. 
 You can remove it if you won't use other libraries (e.g. prototype, scriptaculous etc.) or 
 if you include jQuery before other libraries in yourdocument's head tag. 
 [more info: http://docs.jquery.com/Using_jQuery_with_Other_Libraries] */
jQuery.noConflict(); 
/* calling thumbnailScroller function with options as parameters */
(function($){
window.onload=function(){ 
$("#tS2").thumbnailScroller({ 
    scrollerType:"clickButtons", 
    scrollerOrientation:"horizontal", 
    scrollSpeed:2, 
    scrollEasing:"easeOutCirc", 
    scrollEasingAmount:600, 
    acceleration:4, 
    scrollSpeed:800, 
    noScrollCenterSpace:10, 
    autoScrolling:0, 
    autoScrollingSpeed:2000, 
    autoScrollingEasing:"easeInOutQuad", 
    autoScrollingDelay:500 
});
}
})(jQuery);
 </script>
<!-- thumbnailScroller script -->
<script src="jquery.thumbnailScroller.js"></script>
</div>
</div>
<a class="trigger" href="#">infos</a>

</body>
</html>

【问题讨论】:

  • 你的 jquery 包括什么?

标签: javascript jquery


【解决方案1】:

thumbnailScroller 插件似乎需要计算拇指包装器的尺寸。在您的情况下,包装器最初不会显示,并且可能会使插件混淆。

您可以在执行插件后尝试隐藏 .panel 元素。

jQuery.noConflict(); 
/* calling thumbnailScroller function with options as parameters */
(function($){
    $(document).ready(function(){
        $(".trigger").click(function(){
            $(".panel").toggle("fast");
            $(this).toggleClass("active");
            return false;
        });
    });
    window.onload=function(){ 
        $("#tS2").thumbnailScroller({ 
            scrollerType:"clickButtons", 
            scrollerOrientation:"horizontal", 
            scrollSpeed:2, 
            scrollEasing:"easeOutCirc", 
            scrollEasingAmount:600, 
            acceleration:4, 
            scrollSpeed:800, 
            noScrollCenterSpace:10, 
            autoScrolling:0, 
            autoScrollingSpeed:2000, 
            autoScrollingEasing:"easeInOutQuad", 
            autoScrollingDelay:500 
        });

        // Hide the .panel only now :
        $(".panel").hide();
    };
})(jQuery);

(您需要修改 CSS 以便最初显示 .panel)

【讨论】:

  • 完美谢谢。当页面加载时,它会闪烁一些小点,但我可以忍受。只要我有足够的代表,我就会这样做。
  • 为了防止闪烁,您可以将 .panel 初始化为 visibility:hidden 以便其尺寸正常。而不是使用hide(),你会使用css({visibility:'visible',display:'none'}) ?
【解决方案2】:

document.ready 将在您的 dom/document 准备就绪后立即执行,但 window.load 将仅在所有对象(图像/文本)都加载到 momery 后执行。

我遇到了类似的问题,我使用 document.ready 为我的多个图像列表制作滚动条,但由于某些图像尺寸很大,所以在加载这些图像之前 document.ready 被解雇并面临问题..

所以后来我用 window.load 改变了它,所以现在用 window.load 我的滚动创建过程只会在所有图像正确加载到内存后才会被触发..

希望这将帮助您获得解决方案..

【讨论】:

  • 我的图像滚动条在滑出式面板之外工作得很好。但是,如果我把它放在滑出面板调用它的 div 中,它只会显示一个图像而没有滚动。
【解决方案3】:

为什么您在同一页面中使用 2 个 document.ready 函数。肯定会产生冲突。只需避免一开始的脚本声明并尝试。

你只需要在 script 标签中的这么多 js 代码。这对你有用。

jQuery.noConflict(); /* calling thumbnailScroller function with options as parameters*/
(function($){
    $(".trigger").click(function(){
       $(".panel").toggle("fast");
       $(this).toggleClass("active");
      return false;
    });


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

【讨论】:

  • 这完全阻止了我的面板滑出。
  • 使用 2 个 document.ready 函数绝对不会产生冲突。这些函数按顺序注册到事件侦听器,并在文档准备好时按该顺序执行。
【解决方案4】:

首先移除$(".trigger").click(...)的双击事件绑定,只需要第二个。然后尝试在 window.onload 内的thumbnailScroller 初始化之后移动此绑定,因为 window.onload 在 DOM 就绪事件之后会触发很多。并删除 (function($){ 环绕 window.onload 为不必要的。试试这个:

$(window).load(function() {
    // Initialize scroller
    $("#tS2").thumbnailScroller({ ... });

    // Initialize panel
    $(".trigger").click(function() {
        $(".panel").toggle("fast");
        $(this).toggleClass("active");
        return false;
    });
});

【讨论】:

  • 这绝对是更简洁的代码,但结果相同。
  • 我的面板按原样弹出,但里面的滚动条不会滚动,只显示一张图像。
  • 这可能是因为您的滚动条在初始化时被隐藏了,而 jQuery width() 函数为隐藏图像返回 0 宽度。 .css('width') 应该可以工作,但在这种情况下,您必须调整插件代码 - 不好。不确定,但也许为图像设置 widthheight 属性可以完成这项工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多