JQuery - 使用 :eq(clickedPos) 选择器和变量

发布于 2024-10-10 03:04:37 字数 1439 浏览 0 评论 0原文

我可以将 :eq() 选择器与变量一起使用吗?或者我必须使用替代品..但我可以使用哪个? 对于上下文,我使用画廊。

HTML

<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>

<div id="#gallery">
<ul>
<li>Image 1</li> <!-- User will see this 800px x 450px image -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>

伪 Javascript (JQuery)

$('#thumbs ul li').click(function() {
   clickedPos = $(this).index(); //saving the index number of the clicked thumbnail
   $('#gallery ul li:eq(clickedPos)').clone().insertAfter('#gallery ul li:eq(clickedPos)');
   gallerywidth += 800 //gallery width grows a bit
   $('#gallery ul').css({width: gallerywidth});
   $('#gallery ul').animate({right:"+=800"},250);   
});

交互示例
用户点击缩略图 3

HTML 结果

<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>

<div id="#gallery">
<ul>
<li>Image 1</li> <!-- at first the user saw this image -->
<li>Image 3</li> <!-- after scrolling 800px to the right, he now see this one -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>

Can I use the :eq() selector with variables? Or do I have to use alternatives.. but which could I use instead?
For the context, I use a gallery.

HTML

<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>

<div id="#gallery">
<ul>
<li>Image 1</li> <!-- User will see this 800px x 450px image -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>

Pseudo Javascript (JQuery)

$('#thumbs ul li').click(function() {
   clickedPos = $(this).index(); //saving the index number of the clicked thumbnail
   $('#gallery ul li:eq(clickedPos)').clone().insertAfter('#gallery ul li:eq(clickedPos)');
   gallerywidth += 800 //gallery width grows a bit
   $('#gallery ul').css({width: gallerywidth});
   $('#gallery ul').animate({right:"+=800"},250);   
});

Interaction example
USER clicks on Thumbnail 3

HTML Result

<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>

<div id="#gallery">
<ul>
<li>Image 1</li> <!-- at first the user saw this image -->
<li>Image 3</li> <!-- after scrolling 800px to the right, he now see this one -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>

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

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

发布评论

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

评论(2

嗼ふ静 2024-10-17 03:04:37

使用:

$('#gallery ul li:eq('+clickedPos+')')

或:

$('#gallery ul li').eq(clickedPos)

use:

$('#gallery ul li:eq('+clickedPos+')')

or:

$('#gallery ul li').eq(clickedPos)
深府石板幽径 2024-10-17 03:04:37

为什么不使用 $('#gallery ul li:eq(' + clickedPos + ')')

Why not use $('#gallery ul li:eq(' + clickedPos + ')')

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