简体   繁体   English

jQuery插件在window.resize之后无法正常工作,但是在刷新整页后仍然可以正常工作

[英]Jquery plugin stops working correctly after window.resize, but works well after full page refresh

I'm implementing a jquery carousel into my page. 我在页面中实现了一个jquery轮播。 Because I have used percentage units rather than fixed units, I need to redraw the carousel after window resize. 因为我使用百分比单位而不是固定单位,所以我需要在调整窗口大小后重新绘制轮播。

My problem is that the carousel stops to work correctly and its function call does not behave normally and renders the carousel oddly after resizing using the following code: 我的问题是轮播会停止正常工作,并且其函数调用无法正常运行,并使用以下代码调整大小后会奇怪地呈现轮播:

jQuery(document).ready(function ($) {
                FnUpdateCarousel();                                                                                         
            }); // end ready    

            var lightbox_resize = false;
            $(window).resize(function() {
                if (lightbox_resize)
                    clearTimeout(lightbox_resize);
                lightbox_resize = setTimeout(function() {
                    FnUpdateCarousel(); 
                }, 100);
            });                                         

            function FnUpdateCarousel() {
                    var widthof = 
                    Math.round(parseInt(jQuery('#services-example-1').css('width'))-(45));
                    jQuery('#services-example-1').services(
                        {
                            width:widthof,
                            height:290,
                            slideAmount:6,
                            slideSpacing:30,
                            touchenabled:"on",
                            mouseWheel:"on",
                            hoverAlpha:"off",
                            slideshow:3000,
                            hovereffect:"off"
                        });
                };

Please guide me on how can I make it behave normally. 请指导我如何使其正常运行。

Here is the solution I reached: 这是我达成的解决方案:

<script type="text/javascript">
            var initialContent = jQuery('#services-example-1').html();
            jQuery(document).ready(function ($) {
                FnUpdateCarousel();                                                                                         
            }); // end ready    

            var lightbox_resize = false;
            $(window).resize(function() {
                if (lightbox_resize)
                    clearTimeout(lightbox_resize);
                lightbox_resize = setTimeout(function() {
                    jQuery('#services-example-1').empty();
                    jQuery('#services-example-1').html(initialContent);
                    FnUpdateCarousel();
                }, 200);
            });                                         

            function FnUpdateCarousel() {
                    var widthof = 
                    Math.round(parseInt(jQuery('#services-example-1').css('width'))-(45));
                    jQuery('#services-example-1').services(
                        {
                            width:widthof,
                            height:290,
                            slideAmount:6,
                            slideSpacing:30,
                            touchenabled:"on",
                            mouseWheel:"on",
                            hoverAlpha:"off",
                            slideshow:3000,
                            hovereffect:"off"
                        });
                };
            </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM