使用 JQuery Cycle,AnchorBuilder 如何找到 DIV 中图像的 SRC?

发布于 2024-10-31 12:56:00 字数 2013 浏览 7 评论 0原文

JQuery Cycle 插件非常棒。对于我的特定安装,我想结合其中的两个示例:缩略图寻呼机及其循环 DIV 而不是图像的能力。

目标是设置一组缩略图,单击这些缩略图即可更改 DIV。该 DIV 将包含大图像和一个包含 HTML 的框(特别是一些链接)。

我在这里做了一个粗略的版本,拇指位于左下角,DIV 为灰色,位于右下角。图片不是我的;我只是为了演示的目的而抓住它:

https://i.sstatic.net/l49Kt.jpg< /a>

目标:单击缩略图,大图和框中的文本都会发生变化。

缩略图是通过获取幻灯片的 SRC 属性并将其缩小来创建的。不幸的是,当图像嵌入到 DIV 中时,插件无法找到 SRC 属性,因此缩略图会损坏。

以下是构建 Cycle 的代码:

<script type="text/javascript">
  $(function() {
    $('#slideshow').after('<ul id="nav">').cycle({
      fx: 'fade',
      speed:  'slow',
      timeout: 0,
      pager:  '#nav',
      pagerAnchorBuilder: function(idx, slide) {
        return '<li><a href="#"><img src="' + slide.src + '" width="49" height="25" /></a></li>';
            }
        });
    });
    </script>

这适用于以下 HTML 块:

<div id="slideshow" class="pics">
    <img src="/images/01_sm.jpg" class="first" title="title" height="329" />
    <img src="/images/02_sm.jpg" title="title" width="642" height="329" />
    <img src="/images/03_sm.jpg" title="title" width="642" height="329" />
</div>

但正如前面提到的,当它们被封装在 DIV 中时会中断:

<div id="slideshow" class="pics">
    <div><img class="slideimg" src="/images/01_sm.jpg" class="first" title="title" height="329" /><div class="menu">foo</div></div>
    <div><img class="slideimg" src="/images/02_sm.jpg" title="title" width="642" height="329" /><div class="menu">foo</div></div>
    <div><img class="slideimg" src="/images/03_sm.jpg" title="title" width="642" height="329" /><div class="menu">foo</div></div>
</div>

因此,有了所有这些,问题就变成了:

如何替换幻灯片。 src 以及可以查看上述 DIV 并找到当前幻灯片的 SRC 的东西?

提前感谢您的帮助。我希望该解决方案足够简单,可以将其添加到不断扩展的 JQuery Cycle 示例列表中。

The JQuery Cycle Plugin is awesome. For my particular install, I'd like to combine two of their examples: The thumbnail pager, and its ability to cycle DIV's instead of images.

The goal is to have a setup where you have a set of thumbnails that, when clicked, change the DIV. That DIV will contain the large image and a box with HTML (specifically, some links).

I've made a rough version of it here, with the thumbs on the lower-left, and the DIV in gray on the lower-right. The image isn't mine; I just grabbed it for the purposes of the demo:

https://i.sstatic.net/l49Kt.jpg

Goal: Click the thumbnail and both the large image and the text in the box change.

The thumbnails are being created by getting the slide's SRC attribute and shrinking it. Unfortunately, when the images are embedded in DIV's, the plugin can't find the SRC attribute, and so the thumbnails break.

The following is the code to build the Cycle:

<script type="text/javascript">
  $(function() {
    $('#slideshow').after('<ul id="nav">').cycle({
      fx: 'fade',
      speed:  'slow',
      timeout: 0,
      pager:  '#nav',
      pagerAnchorBuilder: function(idx, slide) {
        return '<li><a href="#"><img src="' + slide.src + '" width="49" height="25" /></a></li>';
            }
        });
    });
    </script>

This works fine with the following HTML block:

<div id="slideshow" class="pics">
    <img src="/images/01_sm.jpg" class="first" title="title" height="329" />
    <img src="/images/02_sm.jpg" title="title" width="642" height="329" />
    <img src="/images/03_sm.jpg" title="title" width="642" height="329" />
</div>

But as mentioned breaks when they are encased in DIVs thus:

<div id="slideshow" class="pics">
    <div><img class="slideimg" src="/images/01_sm.jpg" class="first" title="title" height="329" /><div class="menu">foo</div></div>
    <div><img class="slideimg" src="/images/02_sm.jpg" title="title" width="642" height="329" /><div class="menu">foo</div></div>
    <div><img class="slideimg" src="/images/03_sm.jpg" title="title" width="642" height="329" /><div class="menu">foo</div></div>
</div>

So with all that established, the question becomes:

How do I replace slide.src with something that can look into the above DIVs and find the current slide's SRC?

Thanks in advance for your help. I hope that the solution will be simple enough that it can be added to the ever-expanding list of JQuery Cycle examples.

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

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

发布评论

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

评论(2

自此以后,行同陌路 2024-11-07 12:56:00

你尝试过这个吗:

pagerAnchorBuilder: function(idx, slide) {
    return '<li><a href="#"><img src="' + slide.find('img').attr('src') + '" width="49" height="25" /></a></li>';
        }

Did you try this:

pagerAnchorBuilder: function(idx, slide) {
    return '<li><a href="#"><img src="' + slide.find('img').attr('src') + '" width="49" height="25" /></a></li>';
        }
滿滿的愛 2024-11-07 12:56:00

就像 Vasu 所说......但你必须使用 jQuery(slide) 而不是幻灯片:

pagerAnchorBuilder: function(idx, slide) {
    return '<li><a href="#"><img src="' +
           jQuery(slide).find('img').attr('src') +
           '" width="49" height="25" /></a></li>';
}

Like Vasu said ... but you have to use jQuery(slide) instead of slide:

pagerAnchorBuilder: function(idx, slide) {
    return '<li><a href="#"><img src="' +
           jQuery(slide).find('img').attr('src') +
           '" width="49" height="25" /></a></li>';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文