如何根据鼠标单击单独的图像库设置 jCarousel 的起始图像?

发布于 2025-01-07 17:43:02 字数 1324 浏览 3 评论 0原文

我有一个页面,其中包含一个 jCarousel 滑块(在 div 中)和一个包含缩略图列表的 div。轮播在页面初始加载时隐藏,而缩略图则可见。我想要实现的是,当用户单击特定的缩略图时,缩略图列表(或者我应该说是图库)会淡出,而轮播变得可见,并且首先显示的单击缩略图的图像的较大版本旋转木马。我尝试将轮播的配置选项“start”设置为包含单击的缩略图 ID 的变量,但这给了我奇怪的结果。任何帮助将不胜感激!这是我的代码:

<script type="text/javascript">
$(document).ready(function() {
    var currImage;
    $('#carousel').hide();
    $('.thumb').click(function(){
        $('#thumbs').fadeOut('slow');
        currImage = $(this).attr('id');

        $('#mycarousel').jcarousel({
            start: currImage,
            visible: 1,
            scroll: 1  
        });
        $('#carousel').fadeIn('slow');      
    }); 
});
</script>
<div id="thumbs">
    <ul class="thumb-display">
        <li>
            <a class="thumb" href="#" id="1">
                <img src="pic1-thumb.jpg" />
            </a>
        </li>
        <li>
            <a class="thumb" href="#" id="2">
                <img src="pic2-thumb.jpg" />
            </a>
        </li>
    </ul>
</div>
<div id="carousel">
    <ul id="mycarousel" class="jcarousel-skin-tango">
        <li><img src="pic1-large/></li>
        <li><img src="pic2-large/></li>
    </ul>
</div>

I have a page which contains a jCarousel slider (in a div) and a div which contains a list of thumbnails. The carousel is hidden on initial load of the page, while the the thumbnails are visible. What I'm trying to achieve is when the user clicks on a specific thumbnail, the list of thumbnails (or gallery, i should say) fades out while the carousel becomes visible WITH the larger version of the image of the clicked thumbnail shown first on the carousel. I've tried setting the carousel's configuration option "start" to a variable that contains the clicked thumbnail id, but that gave me odd results. Any help would be greatly appreciated! Here's my code:

<script type="text/javascript">
$(document).ready(function() {
    var currImage;
    $('#carousel').hide();
    $('.thumb').click(function(){
        $('#thumbs').fadeOut('slow');
        currImage = $(this).attr('id');

        $('#mycarousel').jcarousel({
            start: currImage,
            visible: 1,
            scroll: 1  
        });
        $('#carousel').fadeIn('slow');      
    }); 
});
</script>
<div id="thumbs">
    <ul class="thumb-display">
        <li>
            <a class="thumb" href="#" id="1">
                <img src="pic1-thumb.jpg" />
            </a>
        </li>
        <li>
            <a class="thumb" href="#" id="2">
                <img src="pic2-thumb.jpg" />
            </a>
        </li>
    </ul>
</div>
<div id="carousel">
    <ul id="mycarousel" class="jcarousel-skin-tango">
        <li><img src="pic1-large/></li>
        <li><img src="pic2-large/></li>
    </ul>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2025-01-14 17:43:02

看来简单的答案是 start 是从 0 开始的,而不是从 1 开始的。

因此尝试使用:

    $('#mycarousel').jcarousel({
        start: currImage - 1,
        visible: 1,
        scroll: 1  
    });

来初始化 jCarousel。

或者,插件可以正确确定图像的尺寸吗?如果没有,请尝试添加明确的 heightwidth 属性或 CSS 样式。

此外,ID 可能不是最好的“1”、“2”等。请改用 data-imagenum 属性或类似属性。

It appears that the simple answer is that start is 0-based, not 1-based.

So try using:

    $('#mycarousel').jcarousel({
        start: currImage - 1,
        visible: 1,
        scroll: 1  
    });

to initialise the jCarousel.

Alternatively, can the plugin determine the dimensions of the images correctly? If not, try adding explicit height and width attributes or CSS styles.

Also, IDs are probably not best as "1", "2" and so on. Use a data-imagenum attribute or similar instead.

月隐月明月朦胧 2025-01-14 17:43:02

pphou,您对 jCarousel 的预期用途与 带有外部控件的轮播示例。

诀窍是在 initCallback 函数中将缩略图设置为外部控件。

代码将如下所示:

javascript:

$(document).ready(function() {
    $thumbs = $('#thumbs');
    $carousel = $('#mycarousel').hide();
    $carousel.jcarousel({
        visible: 1,
        scroll: 1,
        initCallback: function(carousel) {
            $thumbs.find('a').on('click', function() {
                carousel.scroll($.jcarousel.intval($(this).attr('id')));
//              carousel.scroll($.jcarousel.intval($(this).index()));//may work, if so then omit ids from the HTML
                $thumbs.fadeOut('slow');
                $carousel.fadeIn('slow');
                return false;
            });
        }
    });
});

未测试

您可能需要稍微修改一下代码才能得到您想要的结果。

您还需要建立一种从轮播返回缩略图的方法。我没有谈到这方面。

请记住,如 gregL 指出的那样,将缩略图 ID 从 0(零)开始编号。

pphou, your intended use of jCarousel is very similar to the Carousel with External Controls example.

The trick is to set up your thumbnails as external controls in an initCallback function.

The code will be something like this:

javascript:

$(document).ready(function() {
    $thumbs = $('#thumbs');
    $carousel = $('#mycarousel').hide();
    $carousel.jcarousel({
        visible: 1,
        scroll: 1,
        initCallback: function(carousel) {
            $thumbs.find('a').on('click', function() {
                carousel.scroll($.jcarousel.intval($(this).attr('id')));
//              carousel.scroll($.jcarousel.intval($(this).index()));//may work, if so then omit ids from the HTML
                $thumbs.fadeOut('slow');
                $carousel.fadeIn('slow');
                return false;
            });
        }
    });
});

untested

You may need to play with the code a bit to get exactly what you want.

You will also need to establish a way back to the thumbnails from the carousel. I have not addressed this aspect.

Remember to number the thumbnail ids from 0 (zero) as gregL points out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文