将scrollTo应用于自动生成的链接
编辑!
结果我在调用滚动时出现了太多撇号。工作代码如下:
$('.miniImage').click(function() {
var $th = $(this);
var id = $th.attr('id');
$.scrollTo("#" + id + "Image", 1000, {offset: {top:96, left:-636} });
});
感谢您的帮助!
我正在为一位艺术家制作一个作品集网站,他希望作品以水平样式显示(因此下面代码中的表格)。这个想法是显示每个图像的缩略图(ol #thumbnails)和右侧的图像。
我想使用scrollTo插件来允许用户单击任何图像并将其滚动到视图中。我正在寻找一些帮助来为此创建正确的 jquery。
本质上,如果我对 jQuery 中的每个链接进行硬编码,我就可以让它工作,但这对于 CMS 驱动的网站来说不太理想,因为它会不断更新。
任何人都可以帮助编写一些适用于每个缩略图链接的代码吗?
我有以下代码:
<div id="content">
<ol id="thumbnails">
<li class="mini"><a class="miniImage" id="horseOne"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseTwo"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseThree"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseFour"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseFive"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseSix"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseSeven"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseEight"><img src="media/images/mini.jpg" alt="" /></a></li>
</ol>
<div id="contentRight">
<table id="work">
<tr>
<td id="horseOneImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseTwoImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseThreeImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseFourImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseFiveImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseSixImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseSevenImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseEightImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
</tr>
</table>
</div>
</div>
我当前的 jQuery 是:
$('.miniImage').click(function() {
var $th = $(this);
var id = $th.attr('id');
$.scrollTo('"#" + id + "Image"', 1000, {offset: {top:96, left:-636} });
});
我尝试使用 .each() 函数将 id 拉为变量来应用scrollTo,但无法让任何东西工作。
任何帮助将不胜感激!
Edit!
Turns out I had just got too many apostrophes going on when calling the scrollto. The working code is below:
$('.miniImage').click(function() {
var $th = $(this);
var id = $th.attr('id');
$.scrollTo("#" + id + "Image", 1000, {offset: {top:96, left:-636} });
});
Thanks for the help!
I am making a portfolio site for an artist who wants there work displayed in a horizontal style (hence the table in the code below). The idea is to display thumbnails of each image (ol #thumbnails) and the images to the right.
I want to use the scrollTo plugin to allow the user to click on any image and have it scroll into view. I am looking for some help in creating the right jquery for this.
Essentially I can make it work if I hard code each link in the jQuery but this is less than ideal for a CMS powered site whicg will be constantly updated.
Can anyone help with some code that will apply to each of the thumbnail links?
I have the following code:
<div id="content">
<ol id="thumbnails">
<li class="mini"><a class="miniImage" id="horseOne"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseTwo"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseThree"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseFour"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseFive"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseSix"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseSeven"><img src="media/images/mini.jpg" alt="" /></a></li>
<li class="mini"><a class="miniImage" id="horseEight"><img src="media/images/mini.jpg" alt="" /></a></li>
</ol>
<div id="contentRight">
<table id="work">
<tr>
<td id="horseOneImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseTwoImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseThreeImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseFourImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseFiveImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseSixImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseSevenImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
<td id="horseEightImage" class="mainImage"><img src="media/images/horse.jpg" alt="" /></td>
</tr>
</table>
</div>
</div>
My current jQuery is:
$('.miniImage').click(function() {
var $th = $(this);
var id = $th.attr('id');
$.scrollTo('"#" + id + "Image"', 1000, {offset: {top:96, left:-636} });
});
I tried to apply the scrollTo using .each() function pulling id's as variables but cannot get anything to work.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:重构为仅调用一次
click
绑定并使用事件委托 -jsFiddle 上的工作示例 - http://jsfiddle.net/UB4wC/2/
Edit: Refactored to call your
click
binding only once and use event delegation -Working example on jsFiddle - http://jsfiddle.net/UB4wC/2/