contentSlideShowInitialized

contentSlideShowInitialized

我使用cycle2在Wordpress中创建了一个幻灯片演示–正常。我希望在窗口大小调整时销毁并重新初始化cycle2,即为移动设备打开幻灯片放映,为桌面重新包裹。我从用户dotty借用了以下脚本:

var contentslideShowElement = $('.content-slide-show');
        var contentSlideShowInitialized = true;
        contentslideShowElement.on( 'cycle-initialised', function() {
            contentSlideShowInitialized = true;
        });
        contentslideShowElement.on( 'cycle-destroyed', function() {
            contentSlideShowInitialized = false;
        });

        $(window).resize(function(){
            destroyContentSlideShowForMobile();
        });

        function destroyContentSlideShowForMobile(){
            if( contentSlideShowInitialized && $(window).width() < 768 ){
                contentslideShowElement.cycle('destroy');
            }

           if( !contentSlideShowInitialized && $(window).width() > 768 ){
                contentslideShowElement.cycle({
                slides: 'li',
                next: '.attachment-large',
                previous: ".prev"
            });
            }
        }


CSS如下:

<ul class="content-slide-show cycle-slideshow" data-cycle-slides="li">


看起来应该可以,但是不幸的是我收到了“ contentSlideShowInitialized is not defined”。

有任何想法吗?

更新:在Jorge的建议下,我将“ contentSlideShowInitialized”的默认值设置为true。这使得包装/展开部分起作用。当我将浏览器宽度减小到700以下时,幻灯片放映会被破坏,但是如果我以较小的尺寸加载页面,幻灯片放映仍然存在。

最佳答案

我认为您需要为contentSlideShowInitialized变量设置默认值:

    var contentslideShowElement = $('.content-slide-show');
    var contentSlideShowInitialized = false; // Set default
    contentslideShowElement.on( 'cycle-initialised', function() {
        contentSlideShowInitialized = true;
    });
    contentslideShowElement.on( 'cycle-destroyed', function() {
        contentSlideShowInitialized = false;
    });

    $(window).resize(function(){
        destroyContentSlideShowForMobile();
    });

    function destroyContentSlideShowForMobile(){
        if( contentSlideShowInitialized && $(window).width() < 768 ){
            contentslideShowElement.cycle('destroy');
        }

       if( !contentSlideShowInitialized && $(window).width() > 768 ){
            contentslideShowElement.cycle({
                slides: 'li',
                next: '.attachment-large',
                previous: ".prev"
            });
        }
    }

10-05 21:01