我对 jQuery 的了解非常基础,所以请耐心等待。
我有一个表格列出了几个图像/缩略图,每个缩略图都有放大和缩小的功能。表格顶部还有两个“放大”和“缩小”按钮,以防用户想要放大/缩小所有图像而不是一张一张地进行。
到目前为止,我已经完成了能够:
- 单击每个缩略图并放大/缩小
- 单击放大按钮并放大所有缩略图
- 单击“缩小”按钮并缩小所有缩略图
我遇到的问题是:
A.当所有缩略图都被单独放大后,禁用放大按钮;然后激活缩小按钮。
B.当所有缩略图都被单独缩小后,停用“缩小”按钮;然后激活放大按钮。
我认为这可以通过 来完成.length()
,但我无法理解逻辑。
我试过这个,但当然它不起作用:
$('a.zoom-trigger.shrink').length(function(){
$('.zin').toggleClass('active');
$('.zout').toggleClass('active');
});
这是Codepen 中的演示。
任何帮助是极大的赞赏。
编辑 -
在演示中,我使用了 3 个元素,但实际上表格中的元素数量是未知的,因为所有数据都来自数据库。
这是我正在使用的 HTML 和 JavaScript:
HTML:
<div class="toggle-zoom">
<a href="#" class="noclick zin">Zoom In</a>
<a href="#" class="noclick zout active">Zoom Out</a>
</div>
<table data-filter="#filter-logos" class="footable">
<thead>
<tr>
<th data-sort-ignore="true">Thumbnail</th>
<th data-sort-ignore="true" title="Sort list">Description</th>
<th title="Sort list">File Name</th>
<th title="Sort list">File Type</th>
<th data-type="numeric" title="Sort list">File Size</th>
<th data-sort-ignore="true">Download</th>
</tr>
</thead>
<tbody>
<tr>
<td class="txt-center img-cell">
<a href="#" class="noclick zoom-trigger link-cell" title="Zoom"><img src="http://placebear.com/g/800/200" alt="" class="tn small"></a>
</td>
<td>Logo horizontal</td>
<td>logo-h.eps</td>
<td>EPS</td>
<td class="txt-right">10KB</td>
<td class="txt-center p0">
<a href="#" class="noclick db ir link-cell download" title="Download">Download</a>
</td>
</tr>
<tr>
<td class="txt-center img-cell">
<a href="#" class="noclick zoom-trigger link-cell" title="Zoom"><img src="http://placebear.com/g/800/201" alt="" class="tn small"></a>
</td>
<td>Logo horizontal</td>
<td>logo-h.eps</td>
<td>EPS</td>
<td class="txt-right">10KB</td>
<td class="txt-center p0">
<a href="#" class="noclick db ir link-cell download" title="Download">Download</a>
</td>
</tr>
<tr>
<td class="txt-center img-cell">
<a href="#" class="noclick zoom-trigger link-cell" title="Zoom"><img src="http://placebear.com/g/800/202" alt="" class="tn small"></a>
</td>
<td>Logo horizontal</td>
<td>logo-h.eps</td>
<td>EPS</td>
<td class="txt-right">10KB</td>
<td class="txt-center p0">
<a href="#" class="noclick db ir link-cell download" title="Download">Download</a>
</td>
</tr>
</tbody>
</table>
JavaScript:
//Zoom
$('.zoom-trigger').click(function(){
$(this).find('img').toggleClass('small');
$(this).toggleClass('shrink');
});
//Zoom In
$('.zin').click(function(){
$('.zoom-trigger').addClass('shrink');
$('.tn').removeClass('small');
$(this).addClass('active').siblings().removeClass('active');
});
//Zoom Out
$('.zout').click(function(){
$('.zoom-trigger').removeClass('shrink');
$('.tn').addClass('small');
$(this).addClass('active').siblings().removeClass('active');
});
/*Active correct zoom button when all thumnails have been clicked*/
/*
$('a.zoom-trigger.shrink').length(function(){
$('.zin').toggleClass('active');
$('.zout').toggleClass('active');
});
*/
//Prevents default action of links
$('.noclick').click(function(e) {
e.preventDefault();
});
同样,这是Codepen 中的演示。
再次感谢。