jquery - 将 jCarousel 与 jQuery 模板结合使用

标签 jquery jquery-plugins jcarousel jquery-templates

我正在尝试将 jcarousel 与模板结合使用。当图像直接位于页面中时,轮播它可以完美工作,但我试图通过使用模板使图像动态化(不知道页面加载时将加载哪些图像)。

这就是我所做的,我有这个 jQuery 代码来加载模板(外部模板)

var images = {
    imageItems: [
        { path: '../Content/img/strip/strip_1.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_2.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_3.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_1.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_2.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_3.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_1.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_3.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_2.jpg', name: 'Photo' },
        { path: '../Content/img/strip/strip_1.jpg', name: 'Photo' }
        ]
};

$.get('/jQueryTemplates/_photographerImagesSlideShow.tmpl.htm', function (templates) {
    $('body').append(templates);
    $('#imageSlideShowTemplate').tmpl(images).appendTo('.slidestrip');
});

这是外部模板的代码

<script id="imageSlideShowTemplate" type="x-jquery-tmpl">    
    <ul id="slidestriplist" class="jcarousel-skin-tango">
        {{each imageItems}}
            <li>{{tmpl($value) '#slideShowImage'}}</li>
        {{/each}}
    </ul>
</script>

<script id="slideShowImage" type="x-jquery-tmpl">
    <a href="#" rel="lightbox"><img src="${path}" width="150" height="126" alt="${name}" /></a>
</script>

加载模板后,我有这段代码来初始化轮播,或者至少这是它应该做的事情

$('.slidestrip img').each(function () {
    $(this).hover(function () {
        $(this).stop().animate({ opacity: 1.0 }, 500);
    },
        function () {
            $(this).stop().animate({ opacity: 0.5 }, 500);
        });
});

$('#slidestriplist').jcarousel({
    visible: 6
});

现在的问题是,轮播不起作用,因为我认为,它试图在模板实际注入(inject)页面之前对其进行初始化,从而导致图像垂直加载而不是水平加载,并且没有轮播(即使图像的悬停不执行任何操作)。

所以问题是,有谁知道以这种方式使用模板时如何使其工作?如何仅在模板注入(inject)标记后初始化轮播?

最佳答案

Dave Ward 的帮助下,已针对此问题制定了解决方案。 ,所以我想我会与其他人分享,以防有人遇到相同(或类似的问题)。我需要延迟初始化 jCarousel,为此我需要从 $.get() 调用中调用 jCarousel,如下所示:

   $.get('/jQueryTemplates/_photographerImagesSlideShow.tmpl.htm', function (templates) {
        $('body').append(templates);
        $('#imageSlideShowTemplate').tmpl(images).appendTo('.slidestrip');

        //now load our carousel and add the hover affect to the images
        $('.slidestrip img').each(function () {
            $(this).hover(function () {
                $(this).stop().animate({ opacity: 1.0 }, 500);
            },
            function () {
                $(this).stop().animate({ opacity: 0.5 }, 500);
            });
        });

        $('#slidestriplist').jcarousel({
            visible: 6
        });
    });

感谢戴夫在这件事上的帮助

关于jquery - 将 jCarousel 与 jQuery 模板结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4600765/

相关文章:

jQuery-File-Upload 插件 - 无法删除文件夹

javascript - 在你的 DOM 中有一堆元素有那么糟糕吗?

javascript - 滚动到 jcarousel 的第一项

javascript - 刷新页面后DIV显示 block 状态

jquery - 与 Twitter 时间轴小部件的 CSS 相关错误作为 Chrome 中的 jCarousel 元素

javascript - .mouseover 在放下元素后停止工作

javascript - Laravel 和 Ajax - 将数据插入表而不刷新

php - 为什么我无法使用 ajax 和 laravel 获取表单输入

javascript - 知道所有其他就绪回调何时完成

jquery-plugins - 从文本链接加载 Jquery BlockUI